DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 2204 Accepted Submission(s): 705
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/68ba60d4a793fa9a0a3fbdbcccd96a3f.jpeg)
Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,…and the column from left to right 0,1,2,….If using C(n,k) represents the number of row n, column k. The Yang Hui Triangle has a regular pattern as follows.
C(n,0)=C(n,n)=1 (n ≥ 0)
C(n,k)=C(n-1,k-1)+C(n-1,k) (0<k<n)
Write a program that calculates the minimum sum of numbers passed on a route that starts at the top and ends at row n, column k. Each step can go either straight down or diagonally down to the right like figure 2.
As the answer may be very large, you only need to output the answer mod p which is a prime.
Input
Input to the problem will consists of series of up to 100000 data sets. For each data there is a line contains three integers n, k(0<=k<=n<10^9) p(p<10^4 and p is a prime) . Input is terminated by end-of-file.
Output
For every test case, you should output "Case #C: " first, where C indicates the case number and starts at 1.Then output the minimum sum mod p.
Sample Input
1 1 2 4 2 7
Sample Output
Case #1: 0 Case #2: 5
Author
phyxnj@UESTC
Source
Recommend
这道题是一道比较经典的组合数学的题。开始从DP的角度进行题目的引入。但是我们通过一个一个列举就可以发现规律,规律是result=n-m+C(n-m,0)+C(n-m+1,1)+……+C(n,m)。由于C(n,m)+C(n,m+1)=C(n+1,m+1)。所以result=n-m+C(n+1,m);由于n,m都很大,而且取模的数p为素数,p还小于10^5数量级。所以必然想到大整数取模,用Lucas定理。但是由于p的范围是10000,直接用Lucas定理超时了。。因为init的时候要消耗不少时间。所以只能先对10000的素数进行打表,然后预处理所有阶乘模素数的值。保证不会TLE。
参考代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=l;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
const int N=10002;
int n,m,p;
ll fac[1300][N];
int prime[1300],cnt;
bool flag[N];
void init()
{
cnt=0;
for(int i=1; i<=N; i++)
flag[i]=1;
flag[0]=flag[1]=0;
for(int i=2; i<=N; i++)
{
if(flag[i])
{
for(int j=2*i; j<=N; j+=i)
flag[j]=0;
prime[cnt++]=i;
}
}
for(int i=0; i<cnt; i++)
{
fac[i][0]=1;
for(int j=1; j<=prime[i]; j++)
fac[i][j]=fac[i][j-1]*j%prime[i];
}
}
ll Pow(ll a,ll b)
{
ll tmp=a%p,ans=1;
while(b)
{
if(b&1) ans=ans*tmp%p;
tmp=tmp*tmp%p;
b>>=1;
}
return ans;
}
ll C(ll n,ll m,int pos)
{
if(m>n)
return 0;
return fac[pos][n]*Pow(fac[pos][m]*fac[pos][n-m],p-2)%p;
}
ll Lucas(ll n,ll m,int pos)
{
if(m==0)
return 1;
return C(n%p,m%p,pos)*Lucas(n/p,m/p,pos)%p;
}
int Find(ll p)
{
int l=0,r=cnt;
while(l<r)
{
int m=(l+r)>>1;
if(prime[m]==p)
return m;
if(prime[m]>p) r=m;
else l=m+1;
}
return -1;
}
int main()
{
int cas=1;
init();
while(~scanf("%d%d%d",&n,&m,&p))
{
int pos=Find(p);
if(m>n/2) m=n-m;
ll ans=(Lucas(n+1,m,pos)+n-m)%p;
printf("Case #%d: %I64d\n",cas++,ans);
}
}