题解 P4464 【[国家集训队]JZPKIL】
Weng_Weijie · · 题解
这是一道毒瘤题
需要求的是
由
把
交换求和顺序
将
众所周知,自然数
假设已经求出了这个多项式的系数
进一步化简
再一次交换求和顺序
后面相当于三个积性函数的狄利克雷卷积
那么卷积后的还是积性函数
于是将
现在只需要计算
由 Möbius 函数性质,发现只有
枚举
(1)
答案为
(2)
答案为
得到了
整个问题即可解决
刚才还遗留一个问题,自然数
这是一个经典问题,可以通过斯特林数,伯努利数,差分,拉格朗日插值等方法求得(我用的是伯努利数)
代码:
#include <iostream>
#include <algorithm>
#include <unordered_map>
const int N = 3005;
const int mod = 1E9 + 7;
using LL = long long;
using LD = long double;
int tc, factor[N], ifactor[N], B[N], t[N], x, y;
LL n;
std::unordered_map<LL, int> divisor;
inline int pow(int x, int y, int ans = 1) {
for (; y; y >>= 1, x = static_cast<LL> (x) * x % mod)
if (y & 1) ans = static_cast<LL> (ans) * x % mod;
return ans;
}
inline void reduce(int &x) { x += x >> 31 & mod; }
inline void get_Factor(int R) {
factor[0] = 1;
for (int i = 1; i <= R; ++i)
factor[i] = static_cast<LL> (factor[i - 1]) * i % mod;
ifactor[R] = pow(factor[R], mod - 2);
for (int i = R - 1; ~i; --i)
ifactor[i] = static_cast<LL> (ifactor[i + 1]) * (i + 1) % mod;
}
inline int C(int x, int y) {
return static_cast<LL> (factor[x]) * ifactor[y] % mod * ifactor[x - y] % mod;
}
inline void get_Bernoulli(int R) {
B[0] = 1;
for (int i = 1; i <= R; ++i) {
int sum = 0;
for (int j = 0; j < i; ++j)
reduce(sum -= static_cast<LL> (C(i + 1, j)) * B[j] % mod);
B[i] = pow(i + 1, mod - 2, sum);
}
}
inline LL mul(LL x, LL y, LL mod) {
#ifdef ONLINE_JUDGE
return static_cast<__int128> (x) * y % mod;
#else
LL ret = (x * y - static_cast<LL> (static_cast<LD> (x) * y / mod + 0.5) * mod) % mod;
return ret + (ret >> 63 & mod);
#endif
}
inline LL powmod(LL x, LL y, LL mod, LL ans = 1) {
for (; y; y >>= 1, x = mul(x, x, mod))
if (y & 1) ans = mul(ans, x, mod);
return ans;
}
const int primes[] = { 2, 3, 5, 7, 11, 13, 23, 31 };
inline bool prime_test(LL n) {
LL e = n - 1 >> __builtin_ctz(n - 1);
for (int p : primes) if (p < n) {
for (LL d = e, lst = powmod(p, d, n), now; d != n - 1; d <<= 1, lst = now) {
now = mul(lst, lst, n);
if (lst != 1 && lst != n - 1 && now == 1) return false;
}
if (powmod(p, n - 1, n) != 1) return false;
}
return true;
}
inline void get_Poly(int R) {
t[0] = 0;
for (int i = 0; i <= R; ++i)
t[R + 1 - i] = static_cast<LL> (C(R + 1, i)) * B[i] % mod;
for (int i = 0; i <= R + 1; ++i)
t[i] = pow(R + 1, mod - 2, t[i]);
if (R) ++t[R];
}
inline void pollard_rho(LL n) {
if (n == 1) return;
if (prime_test(n)) { ++divisor[n]; return; }
for (; ; ) {
int c = std::rand(), k = 1, i = 0;
LL x = std::rand(), y = x;
for (; ; ) {
x = mul(x, x, n) + c;
LL d = std::__gcd(n, std::abs(x - y));
if (d > 1 && d < n) { pollard_rho(d), pollard_rho(n / d); return; }
if (x == y) break;
if (++i == k) k <<= 1, y = x;
}
}
}
inline int G(int i, int p, int k) {
int ans = 0;
for (int q = 0; q <= k; ++q)
reduce(ans += pow(p, (static_cast<LL> (q) * x + static_cast<LL> (k - q) * i) % (mod - 1)) - mod);
for (int q = 0; q < k; ++q)
reduce(ans -= pow(p, (static_cast<LL> (q) * x + y + static_cast<LL> (k - q - 1) * i) % (mod - 1)));
return ans;
}
inline int solve(LL n, int x, int y) {
divisor.clear(), pollard_rho(n), get_Poly(y);
int ans = 0;
for (int i = 0; i <= y + 1; ++i) {
int p = t[i];
for (auto pk : divisor)
p = static_cast<LL> (p) * G(i, pk.first % mod, pk.second) % mod;
reduce(ans += static_cast<LL> (p) % mod - mod);
}
ans = static_cast<LL> (ans) * pow(n % mod, y) % mod;
return ans;
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
get_Factor(3001);
get_Bernoulli(3001);
std::cin >> tc;
while (tc--)
std::cin >> n >> x >> y, std::cout << solve(n, x, y) << '\n';
return 0;
}