HDOJ 3944 DP?

尽量沿着边走距离最短。化减后 C(n+1,k)+ n - k,

预处理阶乘,Lucas定理组合数取模

DP?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)

Total Submission(s): 1899    Accepted Submission(s): 633

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

[email protected]

Source

2011 Multi-University
Training Contest 11 - Host by UESTC

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

LL n,k,p;

LL fact[1300][11000];

LL QuickPow(LL x,LL t,LL m)
{
	if(t==0) return 1LL;
	LL e=x,ret=1LL;
	while(t)
	{
		if(t&1LL) ret=(ret*e)%m;
		e=(e*e)%m;
		t>>=1LL;
	}
	return ret%m;
}

int prime[2000],pr;
bool vis[10100];

void get_prime()
{
	for(int i=2;i<10100;i++)
	{
		if(vis[i]==false)
			prime[pr++]=i;
		for(int j=2*i;j<10100;j+=i)
			vis[j]=true;
	}
}

void get_fact()
{
	for(int i=0;i<1240;i++)
	{
		fact[i][0]=1LL;
		for(int j=1;j<=prime[i]+10;j++)
		{
			fact[i][j]=(fact[i][j-1]*j)%prime[i];
		}
	}
}

LL Lucas(LL n,LL m,LL p)
{
	LL ret=1LL;
	int id=lower_bound(prime,prime+pr,p)-prime;
	while(n&&m)
	{
		LL a=n%p,b=m%p;
		if(a<b) return 0;
		ret=(ret*fact[id][a]*QuickPow((fact[id][b]*fact[id][a-b])%p,p-2,p)%p)%p;
		n/=p; m/=p;
	}
	return ret%p;
}

int main()
{
	get_prime();
	get_fact();
	int cas=1;
	while(scanf("%I64d%I64d%I64d",&n,&k,&p)!=EOF)
	{
		if(k>n/2) k=n-k;
		LL ans=(Lucas(n+1,k,p)+n-k)%p;
		printf("Case #%d: %I64d\n",cas++,ans);
	}
	return 0;
}
时间: 2024-11-13 08:22:04

HDOJ 3944 DP?的相关文章

HDOJ 4349 DP?

尽量沿着边走距离最短,化减后 C(n+1,k)+ n - k, 预处理阶乘,Lucas定理组合数取模 DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others) Total Submission(s): 1899    Accepted Submission(s): 633 Problem Description Figure 1 shows the Yang Hui Tri

hdu 3944 dp?

DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804    Accepted Submission(s): 595 Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,-a

hdoj 1257 DP||贪心

最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38985    Accepted Submission(s): 15293 Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高

hdu 3944 DP? (Lucas 定理)

仔细观察杨辉三角后可以发现从最高点到第n行第k个数的最短路为c(n+1,k); 根据Lucas定理可以求出,一般来说要求答案模去一个质数p且p的范围不大于10^5则可用Lucas. Lucas(n,m,p)=cm(n%p,m%p)* Lucas(n/p,m/p,p) Lucas(x,0,p)=1; 另外注意当k>n/2时,必须令k=n-k. #include <iostream> #include <cstdio> #include <cstring> #incl

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

背包DP HDOJ 5410 CRB and His Birthday

题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == 1时,得到a[i] + b[i],若再买得到的是a[i],那么x == 1的情况用01背包思想,x > 1时就是在01的基础上的完全背包.背包dp没刷过专题,这么简单的题也做不出来:( /************************************************* Author

Hdoj 1520&amp;Poj2342 Anniversary party 【树形DP】

Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5917 Accepted Submission(s): 2692 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural

找规律/数位DP HDOJ 4722 Good Numbers

题目传送门 1 /* 2 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () 3 http://www.cnblogs.com/crazyapple/p/3315436.html 4 数位DP:http://blog.csdn.net/cyendra/article/details/11606209 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9

hdoj 5125 Little Zu Chongzhi&#39;s Triangles【状态压缩dp】

题目:hdoj 5125 Little Zu Chongzhi's Triangles 题意:给出n个木棍的长度,然后问这些木棍所能组成三角形的最大面积. 分析:这个题目用状态压缩解,因为木棍的最大个数为12 我们枚举所有状态,用状态对应位的 0 和 1 表示这个木棍是否选择,然后如果某个状态选择的木棍是3的话,判断是否可以组成,可以的话dp[st] = 三角形面积 然后大于三的,分解之后得出转移方程dp[st] = max(dp[st],dp[i] + dp[st - i]) (i | (st