hdu 3944 DP? (Lucas定理)
来源:互联网 发布:微信网络投票公司 编辑:程序博客网 时间:2024/04/29 06:38
DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 3095 Accepted Submission(s): 970
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
Author
phyxnj@UESTC
Source
2011 Multi-University Training Contest 11 - Host by UESTC
Recommend
xubiao | We have carefully selected several similar problems for you: 3940 3941 3942 3943 3945
题解:Lucas定理
可能产生答案的只有两种走法
(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<cmath>#define N 10003using namespace std;int jc[2003][N],n,m,p,mp[10003];int prime[N],pd[N];void init(){ for (int i=2;i<=10000;i++) { if (!pd[i]) prime[++prime[0]]=i,mp[i]=prime[0]; for (int j=1;j<=prime[0];j++) { if (prime[j]*i>10000) break; pd[prime[j]*i]=1; if (i%prime[j]==0) break; } } //cout<<prime[prime[0]]<<endl; for (int i=1;i<=prime[0];i++) { jc[i][0]=jc[i][1]=1; for (int j=2;j<=prime[i];j++) jc[i][j]=(jc[i][j-1]*j)%prime[i]; }}int quickpow(int num,int x){int ans=1; int base=num%p;while (x) {if (x&1) ans=(ans*base)%p;x>>=1;base=(base*base)%p;}return ans;}int calc(int n,int m){if (m>n) return 0;return jc[mp[p]][n]*quickpow(jc[mp[p]][m]*jc[mp[p]][n-m]%p,p-2)%p;}int lucas(int n,int m){if (m==0) return 1;return calc(n%p,m%p)*lucas(n/p,m/p)%p;}int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);int T=0; init();while (~scanf("%d%d%d",&n,&m,&p)) {T++;int a=(lucas(n+1,m+1)-1+m+1)%p;int b=(lucas(n+1,m)+n-m)%p;//cout<<lucas(n+1,m+1)<<" "<<lucas(n+1,m)<<endl;//cout<<a<<" "<<b<<endl;printf("Case #%d: ",T);if (n/2>=m) printf("%d\n",b%p);else printf("%d\n",a%p);}}
0 0
- hdu 3944 DP? (Lucas定理)
- hdu 3944 DP? lucas定理
- HDU 3944 DP?(lucas定理 组合数)
- HDU 3944-DP?(Lucas定理+预处理)
- [HDU 3944] DP?组合数 Lucas定理
- HDU DP?(Lucas + 帕森斯定理)
- hdu 5794 A Simple Chess (dp+lucas定理)
- 多校第11场 HDU 3944 DP (lucas定理,大组合数取模)
- hdu3944 DP? (lucas定理+预处理)
- HDU_5794_ASimpleChess(Lucas定理&&(容斥||dp))
- [HDU3944]DP? (组合数学Lucas定理)
- 帕斯卡公式+Lucas定理______DP?( hdu 3944 )
- HDU/HDOJ 3944 DP? 多校联合11 电子科大 lucas定理
- HDU 3037 Saving Beans (Lucas定理)
- HDU 3037 Saving Beans(lucas定理)
- HDU 3037 Saving Beans (Lucas定理)
- HDU 3037:Saving Beans(Lucas定理)
- hdu-5446(中国剩余定理+lucas)
- Chrome跨域设置
- 【二次开发】将CATIA嵌入到Winform窗体中
- 如果我们有面值为1元、3元和5元的硬币若干枚,如何用最少的硬币凑够N元?(动态规划问题)
- pdf分销链接
- Flex DataGrid 添加超链接
- hdu 3944 DP? (Lucas定理)
- linux下pg数据库相关命令
- Linux下使用Nexus搭建Maven私服
- [译|Qt]Threads, Events and QObjects
- Go连接MySql数据库too many connections错误解决
- USACO 1.1 贪婪的送礼者Greedy Gift Givers
- 接口和抽象类
- java 蓝桥杯 填数字
- LeetCode 386. Lexicographical Numbers