codeforces 525E
给出n个数,k次操作机会,每次操作可以将一个数变成它的阶乘,一个数操作一次,问有多少种方法能够凑出S
#include
#include
#include
#include
#include
#define MAX 27
using namespace std;
typedef long long LL;
mapint > mp[MAX];
int n,k;
LL S,ans;
LL a[MAX];
bool fac ( LL num , LL& res )
{
res = 1LL;
while ( num )
{
res *= num;
if ( res > S ) return false;
num--;
}
return true;
}
void dfs1 ( int m , int t , LL sum )
{
if ( m == n/2 )
{
mp[t][sum]++;
return;
}
LL temp;
if ( fac(a[m], temp ))
if ( temp <= sum && t >= 1 )
dfs1 ( m-1 , t-1 , sum - temp );
if ( a[m] <= sum )
dfs1 ( m-1 , t , sum - a[m] );
dfs1 ( m-1 , t , sum );
}
void dfs2 ( int m , int t , LL sum )
{
if ( m == n/2+1 )
{
for ( int i = t ; i <= k ; i++ )
ans += mp[i][sum];
return;
}
LL temp;
if ( fac(a[m] , temp ) && t < k && sum + temp <= S )
dfs2 ( m+1 , t+1 , sum + temp );
if ( sum + a[m] <= S )
dfs2 ( m+1 , t , sum + a[m]);
dfs2 ( m+1 , t , sum );
}
int main ( )
{
while ( ~scanf ( "%d%d%I64d" , &n , &k , &S ))
{
ans = 0;
for ( int i = 0 ; i < MAX ; i++ )
mp[i].clear();
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%I64d" , &a[i] );
dfs1 ( n , k , S );
dfs2 ( 1 , 0 , 0 );
printf ( "%I64d\n" , ans );
}
}