Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 217 Accepted Submission(s): 98
Problem Description
Patrick Star bought a bookshelf, he named it ZYG !!
Patrick Star has N book .
The ZYG has K layers (count from 1 to K) and there is no limit on the capacity of each layer !
Now Patrick want to put all N books on ZYG :
1. Assume that the i-th layer has cnti(0≤cnti≤N) books finally.
2. Assume that f[i] is the i-th fibonacci number (f[0]=0,f[1]=1,f[2]=1,f[i]=f[i−2]+f[i−1]).
3. Define the stable value of i-th layers stablei=f[cnti].
4. Define the beauty value of i-th layers beautyi=2stablei−1.
5. Define the whole beauty value of ZYG score=gcd(beauty1,beauty2,...,beautyk)(Note: gcd(0,x)=x).
Patrick Star wants to know the expected value of score if Patrick choose a distribute method randomly !
Input
The first line contain a integer T (no morn than 10), the following is T test case, for each test case :
Each line contains contains three integer n,k(0<n,k≤106).
Output
For each test case, output the answer as a value of a rational number modulo 109+7.
Formally, it is guaranteed that under given constraints the probability is always a rational number pq (p and q are integer and coprime, q is positive), such that q is not divisible by 109+7. Output such integer a between 0 and 109+6 that p−aq is divisible by 109+7.
Sample Input
1 6 8
Sample Output
797202805
题目大意:现在有n本书,k层书架,你可以将这n本书以任意方式放到这k层书架里。放完之后,如果第 i 层书架里有 cnt 本书,那么这层书架的稳定值 sta = f[cnt](f[cnt]为斐波那契数列第cnt项),这层书架的美丽值 beauty = 2^(sta)-1。最后的总得分为gcd(beauty1,beauty2,...,beautyk)。现在要求总得分的期望为多少?
题目思路:本题需要用到gcd和斐波那契数列的两个小性质:
。
所以对于本题,我们假设第 i 层书架的beauty值为,那么最后的总得分就有
。
这样对于每种方案对答案的贡献我们只需要考虑分配的书本的数量的gcd即可。
由于是将n本书分配到k层书架里,所以每层书架的数量的gcd必然是n的因子。
接着我们就可以枚举考虑每个n的因子来算答案,对于因子g,因为gcd为g,所以所有的书架里书的数量必然是g的某一个倍数,所以我们看成先往若干层书架里都放g本书,然后再将剩下的书按g的倍数分配到这些有书的书架里。这样分配的方案就变成了有n/g本书,往k层书架里放,同时k层书架中可以有书架为空,那么方案数为。但由于gcd为g的情况里包含了若干gcd为g的某些倍数的情况,所以要将gcd为g的倍数的方案数减掉,最后得到gcd严格为g的方案数,那么gcd为g时的总贡献就为。
为了方便计算,我们可以从大到小枚举g,再用一个数组记录一下严格以g为gcd的方案数,然后按上述方法容斥一下即可。
最后由于是要算期望,所以还要除掉所有的方案数,即除掉
还有一个坑点就是斐波那契数列后面的项数是非常大的,所以得采取一下欧拉降幂公式。
具体实现看代码:
#include
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) x&-x
#define pb push_back
#define MP make_pair
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<"["<pii;
typedef vector VI;
const int MX=2e6+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
int n,k,_;
ll f[MX],invf[MX],fib[MX],cnt[MX];
ll qpow(ll a,ll b){
ll res=1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
void init(){
f[0]=1;
for(int i=1;i=0;i--) invf[i]=invf[i+1]*(i+1)%mod;
fib[0]=0;fib[1]=fib[2]=1;
for(int i=3;i n) return 0;
if(m == 0 || m == n) return 1;
return f[n]*invf[n-m]%mod*invf[m]%mod;
}
int main(){
init();
for(scanf("%d",&_);_;_--){
clr(cnt);
scanf("%d%d",&n,&k);
ll ans=0;
for(int i=n;i>=1;i--){
if(n%i) continue;
ll res=C(n/i+k-1,k-1);
for(int j=i*2;j<=n;j+=i) res=(res-cnt[j]+mod)%mod;
cnt[i]=res;
ans=(ans+(res*(qpow(2,fib[i])%mod-1+mod)%mod))%mod;
}
ans=(ans*qpow(C(n+k-1,k-1),mod-2))%mod;
printf("%lld\n",ans);
}
return 0;
}