HDU - 3944 DP? —— Lucas组合数,预处理,逆元

DP?

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


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 24 2 7
 

Sample Output
 
 
Case #1: 0Case #2: 5

题意:在杨辉三角中,给定n和m表示第n行的第m个数,问从0,0处走到这个节点的最小权值路程,走的时候要么竖直向下走,要么斜向右走

思路:

①先分两种情况讨论,n>=2*m和n<2*m(即当前的点在这一行的前一半还是后一半)

前者要一直斜着走到最左侧,价值和为C(n,m)+C(n-1,m-1)+....=C(n+1,m)+(n-m)

后者要一直竖直走到最上侧,价值和为C(n,m)+C(n-1,m)+C(n-2,m)+....=C(n+1,m+1)+m

都是由C(n,m)=C(n-1,m-1)+C(n-1,m)推出

②问题即转化为求这两个组合数的大小,给定的模数p是素数,用lucas定理,但是求C中的阶乘时暴力会T

③预处理一下所有的数的阶乘对每个素数的模和逆元,因为p是1e4所以可以存得下,求逆元用到了费马小定理

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#define max_ 100010
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
#define mod 9901
using namespace std;
ll n,m,p;
int casnum=1;
ll f[10010][10010];
ll ni[10010][10010];
bool vis[10010];
int prime[5000];
int pl=0;
void getprime()
{
	for(int i=2;i<=10000;i++)
	{
		if(vis[i]==false)
		prime[++pl]=i;
		for(int j=1;j<=pl&&i*prime[j]<=10000;j++)
		{
			vis[prime[j]*i]=true;
			if(i%prime[j]==0)
			break;
		}
	}
}
ll fpow(ll a,ll b,ll p)
{
	ll tmp=a;
	ll ans=1;
	while(b)
	{
		if(b&1)
		ans=(ans*tmp)%p;
		tmp=(tmp*tmp)%p;
		b/=2;
	}
	return ans;
}
void getf()
{
	for(int i=1;i<=pl;i++)
	{
		f[prime[i]][1]=ni[prime[i]][1]=1;
		for(int j=2;j<prime[i];j++)
		{
			f[prime[i]][j]=(f[prime[i]][j-1]*j)%prime[i];
			ni[prime[i]][j]=fpow(f[prime[i]][j],prime[i]-2,prime[i]);
		}
	}
}
ll C(ll n,ll m)
{
	if(m>n)
	return 0;
	if(m==n||m==0)
	return 1;
	return (f[p][n]*ni[p][n-m])%p*ni[p][m]%p;
}
ll lucas(ll n,ll m)
{
	if(m==0)
	return 1;
	return (C(n%p,m%p)*lucas(n/p,m/p))%p;
}
int main(int argc, char const *argv[]) {
	getprime();
	getf();
	while(scanf("%lld%lld%lld",&n,&m,&p)!=EOF)
	{
		if(n>=2*m)
		{
			printf("Case #%d: %lld\n",casnum++,(lucas(n+1,m)+(n-m))%p );
		}
		else
		{
			printf("Case #%d: %lld\n",casnum++,(lucas(n+1,m+1)+m)%p );
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值