hdu 3944 DP? lucas定理

DP?

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


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
预处理出所有质数p,n!%p以及n!对于p的逆元,
然后
http://www.tuicool.com/articles/E3yaIn看这个博客吧。


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 10001
int check[maxn];
int jie[maxn][maxn];
int ni[maxn][maxn];
int cal(int n,int t,int p){
    int ans = 1;
    while(t){
        if(t&1) ans = ans*n%p;
        n = n*n%p;
        t/=2;
    }
    return ans;
}

void init(){
    memset(check,0,sizeof(check));
    for(int i = 2;i < maxn; i++){
        if(!check[i]){
            for(int j=i+i;j < maxn; j+=i)
                check[j] = 1;
            jie[i][0] = ni[i][0] = 1;
            for(int j = 1;j < i ;j++){
                jie[i][j] = jie[i][j-1]*j%i;
                ni[i][j] = cal(jie[i][j],i-2,i);
            }
        }
    }
}
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]*ni[p][a-b]%p*ni[p][b]%p;
    return Lucas(n/p,m/p,p)*ans%p;
}
int main(){
    init();
    int tt=1,n,m,p,ans;
    while(scanf("%d%d%d",&n,&m,&p)!=EOF){
        if(n/2>=m) ans = (Lucas(n+1,m,p)+n-m)%p;
        else ans = (Lucas(n+1,m+1,p)+m)%p;
        printf("Case #%d: %d\n",tt++,ans);
    }
    return 0;
}























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值