codeforces 525E (29/600)

本文介绍了一种解决特定计数问题的方法,通过将问题分解并使用递归深度优先搜索结合记忆化来寻找所有可能的方式,使得选定的数字组合在经过特定操作后的总和等于给定的目标值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Input
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
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.

Example
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.

复习了一下折半搜索…
分成两部分用二分找另一半是否存在…
甚至可以简化成用map

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
ll n, k, s;
map<long long, long long>q[30];
ll tu[30],jc[30];
struct pp
{
    long long x, y;
};
pp qw[1100000];
long long jj = 0;
void dfs1(long long ml, long long bb, long long he,long long jx)
{
    if (bb > k || he > s)return;
    if (ml == jx + 1)
    {
        qw[++jj].x = bb;
        qw[jj].y = he;
        return;
    }
    dfs1(ml + 1, bb, he + tu[ml],jx);
    dfs1(ml + 1, bb, he ,jx);
    if (tu[ml] <= 18)
    {
        dfs1(ml + 1, bb + 1, he + jc[tu[ml]], jx);
    }
}
void dfs2(long long ml, long long bb, long long he, long long jx)
{
    if (bb > k || he > s)return;
    if (ml == jx + 1)
    {
        q[bb][he]++;
        return;
    }
    dfs2(ml + 1, bb, he, jx);
    dfs2(ml + 1, bb, he + tu[ml], jx);
    if (tu[ml] <= 18)
    {
        dfs2(ml + 1, bb + 1, he + jc[tu[ml]], jx);
    }
}
int main()
{
    cin >> n >> k >> s;
    for (long long a = 1; a <= n; a++)cin >> tu[a];
    jc[0] = 1;
    for (long long a = 1; a <= 18; a++)jc[a] = jc[a - 1] * a;
    long long mid = (n) / 2;
    dfs1(1, 0, 0, mid);
    dfs2(mid + 1, 0, 0, n);
    long long daan = 0;
    for (long long a = 1; a <= jj; a++)
    {
        for (long long b = 0; b <= k - qw[a].x; b ++)
        {
            daan += q[b][s - qw[a].y];
        }
    }
    cout << daan;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值