1. 程式人生 > >51nod1453(排列組合)

51nod1453(排列組合)

sed color alt 組合計數 pac .html opened log 編號

題目鏈接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1453

題意: 中文題誒~

思路: 因為最後一個球總是在編號比他大的球拿完之前拿完, 所以可以先把每種編號的求都拿出一個來, 按照 1, 2, .... n 排列. 然後再把 xi 個 i 號球插入這裏的 i 號球之前即可. 然後直接用排列組合計數即可.

代碼:

技術分享
 1 #include <iostream>
 2 #define ll long long
 3 using namespace std;
 4 
 5 const int mod = 1e9 + 7
; 6 const int MAXN = 1e3 + 10; 7 ll a[MAXN], vis[MAXN]; 8 9 ll get_pow(ll x, int n){ 10 ll ans = 1; 11 while(n){ 12 if(n & 1) ans = ans * x % mod; 13 x = x * x % mod; 14 n >>= 1; 15 } 16 return ans; 17 } 18 19 int main(void){ 20 vis[0] = 1; 21 for
(ll i = 1; i < MAXN; i++){ 22 vis[i] = vis[i - 1] * i % mod; 23 } 24 ll n, x; 25 cin >> n; 26 for(int i = 1; i <= n; i++){ 27 cin >> x; 28 a[i] = a[i - 1] + x; 29 } 30 ll cnt1 = 1, cnt2 = 1; 31 for(int i = 1; i <= n; i++){ 32 for
(ll j = a[i - 1] + 1; j <= a[i] - 1; j++){ 33 cnt1 = cnt1 * j % mod; 34 } 35 cnt2 = cnt2 * vis[a[i] - a[i - 1] - 1] % mod; 36 } 37 ll sol = cnt1 * get_pow(cnt2, mod - 2) % mod; 38 cout << sol << endl; 39 return 0; 40 }
View Code

51nod1453(排列組合)