P16971 [XRCOI Round 2] C. 永恒
Genius_Star · · 题解
官方题解。
笑点解析:这题场上 80% >=60pts 的都是 ai 哥,给 ai 磕头了/bx
出题人水平不足,因此只能想到 ai 也会的做法,在此致歉。
思路:
首先考虑生成函数,对于一个位置
其第
设其展开后为:
我们要求的是
考虑单位根反演:
考虑如何求
考虑
然后来考虑上面的式子:
-
当
i \mid h 时:\alpha^i = 1 ,于是1 + \alpha^i + \cdots + \alpha^{im} = m + 1 。 -
否则:
1 + \alpha^i + \cdots + \alpha^{im} = \frac{1 - \alpha^{(m + 1)i}}{1 - \alpha^i} 。
考虑
专门考虑:
容易发现:
-
当
\gcd(m + 1, h) = d' > 1 ,于是考虑i = \frac{h}{d'} ,此时\alpha^{(m + 1)i} = \alpha^{h \frac{m + 1}{d'}} = 1 ,于是整个式子值为0 。 -
否则
\gcd(m + 1, h) = 1 ,然后就是经典的\alpha^{(m + 1)i} = \alpha^j, 1 \le j < h ,必然可以和某一个位置约掉,于是整个式子为1 。
最后可以得到:
先令
容易发现,首先
直接做是
当
过程中使用龟速乘或者 int128 即可。
完整代码:
#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define lowbit(x) x & (-x)
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
bool Begin;
const int N = 105, mod = 1e9 + 7, M = 5e6 + 100;
const ll _mod = 1ll * mod * mod;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
ll T, n, m, cnt, B, ans;
ll p[N], a[N], p1[M], p2[M];
inline ll calc(ll x, bool f){
if(f)
return (__)p2[x / B] * p1[x % B] % _mod;
else
return 1ll * p2[x / B] * p1[x % B] % mod;
}
inline int qpow(int a, int b){
int ans = 1;
while(b){
if(b & 1)
ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return ans;
}
inline void dfs(int pos, ll now, ll phi, bool f){
if(pos == cnt + 1){
if(__gcd(now, m) == 1){
if(f)
ans = (ans + (__)phi % _mod * calc(n / now, f) % _mod) % _mod;
else
ans = (ans + 1ll * phi % mod * calc(n / now, f) % mod) % mod;
}
return ;
}
for(int i = 0; i <= a[pos]; ++i){
dfs(pos + 1, now, phi, f);
now *= p[pos];
if(!i)
phi *= (p[pos] - 1);
else
phi *= p[pos];
}
}
inline void solve(){
ans = cnt = 0;
n = read(), m = read();
if(n == 1){
write((m + 1) % mod);
putchar('\n');
return ;
}
++m;
ll x = n;
for(ll i = 2; i * i <= x; ++i){
if(x % i == 0){
p[++cnt] = i;
a[cnt] = 0;
while(x % i == 0){
++a[cnt];
x /= i;
}
}
}
if(x != 1){
p[++cnt] = x;
a[cnt] = 1;
}
B = sqrt(n) + 10;
if(n % mod == 0){
ll M = m % _mod;
p1[0] = p2[0] = 1;
for(int i = 1; i <= B; ++i)
p1[i] = (__)p1[i - 1] * M % _mod;
for(int i = 1; i <= B; ++i)
p2[i] = (__)p2[i - 1] * p1[B] % _mod;
dfs(1, 1, 1, 1);
ans = 1ll * (ans / mod) * qpow(n / mod, mod - 2) % mod;
write(ans);
}
else{
int M = m % mod;
p1[0] = p2[0] = 1;
for(int i = 1; i <= B; ++i)
p1[i] = 1ll * p1[i - 1] * M % mod;
for(int i = 1; i <= B; ++i)
p2[i] = 1ll * p2[i - 1] * p1[B] % mod;
dfs(1, 1, 1, 0);
write(1ll * ans * qpow(n % mod, mod - 2) % mod);
}
putchar('\n');
}
bool End;
int main(){
T = read();
while(T--)
solve();
//cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB";
return 0;
}