hdu 3944 数学组合+帕斯卡定理
2017-05-12 23:52
204 查看
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)
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
(1)先走最外侧的斜边,然后竖直向下走到达c(n,m)
那么我们会走过m+1个1
然后还需要加入c(m+1,m)+c(m+2,m)+c(m+3,m)+…+c(n,m)
然后我先强行加入c(m+1,m+1),然后可以将c(m+1,m)和c(m+1,m+1)合并成c(m+2,m+1),然后不断向后合并,最终得到c(n+1,m+1),因为我们加入了c(m+1,m+1)=1所以最终的答案是c(n+1,m+1)+m
(2)先竖直向下,然后走斜边
那么我们会先走过n-m个1
然后还需要加入c(m,0)+c(m+1,1)+c(m+2,2)+…+c(n,m)
c(m,0)=c(m+1,0)替换,然后将c(m+1,0),c(m+1,1)合并得到c(m+2,1),然后不断向后合并最终得到c(n+1,m)
所以最终的答案是c(n+1,m)+n-m
然后观察后发现,单独的1的个数多的方案最终的结果会更优。
所以当n-m>m,选择方案(2),否则选择方案(1)
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int maxn=10005; int prime[maxn]; int C[maxn][maxn]; int A[maxn][maxn]; vector<int> v; int qpow(int a,int b,int m) { int ans=1; a%=m; while(b) { if(b&1){ ans=ans*a%m; } b>>=1; a=a*a%m; } return ans; } void init() { for(int i=2;i<maxn;i++) { if(!prime[i]) { v.push_back(i); for(int j=i*2;j<maxn;j+=i) { prime[j]=1; } } } int size=v.size(); for(int i=0;i<size;i++) { A[v[i]][0]=C[v[i]][0]=1; for(int j=1;j<v[i];j++) A[v[i]][j]=(A[v[i]][j-1]*j)%v[i]; C[v[i]][v[i]-1]=qpow(A[v[i]][v[i]-1],v[i]-2,v[i]); for(int j=v[i]-2;j>=0;j--) { C[v[i]][j]=(C[v[i]][j+1]*(j+1))%v[i]; } } } int com(int n,int m,int P) { return A[P] *(C[P][m]*C[P][n-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 n,m,p; int cas=1; init(); while(cin>>n>>m>>p) { if(2*m<n) printf("Case #%d: %d\n",cas++,(Lucas(n+1,m,p)+n-m)%p ); else printf("Case #%d: %d\n",cas++,(Lucas(n+1,m+1,p)+m)%p ); } }
相关文章推荐
- HDU 3944 组合数学+数论
- hdu 3944 DP? 组合数学与数论的结合
- hdu 3811 用状态压缩DP 解决看似组合数学的题目
- HDU 3944 DP?------Lucas 大组合数取余
- 组合数学第一发 hdu 2451 Simple Addition Expression
- hdu 4810 Wall Painting(组合数学)
- HDU 4810 Wall Painting(组合数学)
- hdu 1796 How many integers can you find(组合数学:容斥定理+dfs)
- HDU-4532 湫秋系列故事——安排座位 组合数学DP
- hdu 4045 Machine scheduling(组合数学)
- hdu2049(组合数学)
- HDU 4248【组合数学 + DP】
- hdu 2965组合数学题
- HDU 4048 Zhuge Liang's Stone Sentinel Maze [组合数学+Burnside]
- hdu 4790 数论 实现 组合数学
- HDU 4254 A Famous Game (概率&组合数学公式)
- hdu 1996 汉诺塔VI 组合数学
- HDU 4373 Mysterious For [组合数学]
- hdu 4602 Partition 数学(组合-隔板法)
- HDU 1027 组合数学