To find the large number of combined HIT2813

Source: Internet
Author: User

Test instructions: Give N,m,p, C (n+m,n)%p


By using the integer decomposition of factorial, the form of C as factorial is decomposed into a prime-value expression.


Code:

#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> Using namespace std, #define PB push_back#define MP make_pair#define REP (i,x,n) for (int i=x;i< (n); ++i) #define for (I,l, h) for (int i= (l); i<= (h), ++i) #define FORD (i,h,l) for (int i= (h); i>= (l); i) #define SZ (x) ((int) (x). Size ()) #define All (x) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%d", &x, &y, &z) #define OI (x) printf ("%d", x), #define RS (x) scanf ("%s", (x)) #define MS0 (x) memset ( (x), 0, sizeof ((x))) #define MS1 (x) memset ((x),-1, sizeof ((x))) #define LEN (x) strlen (x) #define F first#define S second#def  Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &AMP;A,CONST int    &AMP;B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*----------------Please-----does-----not-----HACK-----ME--------------------*/const int n=2e5+5;int prime[n+5]; int vis[n+5];int cnt;void getprime () {cnt=0;for (int i=2;i<=n;i++) {if (!vis[i]) prime[cnt++]=i;for (int j=0;j<cnt    &&prime[j]<=n/i;j++) {vis[prime[j]*i]=1; if (i%prime[j]==0) break;}}}    int cal (int n,int p) {if (n<p) return 0; Return n/p+cal (n/p,p);}    int Mymul (int a,int b,int q) {int res=0;        while (b) {if (b&1) res= ((Long Long) res+a)%q;        A= (Long Long) a<<1)%q;    b>>=1; } return res;    int mypow (int a,int b,int q) {int res=1;        while (b) {if (b&1) Res=mymul (RES,A,Q);        A=mymul (A,A,Q);    b>>=1; } return res; int main () {MS0 (VIS); Getprime (); int T;    RI (T); while (t--) {int n,m,p;    RIII (N,M,P);    Long long int ans=1;    for (int i=0;prime[i]<=n+m;i++) {int tmp=cal (n+m-2,prime[i]);    Tmp-=cal (N-1,prime[i]);    Tmp-=cal (M-1,prime[i]);Ans= (ans* (Long Long) Mypow (prime[i],tmp,p))%p;        } Cout<<ans<<endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

To find the large number of combined HIT2813

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.