A question that sets out all the more important theorems of number theory
If you have not learned the fast power, Fermat theorem, Lucas theorem, the Chinese remainder theorem , please turn off the computer science and culture class, Oi is not suitable for you , please go out to the left of Baidu/google
This is the big stupid question.
In addition to pay attention to the special n=p-1, Fermat theorem is not established, direct output 0
#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <cstdlib > #include <algorithm> #define LL long longusing namespace Std;const int p0=999911659; const int P1=999911658;const int P[4]={35617,4679,3,2};int pow (int x,int y,int p) {int ret=1;while (y) {if (y&1) ret= (ll ) ret*x%p;x= (ll) x*x%p;y=y>>1;} return ret;} int inv (int x,int P) {return pow (x,p-2,p);} int fact[66666];int C (int n,int m,int P) {if (n<m) return 0;return (LL) FACT[N]*INV (fact[n-m],p)%P*INV (fact[m],p)%P;} int Lucas (int n,int m,int P) {if (!n&&!m) return 1;return (LL) Lucas (n/p,m/p,p) *c (n%p,m%p,p)%P;} int N,g;int Main () {scanf ("%d%d", &n,&g), if (g==p0) {puts ("0"); return 0;} for (int i=fact[0]=1;i<=p[0];++i) fact[i]= (LL) fact[i-1]*i%p1;int tot=0;for (int k=0;k<4;++k) {int now=0;for (int i =1;i*i<=n;++i) if (n%i==0) {(Now+=lucas (n,i,p[k]))%=p[k];if (n/i!=i) (Now+=lucas (N,n/i,p[k]))%=p[k];} (tot+= (LL) now* (P1/p[k])%P1*INV (P1/p[k],p[k])%p1)%=p1;} printf ("%D\n ", pow (g,tot,p0)); return 0;}
bzoj1951: [Sdoi2010] Ancient pig wen