Anya and Cubes - CodeForces 525 E dp

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 ≤ 250 ≤ 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

题意:一共有n个数,你可以选择其中的一些数,然后最多使其中的k个数变成它的阶乘,问使其和为S共有多少种方案。

思路:dp思路很简单,但是如果是简单dp的话会TLE。解决方案是分两半,每一半做一个dp,由左一半的所有结果去找右一半的方案数。比如左边和为S2的方案数乘以右边和为S-S2的方案数,还要注意阶乘的使用次数不超过k。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
map<ll,ll> match[3][30];
map<ll,ll>::iterator iter;
int n,m;
ll S,jie[20],val[30];
void solve(int pos,int num,ll S2,ll k)
{
    if(S2+k<=S)
      match[pos][num][S2+k]+=match[pos][num][S2];
    if(num>0 && k<=18 && S2+jie[k]<=S)
      match[pos][num-1][S2+jie[k]]+=match[pos][num][S2];
}
void dp(int pos,int l,int r)
{
    int i,j,k,p;
    match[pos][m][0]=1;
    for(p=l;p<=r;p++)
       for(i=0;i<=m;i++)
          if(!match[pos][i].empty())
          {
              iter=match[pos][i].end();
              iter--;
              while(iter!=match[pos][i].begin())
              {
                  solve(pos,i,iter->first,val[p]);
                  iter--;
              }
              solve(pos,i,iter->first,val[p]);
          }
}
int main()
{
    int i,j,k;
    ll ans=0,ret,S2;
    jie[0]=1;
    for(i=1;i<=18;i++)
       jie[i]=jie[i-1]*i;
    scanf("%d%d%I64d",&n,&m,&S);
    for(i=1;i<=n;i++)
       scanf("%I64d",&val[i]);
    dp(1,1,n/2);
    dp(2,n/2+1,n);
    for(i=0;i<=m;i++)
       for(iter=match[1][i].begin();iter!=match[1][i].end();iter++)
       {
           S2=iter->first;
           ret=0;
           for(k=m;k>=m-i;k--)
              ret+=match[2][k][S-S2];
           ans+=iter->second *ret;
       }
    printf("%I64d\n",ans);
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值