HDU3944 DP? (LUCAS定理+阶乘预处理)
来源:互联网 发布:财务指标分析软件 编辑:程序博客网 时间:2024/05/16 09:26
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题目分析:要求计算最小的和,当 k < n/2 时需要将C(N,K) 化成 C(N,N-K);原式 C(N,K)+C(N-1,K-1)+...+C(N-K,0)+K;由 C(N-1,K)+C(N-1,K-1)=C(N,K) 原式可化为C(N+1,K)+K;然后经过Lucas定理+对阶乘的预处理即可得出结果;代码如下:#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 10005;bool prime[N];int p[N],f[N][N],inv[N][N],cnt,pth[N];void isprime(){ cnt=0; memset(prime,1,sizeof(prime)); for(int i=2;i<N;i++){ if(prime[i]){ p[++cnt]=i; pth[i]=cnt; for(int j=i+i;j<N;j+=i) prime[j]=0; } }}int quick_mod(int a,int b,int m){ int ans=1; a%=m; while(b){ if(b&1){ ans=ans*a%m; b--; } b>>=1; a=a*a%m; } return ans;}void init(){//预处理阶乘与逆元 int i,j; for(int i=1;i<=cnt;i++){ f[i][0]=inv[i][0]=1; for(int j=1;j<p[i];j++){ f[i][j]=(f[i][j-1]*j)%p[i]; inv[i][j]=quick_mod(f[i][j],p[i]-2,p[i]); } }}int com(int n,int m,int P){ if(m>n) return 0; if(m==n) return 1; int t=pth[P]; return f[t][n]*(inv[t][n-m]*inv[t][m]%P)%P;}int lucas(int n,int m,int P){ if(m==0) return 1; return com(n%P,m%P,P)*lucas(n/P,m/P,P)%P;}int main(){ int cas=1,n,m,P; isprime(); init(); while(cin>>n>>m>>P){ if(m<=n/2) m=n-m; n++; printf("Case #%d: %d\n",cas++,(m%P+lucas(n,m+1,P))%P); } return 0;}
0 0
- HDU3944 DP? (LUCAS定理+阶乘预处理)
- hdu3944 lucas定理+阶乘预处理
- hdu3944 DP? (lucas定理+预处理)
- hdu3944(Lucas定理+预处理)
- hdu3944(Lucas定理+预处理)
- HDU3944-DP?(帕斯卡公式+Lucas定理)
- HDU3944 DP?(大组合数取模:lucas定理)
- [HDU3944]DP? (组合数学Lucas定理)
- lucas定理+费马小定理方法求逆元-HDU3944
- HDU 3944-DP?(Lucas定理+预处理)
- 【HDU3944 】DP?【LUCAS定理+阶乘预处理】 【杨辉三角中的点(第n行m列),问你从(0,0)点走到该点经过的点最少的权值和(只能向下走或斜着走)。同时对素数p取余】
- A(HDU3944)---Lucas
- hdu 3944 DP? lucas定理
- Lucas+阶乘打表+费马小定理模板
- HDU DP?(Lucas + 帕森斯定理)
- hdu5794A Simple Chess(lucas定理+dp)
- HDU_5794_ASimpleChess(Lucas定理&&(容斥||dp))
- codeforces 560E Lucas定理+dp
- 中国剩余定理
- HDUJ 1250 Hat's Fibonacci
- I/O上的等待事件 —— control file sequential read
- A008: str函数和repr函数的区别
- PDF转换成Word转换器在线转换效果如何
- HDU3944 DP? (LUCAS定理+阶乘预处理)
- Maven系列1--settings.xml 配置详解
- ios 获取文件夹大小
- Android布局文件layout.xml的一些属性值
- 【水比例】#41 A. Guilty — to the kitchen!
- 【浏览器渲染原理】布局之布局处理
- 服务器编码笔记
- 选数
- 函数可变参数深入分析之va_list、va_start、va_arg、va_end