Codeforces 451E Devu and Flowers (tolerant principle)

Source: Internet
Author: User

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&LT;&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.