bzoj1951: [Sdoi2010] Ancient pig wen

Source: Internet
Author: User

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

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.