这个题其实不难。。只是当时不会数学吧。。
首先这个题目比较绕。。要先化简一波。。
首先是
这个可以这样考虑,像2^n-1这个形式化成二进制就是n个1,那么他们取gcd就会形成gcd个1(gcd个1可以通过左移,即乘若干个2把每个数逐一除掉),因此
然后枚举gcd(设枚举的值为k),可以发现n必须被分成若干个k放进书架,因此看成将n/k本书随机放进k个书架就可以了。。
然后将n本书放进k个书架可以这样考虑,因为有些书架可以不放书,因此不能直接用插板法,而我们可以手动给每个书架加一本书,这样就可以插板了,为
再回到将n/k本书随机放进k个书架这里,由于通过上面的算法只能算出把n/k本书随机放进这些书架,而没有保证这些数的gcd=1,即这样算出来的是gcd能被k整除的时候的方案数,不妨设为g(k),所以,而f[k]比较大,所以需要欧拉降幂一下
算出g(k)容斥一下就可以了,设答案为f(k),那么f(k)=g(k)-f(2k)-f(3k)-....f(mk),这样复杂度为O(nlogn)
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2)
#define NM 1000005
#define nm 10005
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e9+7;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,k;
ll f[NM],invp[2*NM],p[2*NM],ans,g[NM];
ll qpow(ll x,ll t){return t?qpow(sqr(x)%inf,t>>1)*(t&1?x:1)%inf:1LL;}
ll solve(ll n){return p[n+k-1]*invp[n]%inf*invp[k-1]%inf;}
int main(){
n=1e6;
f[1]=p[1]=invp[1]=1;
inc(i,2,n)f[i]=(f[i-1]+f[i-2])%(inf-1);
inc(i,2,2*n)p[i]=p[i-1]*i%inf,invp[i]=invp[inf%i]*(inf-inf/i)%inf;
inc(i,2,n*2)invp[i]=invp[i-1]*invp[i]%inf;
int _=read();while(_--){
n=read();k=read();ans=0;mem(g);
for(int i=1;sqr(i)<=n;i++)if(n%i==0)g[i]=solve(n/i),g[n/i]=solve(i);
dec(i,n,1)if(n%i==0){
for(int j=i*2;j<=n;j+=i)g[i]+=inf-g[j],g[i]%=inf;
ans+=g[i]*(qpow(2,f[i])-1+inf)%inf;ans%=inf;
}
ans=ans*invp[n+k-1]%inf*p[n]%inf*p[k-1]%inf;
printf("%lld\n",ans);
}
return 0;
}
bookshelf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 823 Accepted Submission(s): 365
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
Source
2018 Multi-University Training Contest 6
Recommend
chendu