Astar2016-Round2B 1003(杨辉三角,求大数组合)

本文探讨了一道经典的算法竞赛题目,通过分析发现其解法与杨辉三角及组合数学密切相关。介绍了如何利用Lucas定理高效求解大数值组合问题,并提供了两种实现方式的源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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 0x3f3f3f3f
const 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 long
const 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-2
		int 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值