hdu 3944

DP?

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


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
 



这题难度太大如果没有对LUCAS和组合数有很好的理解和扎实的基础,建议不要尝试对思维的要求太高

主要的思想就是通过组合数的杨辉三角用一个数和整个序列相加做到优化来消项从而简化计算;

开一个INT数组long long 存不下, 纯lucas模板会超时要打表,这个思想真的很重要,因为P为素数所以打一个10000以内的所有阶乘和阶乘与P的数组

最后还要注意模板中的变量要一一对应,理解每一个变量的含义

这个题的想法还是比较明确,要让值最小,那么必然就是要让1走的尽可能多,所以路线必然是尽可能的走最边上的1
分析一下:
显然第 n 行第 k 列的数就是组合数 C(n,k)  ,答案满足对称性。当k<=n/2的
情况。考虑从目的地往上走到顶点,因为组合数在k<=n/2是递增的,所以每次只要斜向上
走,到了最左端,再往上走就可以得到最小的和,所以最小的和为 
C(n,k)+C(n-1,k-1)+C(n-2,k-2)+……+C(n-k,0)+n-k;
用C(n-k+1,0)替换掉C(n-k,0)后,得到: 
C(n-k+1,0)+C(n-k+1,1)+C(n-k+2,2)+……+C(n-1,k-1)+C(n,k)+n-k 
对于组合数有C(n,k)=C(n-1,k-1)+C(n-1,k) 
所以最左边两个数相加得 C(n-k+2,1),继续与 C(n-k+2,2)相加得到 C(n-k+3,2),一直加下
去最后得到C(n+1,k)。 
所以最小的和为C(n+1,k)+n-k。 
当k>n/2时要从右边走思路一样
紧接着,就是计算C(n+1,k)对p去模了,这个需要用到lucas定理。
而且还要预处理


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define  N  10001
typedef long long LL;
int jie[N][N], luc[N][N], visit[N];
int lucas(int n,int m,int p);


int power(int m,int n,int p);
void init();


int main()
{
    int n, k, p;
    int ncase=1;
    init();
    while(scanf("%d %d %d",&n, &k, &p)!=EOF)
    {
        if(n/2>=k)
        {
            printf("Case #%d: %d\n",ncase++,(lucas(n+1,k,p)+(n-k))%p);
        }
        else
        {
            printf("Case #%d: %d\n",ncase++,(lucas(n+1,k+1,p)+k)%p);
        }
    }
    return 0;
}


void init()
{
    memset(visit,0,sizeof(visit));
    for(int i=2; i<=N; i++)
    {
        if(!visit[i])
        {
            for(int j=2*i; j<=N; j+=i)
            {
                visit[j]=1;
            }
            jie[i][0]=1, luc[i][0]=1;
            for(int j=1; j<i; j++)
            {
                jie[i][j]=jie[i][j-1]*j%i;
                luc[i][j]=power(jie[i][j],i-2,i);
            }
        }
    }
}


int power(int m,int n,int p)
{
    int r=1;
    while(n)
    {
        if(n&1)
        {
            r=r*m%p;
        }
        m=m*m%p;
        n/=2;
    }
    return r;
}




int lucas(int n,int m,int p)
{
    if(m==0)
    {
        return 1;
    }
    int ans=1;
    int a=n%p,b=m%p;
    if(b>a)
    {
        return 0;
    }
    ans=jie[p][a]*luc[p][a-b]%p*luc[p][b]%p;
    return lucas(n/p,m/p,p)*ans%p;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值