The Triangle division of the convex Polygon
Test Instructions: How many methods of N convex polygon can be decomposed into disjoint triangles, the last value modulo m.
idea: The example of Cattleya number, just die m is a headache, because m is not necessarily a prime, so there is not necessarily an inverse element.
Solution: The equation is f (n) = (C (n-2), (n-2)) / (n-1))% m; p = n-2, the equation can be translated as: f (P) = ((2*p)! /(p! * (p+1)!))% m;
To s! Decomposition of qualitative factors, the number of statistics. Set the prime number of less than equals S to P1, p2, p3, ..., PK;
The number of elements of each factor is:
for 1 To k q = s 0while 0= q /< c16> Pi + = q while for
So, we can count the factor and number of F (p), numerator +, denominator-. The last calculation is to use the fast power.
Code:
#include <climits>#include<cstdio>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<cstdlib>#include<cstdarg>#include<iostream>#include<fstream>#include<iomanip>#include<sstream>#include<exception>#include<stdexcept>#include<memory>#include<locale>#include<bitset>#include<deque>#include<list>#include<map>#include<Set>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<iterator>#include<functional>#include<string>#include<complex>#include<valarray>using namespaceStd;typedefLong Longll;Const intN = 1e6+7;BOOLTag[n];intP[n>>3];intT;voidPrime () {T=0; memset (Tag,0,sizeoftag); P[t++] =2, tag[4] =0; for(inti =3; i < N; i + =2) { if(!tag[i]) p[t++] =i; for(intj =0K J < T && (k = i * P[j]) < N; ++j) {Tag[k]=1; if(i% p[j] = =0) Break; } } return ;}intN;ll m, ans;intZp[n>>3], mp[n>>3];intTZ, TP;intFactor (intQ[],intu) {//decomposition n! inti; for(i =0; I < T && p[i] <= u; ++i) {intv =u; while(v) {v/=P[i]; Q[i]+=v; } } returni;}voidCatintN) {intnn = n +N; TZ= TP =0; Memset (ZP,0,sizeofZP); Memset (MP,0,sizeofMP); TZ=Factor (ZP, nn); TP=Factor (MP, N); TP= Factor (MP, n+1); for(inti =0; i < TP; ++i) Zp[i]-=Mp[i]; return ;} ll Mult_mod (intAintB, ll M) {LL res= 1LL, TT =(ll) A; while(b) {if(b&1) Res = (RES * TT)%m; TT= TT * TT%m; b>>=1; } returnRes;}voidSolve () {n-=2; Cat (n); Ans=1LL; for(inti =0; I < TZ; ++i) {ans= (ans * mult_mod (P[i], zp[i], m))%m; } printf ("%i64d\n", ans);}intMain () {#ifdef PIT freopen ("c.in","R", stdin);#endif //PITPrime (); while(~SCANF ("%d%i64d", &n, &m)) {solve (); } return 0;}
Hoj 13101 The Triangle division of the Convex Polygon (number theory for Cattleya numbers (modulo not prime))