Codeforces Round #297 (Div. 2)

E. Anya and Cubes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 kstickers 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 kexclamation 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.

Input

The first line of the input contains three space-separated integers nk and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n1 ≤ 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

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.

Sample test(s)
input
2 2 30
4 3
output
1
input
2 2 7
4 3
output
1
input
3 1 1
1 1 1
output
6
Note

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.

思路:dp[i][j][s]表示前i个,用了j个感叹号,和为s的方案数,

这里发现s太大,不过我们可以用map加上滚动数组。

不过内存还是不够,处理方式是。。把数组分成两个部分,

先求出第一部分,加进答案,然后求第二部分,加进答案,还有查找和第一部分和起来等于s的

需要注意的是,dp[0][0]需要减去1,因为我们初始化了dp[0][0]=1;

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#include<queue>
#include<cstring>
#include<set>
#include<stack>
#include<string>
#include<ctime>
#define LL long long
#define maxn 4337
#define MAX 500010
#define INF 999
#define eps 1e-6
#define mod 1000000007
using namespace std;

LL s;
map<LL,LL>dp[26];
map<LL,LL>::iterator it;
int a[30] ;
LL get(int x)
{
    LL ans=1;
    for(int i=x;i>=1;i--)
    {
        ans *= i ;
        if(ans>s) return -1;
    }
    return ans;
}
LL p[100010][2],q[26][100010][2];
LL find(int x,LL y,int n )
{
   int  L , R , mid ;
   L = 0 ;R = n ;
   while(L<=R)
   {
       mid=(L+R)>>1;
       if(q[x][mid][0]==y) return q[x][mid][1] ;
       else  if(q[x][mid][0]<y) L=mid+1;
       else R= mid-1;
   }
   return 0 ;
}
int main()
{
     int i,n,m,j,k;
     int v ,u;
     while(scanf("%d%d%I64d",&n,&k,&s) != EOF)
     {
         for(int j=0;j<=k;j++)
              dp[j].clear();
         for( i = 1 ; i <= n ;i++)
            scanf("%d",&a[i]) ;
         dp[0][0]=1;
         for( i =1 ; i <= n/2 ;i++)
         {
             LL u=get(a[i]);
             for( j = k; j >= 0;j--)
             {
                 int len=0;
                 for( it=dp[j].begin();it != dp[j].end();it++)
                    p[len][0]=it->first,p[len++][1]=it->second;
                 for( v=0;v<len;v++)
                 {
                     if(p[v][0]+a[i]<=s)dp[j][p[v][0]+a[i]] += p[v][1];
                     if(u != -1&&p[v][0]+u<=s&&j<k){
                        dp[j+1][p[v][0]+u] += p[v][1];
                     }
                 }
             }
         }
         int len1[30]={0};
         LL ans=0;
         dp[0][0]--;
         for( j = k; j >= 0;j--)
             for( it=dp[j].begin();it != dp[j].end();it++){
                    q[j][len1[j]][0]=it->first,q[j][len1[j]++][1]=it->second;
                    if(it->first==s)ans += it->second;
            }
         for(int j=0;j<=k;j++)
              dp[j].clear();
         dp[0][0]=1;
         for( i =n/2+1 ; i <= n ;i++)
         {
             LL u=get(a[i]);
             for( j = k; j >= 0;j--)
             {
                 int len=0;
                 for( it=dp[j].begin();it != dp[j].end();it++)
                    p[len][0]=it->first,p[len++][1]=it->second;
                 for( v=0;v<len;v++)
                 {
                     if(p[v][0]+a[i]<=s)dp[j][p[v][0]+a[i]] += p[v][1];
                     if(u != -1&&p[v][0]+u<=s&&j<k){
                        dp[j+1][p[v][0]+u] += p[v][1];
                     }
                 }
             }
         }
         dp[0][0]--;
         for( j = k; j >= 0;j--)
             for( it=dp[j].begin();it != dp[j].end();it++)
             {
                 if(it->first==s)
                 {
                     ans += it->second;
                     continue;
                 }
                 for(v=0;v+j<=k;v++)
                 {
                     ans += it->second*find(v,s-it->first,len1[v]-1);
                 }
             }
        cout<<ans<<endl;
     }
     return 0;
}
View Code

 

posted @ 2015-03-27 16:45  _log__  阅读(129)  评论(0编辑  收藏  举报