hdu6363 bookshelf 容斥+数列+数论gcd定理(也可以Möbius)

时间:2023-03-08 22:39:56

hdu6363 bookshelf 容斥+数列+数论gcd定理(也可以Möbius)

hdu6363 bookshelf 容斥+数列+数论gcd定理(也可以Möbius)

#define _CRT_SECURE_NO_WARNINGS
#include<cmath>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 2e6 + ;
const ll mod = 1e9 + ; int n, m, k; ll inv[maxn], f[maxn], fac[maxn];
ll c[maxn];
long long kpow(long long a, long long n) {
long long res = ;
while (n > ) {
if (n & )res = res * a%mod;
a = a * a%mod;
n >>= ;
}
return res;
}
void init() {
f[] = ; f[] = ;
fac[] = fac[] = ;
inv[] = ;
rep(i, , maxn) {
fac[i] = fac[i - ] * (ll)i % mod;
inv[i] = kpow(fac[i], mod - );
f[i] = (f[i - ] * f[i - ]) % mod;
//f[i]-1==F[i]==2^fi-1
}
}
ll C(int n, int m) {
if (n < m) return 0ll;
if (m == || n == m) return 1ll;
if (n - == m || m == ) return n;
return fac[n] * inv[m] % mod * inv[n - m] % mod;
} int main() {
int t;
cin >> t;
init();
while (t--) {
cin >> n >> k;
mmm(c, );ll ans = ;
per(g, n, ) if(n%g==){
c[g] = C(n / g + k - , k - );
for (int gg = * g; gg <= n; gg += g) {c[g] -= c[gg];if (c[g] < )c[g] += mod;}
ans += c[g] * (f[g] - ) % mod;ans %= mod;
}
ans *= kpow(C(n + k - , k - ), mod - );ans %= mod;
cout << ans << endl;
}
}
/* */