HDU-6363 bookshelf



Patrick Star bought a bookshelf, he named it ZYG !!
Patrick Star has book .
The ZYG has layers (count from to ) and there is no limit on the capacity of each layer !
Now Patrick want to put all books on ZYG :
1. Assume that the i-th layer has books finally.
2. Assume that is the i-th fibonacci number ().
3. Define the stable value of i-th layers .
4. Define the beauty value of i-th layers .
5. Define the whole beauty value of ZYG (Note: ).
Patrick Star wants to know the expected value of if Patrick choose a distribute method randomly !







#include<bits/stdc++.h>
#define me(a,x) memset(a,x,sizeof(a))
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
#define sc scanf
#define itn int
#define STR clock_t startTime = clock();
#define END clock_t endTime = clock();cout << double(endTime - startTime) / CLOCKS_PER_SEC *1000<< "ms" << endl;
using namespace std;
const int N=1e6+5;
const long long mod=1e9+7;
const long long mod2=998244353;
const int oo=0x7fffffff;
const int sup=0x80000000;
typedef long long ll;
typedef unsigned long long ull;
template <typename it>void db(it *begin,it *end){while(begin!=end)cout<<(*begin++)<<" ";puts("");}
template <typename it>
string to_str(it n){string s="";while(n)s+=n%10+'0',n/=10;reverse(s.begin(),s.end());return s;}
template <typename it>int o(it a){cout<<a<<endl;return 0;}
inline ll mul_64(ll x,ll y,ll c){return (x*y-(ll)((long double)x/c*y)*c+c)%c;}
inline ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
inline void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=x*(a/b);}
short int mu[N];
int inv_fac[N<<1|1],f[N],inv[N<<1|1],fac[N<<1|1];
void pre(){
    inv[0]=inv[1]=inv_fac[0]=fac[0]=1;
    for(int i=2;i<2*N;i++) inv[i]=1LL*inv[mod%i]*(mod-mod/i)%mod;
    for(int i=1;i<2*N;i++)fac[i]=1LL*fac[i-1]*i%mod;
    for(int i=1;i<2*N;i++)inv_fac[i]=1LL*inv_fac[i-1]*inv[i]%mod;
    mu[1]=f[1]=1;
    for(itn i=2;i<N;i++)f[i]=(1LL*f[i-1]+f[i-2])%(mod-1);
    for(int i=1;i<N;i++)
    for(int j=i*2;j<N;j+=i)mu[j]-=mu[i];
}
ll C(ll m,ll n){
    return 1LL*fac[m]*inv_fac[m-n]%mod*inv_fac[n]%mod;
}
int n,k;
ll g(int i){
    ll ans=0;
    for(int d=i;d<=n;d+=i){
        if(n%d)continue;
        ans+=mu[d/i]*C(n/d+k-1,k-1);
        if(ans>=mod||ans<0)ans=(ans%mod+mod)%mod;
    }
    return ans;
}
ll h(int x){
    if(x>=45)return ksm(2,f[x]+mod-1,mod)-1;
    return ksm(2,f[x],mod)-1;
}
int main(){
    pre();
    int t;cin>>t;
    while(t--){
        sc("%d%d",&n,&k);
        ll x=C(n+k-1,k-1);x=ksm(x,mod-2,mod);
        ll ans=0;
        for(int d=1;d*d<=n;d++){
            if(n%d==0){
                ans+=h(d)*g(d)%mod;
                if(ans>=mod)ans%=mod;
                if(d!=n/d){
                    ans+=h(n/d)*g(n/d)%mod;
                    if(ans>=mod)ans%=mod;
                }
            }
        }
        ans=(ans%mod+mod)%mod;
        o(ans*x%mod);
    }
}