CodeForces - 690D2(卢卡斯)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40984919/article/details/81239892

在这个题中我们可以看出当给出n,m之后。输出的值为C(n+m) m - 1;

题意:给你n块砖,然后给你一面墙的宽度。问:你有多少种放法;

        那么,我们可以了解到,这道题和给你n个盒子,让你放一些小球,可不放。最后,总数小于等于m。是一个道理。

那么,我们可以得到上面一个组合公式是C(n+m) m  所以在这道题中就是C(n+m) m - 1;

#include<map>
#include<stack>
#include<bitset>
#include<math.h>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define fa ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
const int MAX_N=1000000+50;
const int INF=0x3f3f3f3f;

ll a[MAX_N],p =1e6+3 ;

//快速幂(带取余) 
ll qpow(ll x, ll m)
{
	if(m == 0) return 1;
	ll tmp = qpow(x, m / 2);
	tmp = (tmp * tmp) % p;
	if(m % 2 == 1) tmp = (tmp * x) % p;
	return tmp;
}

ll getc(ll n, ll m)
{
	if(m > n) return 0;
	return (a[n] * qpow(a[m],p-2)) % p * qpow(a[n-m],p-2) % p;
}

ll lucas(ll n,ll m)
{
	if(m == 0) return 1;
	return getc(n%p, m%p) * lucas(n/p,m/p) % p;
}

int main()
{
	ll n,m;
	cin>>n>>m;
	a[0] = 1;
	for(int i = 1; i <= p; i++) a[i] = (a[i-1] * i) % p ;
	cout<<lucas(n+m,n) - 1<<endl;
 	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40984919/article/details/81239892