A(HDU3944)---Lucas

DP?

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


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


诶呀妈呀,这稍微难点的题目自己就是驾驭不了呀!!!

各种陷阱呀。在比赛的时候肯定A不了吧。。。

权当学习了。

Lucas就是个大组合数取模的算法,如果数据多了,你就得预处理,要不然就超时间,诶呀妈呀,这里面什么新算法都有。

什么求一段区间内所有的质数算法啦,

什么map存储着质数和个数的关系啦,

什么将所有的质数对应的factor(阶乘)求出来啦,

什么将所有的质数对应的阶乘的逆元求出来啦,

。。。。

而且,我TLE在了最初的思想,我根本就不知道怎么才能最简单的求出答案,看了别人的想法,

原来是C(N +1,M+1) + K,当然K要预处理 if (k > N / 2) k = N - k;

这种题目,自己就只能学习学习了,A是A不了了,...

贴出代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

int N, K, P;

int fac[1333][10011];

int prime[10011];

int flag[10011];

int re[1333][10011];

int map[10011];

int quick_pow(int a, int n, int p)
{
	int ans = 1;
	while (n)
	{
		if (n & 1)
		{
			ans = (long long int ) ans * a % p;
		}
		a = (long long int ) a * a % p;
		n >>= 1;
	}
	return ans;
}

void init()
{
	int cnt = 0;
	memset(flag, 0, sizeof(0));
	for (int i = 2; i < 10011; i++)
	{
		if (!flag[i])
		{
			prime[cnt++] = i;
			for (int j = i * i; j < 10011; j += i)
			{
				flag[j] = 1;
			}
		}
	}
	for (int i = 0; i < cnt; i++)
	{
		int k = prime[i];
		map[k] = i;
		fac[i][0] = 1; ;
		for (int j = 1; j <= k; j++)
		{
			fac[i][j] = (long long int )fac[i][j - 1] * j % k;
		}
	}
	for (int i = 0; i < cnt; i++)
	{
		int k = prime[i];
		for (int j = 0; j < k; j++)
		{
			re[i][j] = quick_pow(fac[i][j], k - 2, k);
		}
	}
}

int C(int n, int m)
{
	if (n < m)
	{
		return 0;
	}
	int x = map[P];
	return (long long int)fac[x][n] * re[x][m] * re[x][n - m]% P;
}

int lucas(int n, int m)
{
	if (m == 0) // 由于lucas(N, 0, P) == 1; 
	{
		return 1;
	}
	return C(n % P, m % P) * lucas(n / P, m / P) % P;
}

int main()
{
	int cnt = 1;
	init();
	while (scanf("%d%d%d", &N, &K, &P) != EOF)
	{
		int ans = 0;
		if (K <= N / 2)
		{
			K = N - K;
		}
		ans += K + lucas(N + 1, K + 1);
		printf("Case #%d: %d\n", cnt++, ans % P);
			
	}
//	system("pause");
	return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值