bzoj1951: [Sdoi2010] Ancient pig wen

Source: Internet
Author: User

Number theory synthesis. Fermat theorem, Lucas theorem, Chinese remainder theorem, exgcd, fast power, multiplication inverse.

The first is to calculate each of the n's approximate, simple sqrt (n) enumeration can be.

Then calculate C (i,m) (m in the combination number of pick I, PS: Because both the pros and cons of the online, so mark it. )

Set S=sum (C (i,m))

The topic requires g^ (s)%mod,

g^ (s) =g^ (s% (mod-1)) by the Fermat theorem. So here's a special sentence. g is equal to mod.

But the combinatorial number is calculated because the division is used, so modulus must be prime. 999911659-1=2x3x4679x35617.

The four numbers are set to D[i], and the four numbers are calculated separately.

Because the number of combinations is large, the direct calculation will time out.

Lucas theorem needs to be used. Lucas (N,M) =lucas (n/mod,m/mod) *c (n%mod,m%mod). Here the MoD is not 999911659, but the 4 numbers.

So you get 4 answers m[i].

The calculation of combinatorial number can be converted into fast power calculation according to Fermat theorem.

And then merge with the Chinese remainder theorem.

The answers you get can be written in the following form:

S%D[0]=M[0],S%D[1]=M[1];

Rewritten as S%A1=B1,S%A2=B2 (1)

There are a1*x+b1=a2*y+b2 (x, Y are two unknowns, and any number above does not have any relation)

Set A=A1,B=A2,C=B2-B1

Then the ax+by=c is changed (the sign doesn't matter because Y is not used)

Calculation T=EXTENDED_GCD (a,b,x,y)

If c%t! =0, it shows that the equation is non-solution. T=1 in the subject. (all based on the case t=1)

And then we figure out the ax+by=t. So we multiply each item by C.

x= (x*c%b+b)%b.

Of course such x has countless groups for x1=x+k*b.

Bring in (1) type has S=A1 (x+k*b) +b1.

S=a1*b*k+a1*x+b1.

So there are s% (a1*b) =a1*x+b1.

So b1=b1+a1*x,a1=a1*b.

In this case after this operation A1>B1, in fact, even if t! =1,B1 will also be less than 1.

Finally, the merged return B1 can be completed.

EN TARO Tassadar
#include <cstdio>#include<algorithm>#include<cstring>#include<cmath>#defineLL Long Longusing namespacestd;Const intMoD =999911659;Const intd[]={2,3,4679,35617};intfac[4][40000],m[4];intn,g;intPower (LL A,intEintMoD) {LL res=1;  while(e) {if(e&1) res=res*a%MoD; A=a*a%MoD; E>>=1; }    returnRes;}intCintNintMintx) {if(n<m)return 0; returnFac[x][n]*power (fac[x][m]*fac[x][n-m]%d[x],d[x]-2, D[x])%d[x];}intLucasintNintMintx) {if(m==0)return 1; returnLucas (N/D[X],M/D[X],X) *c (n%d[x],m%d[x],x)%D[x]; }intEXGCD (intAintBint&x,int&y) {if(b==0) {x=1; y=0;returnA; }    Else {        intT=EXGCD (b,a%b,y,x); Y-= (A/b) *x; returnT; }}intsolve () {intA1,b1,a2,b2,a,b,c,x,y; A1=d[0]; b1=m[0];  for(intI=1;i<4; i++) {A2=d[i];b2=M[i]; A=a1,b=a2,c=b2-B1;        EXGCD (A,b,x,y); X= (c*x%b+b)%b;//if (c%t) printf ("test\n");B1=b1+a1*x; A1=a1*b; }    returnB1;}intMain () { for(intI=0;i<4; i++) {fac[i][0]=1;  for(intj=1; j<=40000; j + +) fac[i][j]=fac[i][j-1]*j%D[i]; } scanf ("%d%d",&n,&g); if(g==mod) printf ("0\n"); Else{g=g%MoD; intN2= (int) sqrt (n+0.5);  for(intI=1, j;i<=n2;i++)         if(n%i==0) {J=n/i;  for(intx=0;x<4; x + +) {M[x]= (M[x]+lucas (n,i,x))%D[x]; if(i!=j) m[x]= (M[x]+lucas (n,j,x))%D[x]; }} printf ("%d\n", Power (g,solve (), MoD)); }    return 0; }

bzoj1951: [Sdoi2010] Ancient pig wen

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.