John獲得了n罐糖果。不一樣的糖果罐,糖果的種類不一樣(即同一個糖果罐裏的糖果種類是相同的,不一樣的糖果罐裏的糖果的種類是不一樣的)。第i個糖果罐裏有 mi個糖果。John決定吃掉一些糖果,他想吃掉至少a個糖果,但不超過b個。問題是John 沒法肯定吃多少個糖果和每種糖果各吃幾個。有多少種方法能夠作這件事呢?
ios
3027: [Ceoi2004]Sweet
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 71 Solved: 34Description
John獲得了n罐糖果。不一樣的糖果罐,糖果的種類不一樣(即同一個糖果罐裏的糖果種類是相同的,不一樣的糖果罐裏的糖果的種類是不一樣的)。第i個糖果罐裏有 mi個糖果。John決定吃掉一些糖果,他想吃掉至少a個糖果,但不超過b個。問題是John 沒法肯定吃多少個糖果和每種糖果各吃幾個。有多少種方法能夠作這件事呢?
iosInput
從標準輸入讀入每罐糖果的數量,整數a到b
John可以選擇的吃掉糖果的方法數(知足以上條件)
ideOutput
把結果輸出到標準輸出(把答案模 2004 輸出) spa1<=N<=10,0<=a<=b<=10^7,0<=Mi<=10^6code
Sample Input
2 1 3
3
5
Sample Output
9
HINT
(1,0),(2,0),(3,0),(0,1),(0,2),(0,3),(1,1),(1,2),(2,1)
blogSource
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define Mod 2004 8 #define Maxm 10000010 9 #define LL long long 10 11 int w[15],n; 12 LL mul; 13 14 int get_c(int x,int y) 15 { 16 if(x<y) return 0; 17 LL mod=mul*Mod,ans=1; 18 for(int i=x;i>=x-y+1;i--) ans=1LL*ans*i%mod; 19 return (ans/mul)%Mod; 20 } 21 22 int cal(int x) 23 { 24 int ans=0; 25 for(int i=0;i<(1<<n);i++) 26 { 27 int ss=0,sm=x; 28 for(int j=1;j<=n;j++) if((1<<j-1)&i) 29 { 30 ss++;sm-=w[j]+1; 31 } 32 if(sm<0) continue; 33 if(ss&1) ans-=get_c(sm+n,n); 34 else ans+=get_c(sm+n,n); 35 ans%=Mod; 36 } 37 return ans; 38 } 39 40 int main() 41 { 42 int a,b; 43 scanf("%d%d%d",&n,&a,&b); 44 mul=1;for(int i=2;i<=n;i++) mul=mul*i; 45 for(int i=1;i<=n;i++) scanf("%d",&w[i]); 46 printf("%d\n",((cal(b)-cal(a-1))%Mod+Mod)%Mod); 47 return 0; 48 }
2017-04-25 21:25:39ip