题解:P4270 [USACO18FEB] Cow Gymnasts P
首先考虑移动的过程。由于移动与一头奶牛在第几层有关,所以不妨按照层来考虑奶牛。
考虑所有的奶牛,不难发现把每一层的奶牛看做一个整体,然后一次移动就视作第
由于每一层是独立的,那么就可以把每一层分开看。不妨设现在关注的是第
所以问题就变成枚举最高层并统计方案。不难发现总的方案数为:
把常数都提出来之后变成:
对后面一坨随便处理一下,可以得到:
那么问题就在于枚举因数并求欧拉函数,这个都是简单的,随便写可以做到
#include<bits/stdc++.h>
using namespace std;
#define __MY_TEST__ 0
#define int long long
inline int read()
{
int re=0,f=1;
char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1; ch=getchar();}
while( isdigit(ch)) re=(re<<3)+(re<<1)+(ch^48),ch=getchar();
return re*f;
}
const int mod=1e9+7;
int n,ans;
vector<pair<int,int> >yz;
int ksm(int a,int b)
{
int k=1;
while(b)
{
if(b&1) k*=a,k%=mod;
a*=a,a%=mod;
b/=2;
}
return k;
}
void dfs(int depth,int re,int phi)
{
if(depth==yz.size())
{
if(re==n) return ;
ans=(ans+ksm(2,re)*(phi%mod)%mod)%mod;
return ;
}
dfs(depth+1,re,phi);
phi*=yz[depth].first-1;
for(int i=1;i<=yz[depth].second;i++)
{
re/=yz[depth].first;
dfs(depth+1,re,phi);
phi*=yz[depth].first;
}
}
signed main(){
#if __MY_TEST__
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
n=read();
int ts=n;
for(int i=2;i*i<=ts;i++)
{
if(ts%i==0)
{
int cnt=0;
while(ts%i==0) cnt++,ts/=i;
yz.emplace_back(i,cnt);
}
}
if(ts) yz.emplace_back(ts,1);
dfs(0,n,1);
ans+=2-n;
cout<<(ans%mod+mod)%mod<<endl;
}