这个题其实不难。。只是当时不会数学吧。。
首先这个题目比较绕。。要先化简一波。。
首先是
这个可以这样考虑,像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
#include
#include
#include
#include
#include
#include
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
Statistic | Submit | Discuss | Note