bjfu1238 Cattleya number of surplus

Source: Internet
Author: User

The topic is to specify N, Cattleya CA (n)%m. The Cattleya number has recursive formula, general formula and approximate Formula Three, because to take the remainder, so the approximate formula can not be used directly, recursive formula I simply tried, TLE. So we can only start with the general formula.

Ca (n) = (2*n)! /n! /(N+1)!

The idea is to decompose the CA (n) mass factor and then calculate the final answer with a quick power. However, when the calculation of n! from 1 to N in turn, the quality factor decomposition, it must be time-out, fortunately, the factorial to take the rest of the law, and constantly remove the element factor can be.

Finally, or grazing, perhaps the screen is written in general, but also the title of the request too carved.

/** Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespacestd; #ifdef on_local_debug#else#endiftypedefLong LongLL;Const intMAXN =80000;Const intN =1000001;BOOLIsprime[n +3];//use two more elements to avoid judging boundariesintPN, PT[MAXN], NUM[MAXN];voidinit_prime_table () {memset (IsPrime,true,sizeof(IsPrime)); intp =2, Q, del; Doubletemp;  while(P <=N) { while(!isprime[p]) {p++; }        if(P > N) {//is over             Break; } temp= (Double) p; Temp*=p; if(Temp >N) Break;  while(Temp <=N) {del= (int) temp; Isprime[del] =false; Temp*=p; } q= p +1;  while(Q <N) { while(!isprime[q]) {q++; }            if(q >= N) { Break;} Temp= (Double) p; Temp*=Q; if(Temp > N) Break;  while(Temp <=N) {del= (int) temp; Isprime[del]=false; Temp*=p; } q++; } P++; } PN=0;  for(inti =2; I <= N; i++) {        if(Isprime[i]) {PT[PN++] =i; }    }}intModular_exp (intAintBintc) {LL res, temp; Res=1% c, temp = a%C;  while(b) {if(B &1) {res= Res * Temp%C; } temp= Temp * Temp%C; b>>=1; }    return(int) Res;voidGetnums (intN) {intx =2*N; intLen =PN;  for(inti =0; I < len && pt[i] <= x; i++) {        intR =0, a =x;  while(a) {R+ = A/Pt[i]; A/=Pt[i]; } Num[i]=R; } x=N;  for(inti =0; I < len && pt[i] <= x; i++) {        intR =0, a =x;  while(a) {R+ = A/Pt[i]; A/=Pt[i]; } Num[i]-=R; } x= n +1;  for(inti =0; I < len && pt[i] <= x; i++) {        intR =0, a =x;  while(a) {R+ = A/Pt[i]; A/=Pt[i]; } Num[i]-=R; }}intMain () {#ifdef on_local_debug freopen ("data.in","R", stdin);//freopen ("test.in", "R", stdin);//freopen ("Data.out", "w", stdout);#endif    intN, M;    Init_prime_table ();  while(SCANF ("%d%d", &n, &m) = =2) {getnums (n); LL ans=1; intN2 =2*N;  for(inti =0; Ans && (i < PN) && Pt[i] <= n2; i++) {            if(Num[i] >0) {ans= ans *(LL) modular_exp (Pt[i], num[i], M); Ans%=m; }} printf ("%d\n", (int) ans); }    return 0;}

bjfu1238 Cattleya number of surplus

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.