题目描述:
Chino的数学很差,因此Cocoa非常担心。今天,Cocoa要教Chino解不定方程。
众所周知,不定方程的解有0个或者若干个。
给出方程:x1+x2+x3+…xm=n (m<=n)
Cocoa想知道这个不定方程的正整数解和非负整数解各有几个。
题目对Chino来说太难啦,你能帮一帮Chino吗?
输入描述:
两个正整数m, n。
输出描述:
题目要求的答案,即正整数解的个数和非负整数解的个数 。由于答案可能会很大,你只需要输出答案 mod(109 + 7) 即可.
代码1:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;
ll a[maxn];
ll powermod(ll a, ll b)
{
ll ans = 1;
a = a % mod;
while(b)
{
if(b&1) ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans % mod;
}
void jc()
{
a[0] = 1;
for(ll i = 1; i <= maxn; ++i)
a[i] = a[i - 1] * i % mod;
}
ll C(ll n,ll m)
{
ll ans=a[n]%mod*powermod(a[m],mod-2)%mod*powermod(a[n-m], mod-2)%mod; //组合数分解成n!/m!/(n-m)!
return ans;
}
int main()
{
ll m, n;
scanf("%lld%lld", &m, &n);
jc();
ll ans = C(n - 1, m - 1);
ll x = C(n + m - 1, m - 1);
cout << ans << " " << x << endl;
return 0;
}
代码2:
#include<iostream>
#include<cstdio>
using namespace std;
const int mod = 1e9+7;
typedef long long ll;
ll inv(ll x, ll p){
ll ans = 1;
while(p){
if(p & 1) ans = (ans*x)%mod;
x = x*x%mod;
p>>=1;
}
return ans;
}
ll C(ll n, ll k)
{
ll x = 1, y = 1;
for(int i = k,j = n; i >= 1; i--, j--){
x = (x*i)%mod;
y = (y*j)%mod;
}
return y*inv(x, mod-2)%mod; //y/x -> y*x的逆元
}
int main(){
ll n, m;
scanf("%lld%lld", &m, &n);
printf("%lld %lld\n", C(n-1,m-1 ), C(n+m-1,m-1));
return 0;
}