Title Link: codeforces 451E Devu and Flowers
The main topic: There are n flower beds. To choose the S-flower, each flower bed has f[i] branch flowers. The flower color of the same flower bed is the same, different flower beds of different color, ask how many kinds of combinations can be.
Problem Solving Ideas:2 n The State, enumeration says those flower beds are more than the flowers, the rest of the useC ( n? 1 sum+n? 1 )The partition method calculates the number. Note that the odd position is to be reduced. The even position is in addition. The principle of tolerance and repulsion.
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace STD;typedef Long Longll//ll N, M, p;ll Qpow (ll A, LL K, ll P) {ll ans =1; while(k) {if(k&1) ans = (ans * a)% P; A = (A * a)% P; K/=2; }returnAns;} ll C (ll A, ll B, ll P) {if(A < b)return 0;if(b > A) b = a A; ll up =1, down =1; for(LL i =0; I < b; i++) {up = up * (a-i)% p; Down = down * (i+1)% P; }returnUp * QPOW (down, p2, p)% p;//Inverse element}ll Lucas (ll A, ll B, ll P) {if(b = =0)return 1;returnC (a%p, b%p, p) * Lucas (a/p, b/p, p)% p;}Const intMAXN = -;Constll mod =1e9+7;intN;ll s, f[maxn];ll solve () {ll ans =0; for(inti =0; I < (1<<N); i++) {LL sign =1, sum = s; for(intj =0; J < N; J + +) {if(i& (1<<j)) {sum-= (f[j]+1); Sign *=-1; } }if(Sum <0)Continue; Ans + = sign * Lucas (SUM + N-1N1, MoD); Ans%= MoD; }return(ans + mod)% MoD;}intMain () {scanf("%d%lld", &n, &s); for(inti =0; I < n; i++)scanf("%lld", &f[i]);printf("%lld\n", solve ());return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Codeforces 451E Devu and Flowers (tolerant principle)