Anya loves to fold and stick. Today she decided to do just that.
Anya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has k stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.
Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most k exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers.
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.
2 2 30
4 3
1
2 2 7
4 3
1
3 1 1
1 1 1
6
In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.
In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.
In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.
tag:
binary search


转一下官方题解:
http://codeforces.ru/blog/entry/17119?locale=en
525E — Anya and Cubes
To solve this problem we need to use meet-in-the-middle. At first sort given array in increasing order and divide it in two parts. In first part must be first n / 2 elements, in second part — other.
Iterate all submasks of all masks of elements from first part. That is iterate which cubes from first part we take and on which from them we paste exclamation marks. In this way we iterated all possible sums, which we can get with cubes from first part. Let for current submask we get sum sum_lf and use tlf exclamation marks. To store all such sums we use associative arrays map < long long > cnt[k + 1], where k — count of exclamation marks which we have in the beginning.
After that similary iterate all submasks of all masks of elements from second part. Let for current submask sum is sumrg and number of used exclamation marks is trg. Then from first part we need to get sum (s - sumrg) and we can use only (k - trg) exclamation marks, where s — sum which we must get by condition of the problem. Then iterate how many exclamation marks we will use in first part (let it be variable cur) and increase answer on cnt[cur][s - sumrg]. To accelerate our programm we may increase answer only if cnt[cur].count(s - sumrg) = true.
For submasks in iterate we can cut off iteration on current sum for submask (it must be less or equal to given s) and on current count of exclamation marks (it must be less or equal to given k). Also we should not paste exclamation marks on cubecs with numbers larger than 18, because 19! more than 1016 — maximal value of s.
Asymptotic behavior of this solution — O(3((n + 1) / 2) * log(maxcnt) * k), where n — count of cubes, maxcnt — maximal size of associative array, k — count of exclamation marks.
题意:选一些数,某些可以是该数的阶乘(不超过k个),问和等于S的方案数
题解:折半,三进制状压(0表示不选,1表示选ai,2表示选ai!),map
加速: To accelerate our programm we may increase answer only if cnt[cur].count(s - sumrg) = true.
不加速会T,,T在test91
10487086 | 2015-03-27 13:58:06 | njczy2010 | E - Anya and Cubes | GNU C++ | Time limit exceeded on test 91 | 2000 ms | 33200 KB |
10487048 | 2015-03-27 13:54:28 | njczy2010 | E - Anya and Cubes | GNU C++ | Accepted | 858 ms | 3800 KB |
1 #include <cstdio> 2 #include <cstring> 3 #include <stack> 4 #include <vector> 5 #include <map> 6 #include <algorithm> 7 8 #define ll long long 9 int const N = 55; 10 int const M = 205; 11 int const inf = 1000000000; 12 ll const mod = 1000000007; 13 14 using namespace std; 15 16 int n,k; 17 ll s; 18 ll ans; 19 ll a[N]; 20 ll f[N]; 21 map<ll,ll> cnt[N]; 22 int L,R; 23 int totL,totR; 24 int b[N]; 25 26 void ini1() 27 { 28 ll i; 29 f[1]=1; 30 for(i=2;i<=20;i++){ 31 f[i]=f[i-1]*i; 32 } 33 } 34 35 int pw(int x) 36 { 37 int re=1; 38 for(int i=1;i<=x;i++){ 39 re*=3; 40 } 41 return re; 42 } 43 44 void ini() 45 { 46 int i; 47 ans=0; 48 for(i=0;i<n;i++){ 49 scanf("%I64d",&a[i]); 50 } 51 for(i=0;i<=k;i++){ 52 cnt[i].clear(); 53 } 54 L=n/2; 55 totL=pw(L); 56 R=n-L; 57 totR=pw(R); 58 } 59 60 void solve() 61 { 62 int o,j; 63 int te; 64 ll sum; 65 int cou; 66 int ff; 67 for(o=0;o<totL;o++){ 68 te=o; 69 for(j=0;j<L;j++){ 70 b[j]=te%3; 71 te/=3; 72 } 73 sum=0;cou=0; 74 ff=1; 75 for(j=0;j<L;j++){ 76 if(b[j]==0) continue; 77 else if(b[j]==1){ 78 sum+=a[j]; 79 } 80 else{ 81 if(a[j]>=19){ 82 ff=0;break; 83 } 84 else{ 85 sum+=f[ a[j] ]; 86 cou++; 87 } 88 } 89 if(sum>s){ 90 ff=0;break; 91 } 92 } 93 if(ff==0 || sum>s) continue; 94 cnt[ cou ][ sum ]++; 95 } 96 97 for(o=0;o<totR;o++){ 98 te=o; 99 for(j=0;j<R;j++){ 100 b[j]=te%3; 101 te/=3; 102 } 103 sum=0;cou=0; 104 ff=1; 105 for(j=0;j<R;j++){ 106 if(b[j]==0) continue; 107 else if(b[j]==1){ 108 sum+=a[j+L]; 109 } 110 else{ 111 if(a[j+L]>=19){ 112 ff=0;break; 113 } 114 else{ 115 sum+=f[ a[j+L] ]; 116 cou++; 117 } 118 } 119 if(sum>s){ 120 ff=0;break; 121 } 122 } 123 if(ff==0) continue; 124 int leftk=k-cou; 125 ll lefts=s-sum; 126 if(leftk<0) continue; 127 if(lefts<0) continue; 128 for(int x=0;x<=leftk;x++){ 129 //printf(" x=%d lefts=%I64d cnt=%d ",x,lefts,cnt[x][lefts]); 130 if(cnt[x].count(lefts)>0) 131 ans+=cnt[ x ][ lefts ]; 132 } 133 134 } 135 } 136 137 void out() 138 { 139 printf("%I64d ",ans); 140 } 141 142 int main() 143 { 144 ini1(); 145 //freopen("data.in","r",stdin); 146 // freopen("data.out","w",stdout); 147 //scanf("%d",&T); 148 //for(int cnt=1;cnt<=T;cnt++) 149 //while(T--) 150 while(scanf("%d%d%I64d",&n,&k,&s)!=EOF) 151 { 152 ini(); 153 solve(); 154 out(); 155 } 156 }