Advertisement
istinishat

meet in the middle & dp

Apr 21st, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://codeforces.com/problemset/problem/525/E
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define si(n) scanf("%d",&n)
  6. #define ll long long
  7. #define MAX 30
  8.  
  9. int a[MAX],n,K;
  10. ll S,fact[MAX],ans=0;
  11.  
  12. unordered_map<ll,int>M[30];
  13.  
  14. void go(int pos,int k,ll s,int limit,bool flag)
  15. {
  16.     //cout<<pos<<' '<<k<<' '<<s<<' '<<limit<<' '<<flag<<endl;
  17.     if(pos>limit){
  18.         if(flag){
  19.             for(int i=0;i<=K-k;i++)
  20.                 ans+=M[i][S-s];
  21.         }
  22.         else M[k][s]++;
  23.         return ;
  24.     }
  25.     if(a[pos]<19)go(pos+1,k+1,s+fact[a[pos]],limit,flag);
  26.     go(pos+1,k,s+a[pos],limit,flag);
  27.     go(pos+1,k,s,limit,flag);
  28. }
  29.  
  30. int main()
  31. {
  32.     //freopen("input.txt","r",stdin);
  33.     int i,j;
  34.     fact[0]=1;
  35.     for(i=1;i<=18;i++)fact[i]=fact[i-1]*i;
  36.  
  37.     si(n);si(K);scanf("%lld",&S);
  38.     for(i=1;i<=n;i++)si(a[i]);
  39.     go(1,0,0,n/2,0);
  40.     go(n/2 +1,0,0,n,1);
  41.     printf("%lld\n",ans);
  42.  
  43.     return 0;
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement