hdu 3944 DP? (Lucas定理)

DP?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)
Total Submission(s): 3095    Accepted Submission(s): 970


Problem Description

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
xubiao   |   We have carefully selected several similar problems for you:   3940  3941  3942  3943  3945 
 

Statistic |  Submit |  Discuss | Note

题解:Lucas定理

可能产生答案的只有两种走法


(1)先走最外侧的斜边,然后竖直向下走到达c(n,m)

那么我们会走过m+1个1

然后还需要加入c(m+1,m)+c(m+2,m)+c(m+3,m)+...+c(n,m)

然后我先强行加入c(m+1,m+1),然后可以将c(m+1,m)和c(m+1,m+1)合并成c(m+2,m+1),然后不断向后合并,最终得到c(n+1,m+1),因为我们加入了c(m+1,m+1)=1所以最终的答案是c(n+1,m+1)+m

(2)先竖直向下,然后走斜边

那么我们会先走过n-m个1

然后还需要加入c(m,0)+c(m+1,1)+c(m+2,2)+...+c(n,m)

c(m,0)=c(m+1,0)替换,然后将c(m+1,0),c(m+1,1)合并得到c(m+2,1),然后不断向后合并最终得到c(n+1,m)

所以最终的答案是c(n+1,m)+n-m

然后观察后发现,单独的1的个数多的方案最终的结果会更优。

所以当n-m>m,选择方案(2),否则选择方案(1)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 10003
using namespace std;
int jc[2003][N],n,m,p,mp[10003];
int prime[N],pd[N];
void init()
{
   for (int i=2;i<=10000;i++) {
   	if (!pd[i]) prime[++prime[0]]=i,mp[i]=prime[0];
   	for (int j=1;j<=prime[0];j++) {
   		 if (prime[j]*i>10000) break;
   		 pd[prime[j]*i]=1;
   		 if (i%prime[j]==0) break;
	   }
   }
   //cout<<prime[prime[0]]<<endl;
   for (int i=1;i<=prime[0];i++) {
   	 jc[i][0]=jc[i][1]=1;
   	 for (int j=2;j<=prime[i];j++) jc[i][j]=(jc[i][j-1]*j)%prime[i];
   }
}
int quickpow(int num,int x)
{
	int ans=1; int base=num%p;
	while (x) {
		if (x&1) ans=(ans*base)%p;
		x>>=1;
		base=(base*base)%p;
	}
	return ans;
}
int calc(int n,int m)
{
	if (m>n) return 0;
	return jc[mp[p]][n]*quickpow(jc[mp[p]][m]*jc[mp[p]][n-m]%p,p-2)%p;
}
int lucas(int n,int m)
{
	if (m==0) return 1;
	return calc(n%p,m%p)*lucas(n/p,m/p)%p;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	int T=0; init();
	while (~scanf("%d%d%d",&n,&m,&p)) {
		T++;
		int a=(lucas(n+1,m+1)-1+m+1)%p;
		int b=(lucas(n+1,m)+n-m)%p;
		//cout<<lucas(n+1,m+1)<<" "<<lucas(n+1,m)<<endl;
		//cout<<a<<" "<<b<<endl;
		printf("Case #%d: ",T);
		if (n/2>=m) printf("%d\n",b%p);
		else printf("%d\n",a%p);
	}
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值