标签:
求n有顺序的划分为k个数的方案数.
显然这个就是一个组合公式,隔板法。可以把问题转化为x1+x2+…..xk = n 这个多元一次方程上。然后这个解就是C(n+k-1,k-1)
这道题n,k范围都是1e6。
我们可以预处理出阶乘,然后求对应的组合数,注意这里需要取Mod,用下逆元就好啦.
/*
#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:0x800000")
*/
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;
template< class T > T _abs(T n){
return (n < 0 ? -n : n);
}
template< class T > T _max(T a, T b){
return (!(a < b) ? a : b);
}
template< class T > T _min(T a, T b){
return (a < b ? a : b);
}
template< class T > T sq(T x){
return x * x;
}
template< class T > T gcd(T a, T b){
return (b != 0 ? gcd<T>(b, a%b) : a);
}
template< class T > T lcm(T a, T b){
return (a / gcd<T>(a, b) * b);
}
template< class T > bool inside(T a, T b, T c){
return a<=b && b<=c;
}
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define F(i, n) for(int (i)=0;(i)<(n);++(i))
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define repok(i, s, t, o) for(int (i)=(s);(i)<=(t) && (o);++(i))
#define MEM0(addr) memset((addr), 0, sizeof((addr)))
#define MP(x, y) make_pair(x, y)
#define REV(s, e) reverse(s, e)
#define SET(p) memset(pair, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) (int)c.size(
#define PB(x) push_back(x)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pii pair< int, int >
#define psi pair< string, int >
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define debug(x) cout << #x << " = " << x << endl
const double PI = acos(-1.0);
const int inf = 1<<30;
const int maxn = 2e6;
const int mod = 1000000007;
ll fac[maxn+5];
void init(){
fac[0] = 1;
rep(i,1,maxn) fac[i] = (fac[i-1] * i) % mod;
}
ll quickpow(ll m,ll n){
ll ans = 1;
m %= mod;
while(n){
if(n & 1)
ans = (ans * m) % mod;
n >>= 1;
m = (m * m)%mod;
}
return ans;
}
ll solve(ll a, ll b, ll p){
if(b > a) return 0;
return fac[a] * quickpow(fac[b]*fac[a-b],p-2) % p;
}
int main(){
//READ("in.txt");
init();
int t,kase = 1;
scanf("%d",&t);
while(t--){
int n,k;
scanf("%d%d",&n,&k);
printf("Case %d: %lld\n",kase++,solve(n+k-1,k-1,mod));
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
lightoj 1102 - Problem Makes Problem (组合+逆元)
标签:
原文地址:http://blog.csdn.net/notdeep__acm/article/details/48035913