Test Instructions: the number of coprime in the request, wherein.
Analysis: because, so, we are easy to know as to the conclusion
for two positive integers and, if they are multiples, then the number of
This conclusion is well proven, because the number of the in and of the intertextuality is, and know, so
conclusion is established. So for the subject, the answer is
In fact, as long as the inverse of the prime number with EXGCD to beg for a good, the rest is not used
Inverse element Recursive method:
#include <stdio.h>#include<string.h>Const intn=1e7+ the; typedefLong Longll;intPr[n],p[n],cnt,mod;intInv[n],ans1[n],ans2[n];intRead () {intx=0;CharCh=GetChar (); while(ch<'0'|| Ch>'9') ch=GetChar (); while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx;}voidinit () {ans1[1]=ans2[1]=inv[1]=1; for(intI=2; i<n;i++) {Ans1[i]= (LL) ans1[i-1]*i%MoD; if(!P[i]) pr[++cnt]=i; for(intj=1; j<=cnt&&i*pr[j]<n;j++) {P[pr[j]*i]=1; if(i%pr[j]==0) Break; } } for(intI=2; i<n&&i<mod;i++) {Inv[i]= (mod-(ll) mod/i) *inv[mod%i]%MoD; } for(intI=2; i<n;i++) {Ans2[i]=ans2[i-1]; if(!P[i]) ans2[i]= (ll) ans2[i]* (i-1)%mod*inv[i%mod]%MoD; }}intMain () {intt,n,m; scanf ("%d%d",&t,&MoD); Init (); while(t--) {n=read (); m=read (); printf ("%d\n", (LL) ans1[n]*ans2[m]%MoD); } return 0;}
Expand Euclid to seek inverse yuan
#include <stdio.h>#include<string.h>Const intn=1e7+ the; typedefLong Longll;intPr[n],p[n],cnt,mod;intInv[n],ans1[n],ans2[n];intRead () {intx=0;CharCh=GetChar (); while(ch<'0'|| Ch>'9') ch=GetChar (); while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx;}intEX_GCD (intAintBint&x,int&y) { if(!b) {x=1, y=0; returnA; } intANS=EX_GCD (b,a%b,y,x); Y-=a/b*x; returnans;}intGETINV (inti) { intx, y; EX_GCD (I,mod,x,y); X= ((x%mod) +mod)%MoD; returnx;}voidinit () {ans1[1]=ans2[1]=inv[1]=1; for(intI=2; i<n;i++) {Ans1[i]= (LL) ans1[i-1]*i%MoD; if(!P[i]) pr[++cnt]=i,inv[i]=GETINV (i); for(intj=1; j<=cnt&&i*pr[j]<n;j++) {P[pr[j]*i]=1; if(i%pr[j]==0) Break; } } for(intI=2; i<n;i++) {Ans2[i]=ans2[i-1]; if(!P[i]) ans2[i]= (ll) ans2[i]* (i-1)%mod*inv[i%mod]%MoD; }}intMain () {intt,n,m; scanf ("%d%d",&t,&MoD); Init (); while(t--) {n=read (); m=read (); printf ("%d\n", (LL) ans1[n]*ans2[m]%MoD); } return 0;}
Http://hzwer.com/5863.html
http://blog.csdn.net/acdreamers/article/details/8220787
Bzoj 2186 [Sdoi2008] salad Princess puzzle "inverse meta"