Title Address: HDU 3944
Test instructions: Tell you in a point in the Yang Hui triangle (row N m column), ask you to go from (0,0) point to the point passing through the points of the least weight and (can only go down or oblique walk). At the same time, the prime number p takes the remainder
Idea: According to the known point (n,m), if N/2 >= m, then from the known point, you can go straight to the direction of the oblique, until the boundary, then the weight and C (n,m) +c (n-1,m-1) .... C (n+1,m) + (N-M) can then be obtained by combining the formula 22 of the combined number, if n/2< m, then it is going up, and the weight is C (n,m) +c (n-1,m) +c (n-2,m) .... equals C (n+1,m+1) +m. Because also according to the properties of the combination number when m<=n/2,m=n-m. So there's only a second case left.
Also with Lucas theorem simplification, but the sample has 1e5 groups, very large, beginning with the traditional method of the tle. Here to pre-treatment, will each number factorial with 10000 or less each prime of the remainder is preserved, then directly use. When a number >= a prime, the remainder of the prime is definitely equal to 0, which is equivalent to an optimization.
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#include <bitset>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef__int64 LL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;using namespace STD;Const intmaxn=1e4+Ten; bitset<Maxn>priintPRIME[MAXN];intFLAG[MAXN];intFAC[MAXN][MAXN];intINV[MAXN][MAXN];intk=0;intN,m,p;voidIs_prime () {pri.Set();memset(Flag,0,sizeof(flag)); for(intI=2; i<maxn; i++) {if(Pri[i]) {prime[++k]=i; Flag[i]=k; for(LL j=i+i; j<maxn; j+=i) pri[j]=0; } }}intMODXP (intAintBintMoD) {intres=1; while(b>0) {if(b&1) Res=res*a%mod; B=b>>1; A=a*a%mod; }returnRes;}voidInit () { for(intI=1; i<=k; i++) {fac[i][0]=1; inv[i][0]=1; for(intj=1; j<prime[i]; J + +) {fac[i][j]= (fac[i][j-1]*J)%prime[i]; INV[I][J]=MODXP (fac[i][j],prime[i]-2, Prime[i]); } }}intCintNintm) {if(m>n)return 0;if(m==n)return 1;intT=FLAG[P];returnfac[t][n]* (inv[t][n-m]*inv[t][m]%p)%p;}intLucas (intNintm) {if(m==0)return 1;returnC (n%p,m%p) *lucas (n/p,m/p)%p;}intMain () {intIcase=1; Is_prime (); Init (); while(~scanf(" %d%d%d", &n,&m,&p)) {if(m<=n/2) m=n-m;printf("Case #%d:%d\n", icase++, ((M+lucas (n+1, m+1) (%p) +p)%p); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3944-DP? (Lucas theorem + preprocessing)