题目:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=34692
输入一个数N(1 <= N <= 1000000)。
输出解的数量Mod 10^9 + 7。
2
2
#include <iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int maxn=1e6+7,mod=1e9+7; LL sta[maxn],pnum[maxn],top; LL cal(LL n,LL v){ if(n==0)return 0; return (n/v+cal(n/v,v)%mod)%mod; } bool notprime[maxn]; LL prime[maxn/10],top1; void getprime(){ for(LL i=2;i<=maxn;i++){ if(!notprime[i]){ prime[top1++]=i; notprime[i]=1; } for(LL j=0;j<top1&&i*prime[j]<=maxn;j++){ notprime[i*prime[j]]=1; if(i%prime[j]==0)break; } } } void fenjie(LL m){ top=0; memset(sta,0,sizeof(sta)); memset(pnum,0,sizeof(pnum)); for(LL i=0;i<top1&&prime[i]<=m;i++){ sta[top]=prime[i]; pnum[top++]=cal(m,prime[i]); } } LL Extend_Eulid(LL b,LL a) { LL x1,x2,x3,y1,y2,y3 ; x1=1,x2=0,x3=a,y1=0,y2=1,y3=b ; while(y3 && y3!=1) { LL q=x3/y3 ; LL t1,t2,t3 ; t1=x1-q*y1,t2=x2-q*y2,t3=x3-q*y3 ; x1=y1,x2=y2,x3=y3 ; y1=t1,y2=t2,y3=t3 ; } if(!y3)return -1 ; return y2 ; } int main() { //freopen("cin.txt","r",stdin); LL n; LL ni=Extend_Eulid(2,mod); ni=(ni+mod)%mod; getprime(); while(~scanf("%lld",&n)){ fenjie(n); LL ans=1; for(LL i=0;i<top;i++){ LL temp=(2*pnum[i]%mod+1)%mod; ans=(ans*temp)%mod; } printf("%lld\n",(ans+1)*ni%mod); } return 0; }一些人是这样得到逆元的:
LL quick_mod(LL a,LL p){ LL ans=1; while(p){ if(p&1)ans=ans*a%mod; a=a*a%mod; p>>=1; } return ans; } <pre name="code" class="cpp">LL m=quick_mod(2LL,LL(mod)-2);