DP? HDU - 3944

题目链接:点我


这里写图片描述

    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

题意:

求杨辉三角的从顶部开始到坐标为(n, m)的数的和的最小值模p.

思路:

Lucas定理, 高中数学都教过杨辉三角的一个性质,那就是莫一斜列的数的和等于最后那个数的那么那个数,即是: C(m,1) + C(m+1,2) +....+C(m+k,n) = C(m+k+1, n+1);
并且这个值一个是满足题意的最小的值,所以这题我们可以先沿边界走到某个地方之后,我们开始斜着走,使斜着走的自小,那么和也一定最小.那么我们需要沿边界走多少呢,出图书可以看书,当剩下的m (或者当n/2 大于 m 时,我们从右边界走,此时m = n - m)等于 n时,得到的值最小,那么我们只需要走沿边界走m步即可.

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;

typedef long long LL;
int pri[1240];
int M[10000+10];
bool vis[10000+10];
int f[1240][10000+10];
int num;

void prime(){
    memset(vis,false,sizeof(vis));
    num = 0;
    for(int i = 2;i<=10000; ++i){
        if(!vis[i]) pri[++num] = i;
        for(int j = 1;j<=num && i*pri[j]<=10000;++j){
            vis[i * pri[j]] = true;
            if(i % pri[j]==0) break;
        }
    }
    for(int i =1;i<=num;++i)
        M[pri[i]] =i;
}

void init(){//为了优化速度,我选择了先求出阶乘关于所有素数的模,方便后面计算.
    for(int j = 1;j< 1230;++j){
        int mod =M[pri[j]];
        f[mod][0] = 1;
    for(int i = 1;i <= 10000+2; ++i)
        f[mod][i] = f[mod][i-1] * i % pri[j];
    }
}

int qpow(int x, int n,int mod){
    int ans = 1;
    while(n){
        if(n&1) ans =ans *x % mod;
        x =x *x % mod;
        n >>= 1;
    }
    return ans;
}

int C(int n,int m,int mod){
    if(m>n) return 0;
    if(n==m) return 1;
    int k=M[mod];
    return (f[k][n]*qpow(f[k][m],mod-2,mod)% mod) * qpow(f[k][n-m],mod-2,mod)%mod;//利用费马小定理求出逆元,然后算除法取模.
}

int lucas(int n,int m,int mod){
    if(m==0) return 1;
   return C(n%mod,m%mod,mod)*lucas(n/mod,m/mod,mod);
}


int main(){
    int n,m,p;
    int kase = 0;
    prime();
    init();
    while(scanf("%d %d %d", &n, &m, &p) != EOF){
        if(m<=n/2)//此时是从右边界走
            m=n-m;
        int ans = (lucas(n+1 , m+1 ,p) %p +m % p) % p;
        printf("Case #%d: %d\n",++kase,ans );
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值