Bzoj 2186 [Sdoi2008] salad Princess puzzle "inverse meta"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.