【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

每个数字有3种选择。
1.选中它。
2.选中它且加阶乘符号
3.不选中它(即计算和的时候不考虑它)

如果我们直接暴力写的话复杂度是\(3^{25}\)

寻求优化。

我们可以用Meet-in-the-middle这个方法。

先求出1..n/2这些数字的组合方式。

用map<ll,ll> dic[25]来存它们的和。

dic[i][j]表示前n/2个数字中选了i个【2】状态的数字,和为j的方案数。

则我们再穷举n/2+1..n这些数字的选择情况。

得到了和sum以及【2】状态的数字个数cnt之后。

答案累加\(∑_0^{k-cnt}dic[i][m-sum]\)

这样复杂度就是\(O(3^{12})\)级别的了。

完全可以接受了

(n=1的时候,n/2等于0,如果你的dfs里面写的条件是now==n/2,应该把它改为now>=n/2,如果不这么改的话,dic[0][0]=1这个会漏掉,

然后右半边就可能会漏解了)

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std; const int N = 1e6;
const int M = 25;
const int MM = 200e4; int n,k,a[M+10],bo[M+10],tt;
ll pre[M+10],m,ans;
vector<ll>b[M+1]; int get_num(int cnt,ll x){
int l = 0,r = b[cnt].size()-1,temp1=-1,temp2=-1; while (l <= r){
int mid = (l+r)>>1;
if (x<=b[cnt][mid]){
temp1 = mid;
r = mid-1;
}else l = mid+1;
} l = 0,r = b[cnt].size()-1;
while (l <= r){
int mid = (l+r)>>1;
if (x>=b[cnt][mid]){
temp2 = mid;
l = mid+1;
}else r = mid-1;
} if (temp1==-1 || temp2 ==-1 || b[cnt][temp1]!=x) return 0;
else return temp2-temp1+1;
} void dfs(int now,int ope){
int st,en;
if (ope==1)
st = 1,en = n/2;
else
st = n/2+1,en = n;
if (now>=en+1){
ll sum = 0;
int cnt = 0;
for (int i = st;i <= en;i++){
if (bo[i]==0) continue;
if (bo[i]==2){
if (a[i]>=19) return;
sum+=pre[a[i]];
cnt++;
}else sum+=a[i];
}
if (sum > m) return;
if (ope==1){
b[cnt].push_back(sum);
}else{
for (int i = 0;i <= k-cnt;i++) {
ans+=get_num(i,m-sum);
}
}
return;
} bo[now] = 0;
dfs(now+1,ope); bo[now] = 1;
dfs(now+1,ope); bo[now] = 2;
dfs(now+1,ope);
} int main()
{
#ifdef LOCAL_DEFINE
freopen("rush_in.txt","r",stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
pre[0] = 1;
for (int i = 1; ;i++){
pre[i] = pre[i-1]*i;
if (pre[i]>1e16+10) break;
}
cin >> n >> k >> m;
for (int i = 1;i <= n;i++) cin >> a[i];
dfs(1,1);
for (int i = 0;i <= k;i++) sort(b[i].begin(),b[i].end());
dfs(n/2+1,2);
cout<<ans<<endl;
return 0;
}

【Henu ACM Round#18 E】Anya and Cubes的更多相关文章

  1. 【Henu ACM Round#18 F】Arthur and Walls

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑,为什么一个连通块里面的空格没有变成一个矩形? 如果不是形成矩形的话. 肯定是因为某个2x2的单张方形里面. 只有一个角是墙.其 ...

  2. 【Henu ACM Round#18 D】Looksery Party

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 假设现在每个人收到的信息条数存在cnt里面 那个人猜的条数为target 则如果cnt[i]==target[i] 则我们就让第i个 ...

  3. 【Henu ACM Round#18 C】Ilya and Sticks

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用cnt[i]记录数字i出现的次数就好. 然后i从1e6逆序到1 如果cnt[i+1]和cnt[i]>0同时成立的话. 那么得 ...

  4. 【Henu ACM Round#18 B】Modulo Sum

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] m比较小 <=1000 a[i]直接看成a[i]%m就可以了. 有n个0..999之间的整数.. 如果有一个0那么就直接输出Y ...

  5. 【Henu ACM Round#18 A】 Multiplication Table

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 遍历i从1..n 看看x%i==0以及x/i<=n是否成立. [代码] #include <iostream> u ...

  6. 【Henu ACM Round#24 D】Iterated Linear Function

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把B提取出来就是一个等比数列了. 求和一下会发现是这种形式. \(B*\frac{(A^n-1)}{A-1}+A^n*x\) 则求一 ...

  7. 【Henu ACM Round#17 D】Hexagons!

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 题目的图吓人. 找下规律就会发现从内到外是1,6,12,18 即1,16,26,36... 即1+6(1+2+3+...) 等差求和 ...

  8. 【Henu ACM Round#14 D】Kefa and Dishes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 状态压缩动规. 可以写成记忆化搜索的形式. f[bit][p] 表示选取的菜的情况为bit(用0..2^(N)-1的二进制形式表示各 ...

  9. 【Henu ACM Round#24 E】Connected Components

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 要求把连续的一段li..ri的边全都删掉. 然后求剩下的图的联通数 如果暴力的话 复杂度显然是O(k*m)级别的. 考虑我们把li. ...

随机推荐

  1. Pleasant sheep and big big wolf

    pid=3046">点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼.每一个格子仅仅能存在0或1仅仅动物.如今要用栅栏将全部的狼和羊分开.问怎么放,栅栏数放的最少,求出 ...

  2. html+css实现选项卡功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. PHP join() 函数

    PHP join() 函数 实例 把数组元素组合为一个字符串: <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo ...

  4. 各大CMS系统优缺点(2017)

    各大CMS系统优缺点(2017) 总结 WordPress之前用过,可能需要再完整的用一个才会比较了解. 从2015年各行业建站规模来看,还有一大批人想自己搭建网站,下面为大家盘点一下比较实用CMS系 ...

  5. windows电脑空间清理

    最近电脑空间又快满了,想下载一些好电影音频资源都要先临时清理一些文件才行,今天有时间就彻底整理一下,将整理过程及用到的好工具都记录一下,方面下次再遇到问题时可以很方面的参考执行. 1.分析磁盘空间占用 ...

  6. RecordAccumulator 1

    介绍 前面讲过producer会将数据保存在RecordAccumulator中,并通过Sender发送数据.RecordAccumulator 就相当于一个队列保存着那些准备发送到server的数据 ...

  7. ReactiveCocoa 中 RACSignal 所有变换操作底层实现分析(上)

    前言 在上篇文章中,详细分析了RACSignal是创建和订阅的详细过程.看到底层源码实现后,就能发现,ReactiveCocoa这个FRP的库,实现响应式(RP)是用Block闭包来实现的,而并不是用 ...

  8. Xrdp - 通过Windows的RDP连接Linux远程桌面(Ubuntu/CentOS/Redhat 7)(转载)

            您多久访问一次Linux桌面? 您使用什么工具来访问远程桌面? Xrdp是一个开源工具,允许用户通过Windows RDP访问Linux远程桌面. 除了Windows RDP之外,xr ...

  9. JavaScript知识复习

    JavaScript 数据类型 原始数据类型: number string boolean null undefined 对象:object: Function Array Date... 共有六种数 ...

  10. letCode(771 Jewels and Stones )

    问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...