Astar2016-Round2B 1003(杨辉三角,求大数组合)
来源:互联网 发布:思途cms源码 编辑:程序博客网 时间:2024/05/29 10:07
FROM:
2016"百度之星" - 初赛(Astar Round2B)
http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=702&pid=1003
Problem Description
有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第nn行第mm列的格子有几种方案,答案对10000000071000000007取模。http://acm.hdu.edu.cn/data/images/C702-1003-1.jpg
Input多组测试数据。
两个整数n,m(2\leq n,m\leq 100000)n,m(2≤n,m≤100000)
Output
一个整数表示答案
Sample Input
4 5
Sample Output
10
分析
(i,j)的值是(i,j-1)和(i-1,j)值的和。
先是采用dp的方法,但超时了,代码主体如下
int dp[2][SZ]; //采用2行数组,否则空间会超
for(i=2;i<=m;i++){dp[0][i]=1; dp[1][i]=i-1;}int k=2;int f=0;//dp[][0]while(++k<=n){for(i=m-1;i<=m;i++)dp[1-f][i]=(dp[1-f][i-1]+dp[f][i])%mod;f=1-f;}cout<<dp[f][m];
后来分析发现,这是杨辉三角,百度查了求杨辉三角某个位置值的方法,第n行的m个数可表示为 C(n-1,m-1) 。注意:这边的行列和题中不同。
补充组合知识:C(m,n)=C(m-1,n)+C(m-1,n-1)
http://blog.csdn.net/acdreamers/article/details/8037918 有很多组合相关题目解析
因为n,m较大,想了很久,没想到合适的求法,继续百度到了求解方法:http://www.xuebuyuan.com/1154396.html 采用了Lucas定理。赛后发现很多人也是采用了这个方法。
定理公式为:
Lucas(a,b,q)=C(a%q,b%q)*Lucas(a/p,b/p,p);
Lucas(a,0,q)=0;
代码(代码中lucas为第n行第m个组合数,如第5行第1个数为5. p必须为素数!!!)
#include <iostream>using namespace std;#define LL long long#define INF 0x3f3f3f3fconst int SZ=100002;const int p=1000000007;LL Pow(LL a,LL b,LL mod) { LL ans=1; while(b) { if(b&1) { b--; ans=(ans*a)%mod; } else { b/=2; a=(a*a)%mod; } } return ans; } LL C(LL n,LL m) { if(n<m) return 0; LL ans=1; for(int i=1;i<=m;i++) { ans=ans*(((n-m+i)%p)*Pow(i,p-2,p)%p)%p; } return ans; } LL Lucas(LL n,LL m) { if(m==0) return 1; return (Lucas(n/p,m/p)*C(n%p,m%p))%p; } int main(){ int i,j; int n,m; while(cin>>n>>m){ if(n<m) swap(n,m); //C n+m-4,m-2 int a=n+m-4,b=m-2; if(a-b<b) b=a-b; //C a,b LL ans=Lucas(a,b); cout<<ans<<endl; } return 0;}
我也尝试了不使用Lucas定理来求大数组合,结果是正确的,但没在oj中尝试。 对每次除法采用逆元。
#include <iostream>using namespace std;#define LL long longconst int mod=1000000007;LL mod_pow(LL x,LL n,LL mod) {LL res = 1; x%=mod; while(n>0){ if(n & 1){ res = res * x % mod; } x = x * x % mod; n>>=1; } return res; }int main(){int i,j;int n,m;while(cin>>n>>m){if(n<m)swap(n,m);//C n+m-4,m-2int a=n+m-4,b=m-2;if(a-b<b)b=a-b;LL ans=1; //要为LL型,否则乘积可能超出表示范围for(i=1;i<=b;i++){ans*=(a-i+1)%mod;ans%=mod; //不能少这句ans*=mod_pow(i,mod-2,mod)%mod;ans%=mod; //不能少这句//ans=ans*((a-i+1)%mod * mod_pow(i,mod-2,mod)%mod)%mod;}cout<<ans<<endl;}return 0;}
0 0
- Astar2016-Round2B 1003(杨辉三角,求大数组合)
- 利用杨辉三角求组合代码
- Round Numbers--杨辉三角,组合数学
- 组合数和杨辉三角
- 51nod1161【组合数学-杨辉三角】
- 组合——杨辉三角
- C语言 - 杨辉三角:求任意N阶杨辉三角
- 杨辉三角求二项式分布
- HDU 4927 Series 1(大数+杨辉三角)
- lucas定理求大数组合数取模
- UVa 10883 - Supermean (杨辉三角 通过取对数解决大数除大数)
- POJ-1850(组合公式+杨辉三角)
- poj 1850 code(杨辉三角+组合数)
- POJ 1850 &&POJ 1496 Code 杨辉三角+组合数
- SDUTOJ 1586 计算组合数 杨辉三角
- 二项式、杨辉三角、组合数三者之间关系
- 组合数取模(杨辉三角+Lucas定理+模合数)
- HDU:1799 循环多少次?(组合,杨辉三角)
- 在WinDBG中查看函数的反汇编代码的命令
- C++的程序的相关引导geekband
- blender手册快捷键和操作技巧六
- 2016"百度之星" - 初赛(Astar Round2B)解题报告
- 哈理工 2084 A+B(二进制加法)
- Astar2016-Round2B 1003(杨辉三角,求大数组合)
- Spring4 MVC Hibernate4集成 Annotation
- 高精度对数函数的实现
- Nginx源码安装
- 数据仓库之拉链算法
- Linux-用户和用户组管理
- 123
- socketClient
- next_day函数用法