【Henu ACM Round#18 E】Anya and Cubes
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
每个数字有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的更多相关文章
- 【Henu ACM Round#18 F】Arthur and Walls
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑,为什么一个连通块里面的空格没有变成一个矩形? 如果不是形成矩形的话. 肯定是因为某个2x2的单张方形里面. 只有一个角是墙.其 ...
- 【Henu ACM Round#18 D】Looksery Party
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 假设现在每个人收到的信息条数存在cnt里面 那个人猜的条数为target 则如果cnt[i]==target[i] 则我们就让第i个 ...
- 【Henu ACM Round#18 C】Ilya and Sticks
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用cnt[i]记录数字i出现的次数就好. 然后i从1e6逆序到1 如果cnt[i+1]和cnt[i]>0同时成立的话. 那么得 ...
- 【Henu ACM Round#18 B】Modulo Sum
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] m比较小 <=1000 a[i]直接看成a[i]%m就可以了. 有n个0..999之间的整数.. 如果有一个0那么就直接输出Y ...
- 【Henu ACM Round#18 A】 Multiplication Table
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 遍历i从1..n 看看x%i==0以及x/i<=n是否成立. [代码] #include <iostream> u ...
- 【Henu ACM Round#24 D】Iterated Linear Function
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把B提取出来就是一个等比数列了. 求和一下会发现是这种形式. \(B*\frac{(A^n-1)}{A-1}+A^n*x\) 则求一 ...
- 【Henu ACM Round#17 D】Hexagons!
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 题目的图吓人. 找下规律就会发现从内到外是1,6,12,18 即1,16,26,36... 即1+6(1+2+3+...) 等差求和 ...
- 【Henu ACM Round#14 D】Kefa and Dishes
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 状态压缩动规. 可以写成记忆化搜索的形式. f[bit][p] 表示选取的菜的情况为bit(用0..2^(N)-1的二进制形式表示各 ...
- 【Henu ACM Round#24 E】Connected Components
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 要求把连续的一段li..ri的边全都删掉. 然后求剩下的图的联通数 如果暴力的话 复杂度显然是O(k*m)级别的. 考虑我们把li. ...
随机推荐
- mysql-过程与函数
一.过程与函数简介 过程与函数是命名的PL/SQL块(也是用户的方案对象),被编译后存储在数据库中,以备执行.因此,其他PL/SQL块可以按名称来使用他们.所以可以将商业逻辑.企业规划写成函数或过程保 ...
- Android触碰事件
OnTouchListener使用 public class ViewActivity extends Activity implements View.OnTouchListener { @Over ...
- 怎样解除内容审查程序的password
如题:怎样解除内容审查程序的password 在不知道password的情况下.通过改动注冊表解决.点击"開始"→"执行",输入"regedit&qu ...
- emmet教程
https://www.zfanw.com/blog/zencoding-vim-tutorial-chinese.html https://www.zfanw.com/blog/zencoding- ...
- spark rdd saveAsTextFile保存为文件
sc.parallelize(["one", "two", "two", "three", "three&qu ...
- Find and counter
Find: In a sense, find is the opposite of the [] operator. Instead of taking an index and extracting ...
- 131.typename在嵌套类中的作用
#include <iostream> using namespace std; class myit { public: static int num; class itit { }; ...
- Metasploit的攻击实例讲解----ms10_046快捷方式图标漏洞
不多说,直接上干货! 准备工具 1.Kali linux 2016.2(Rolling)系统 IP: 192.168.1.103 2.受害者机子(windows XP系统) IP: 10.10 ...
- Orientdb基本操作
https://blog.csdn.net/clj198606061111/article/details/82314459
- Linux 运维笔试题(一)
试题: 1.说出下列服务对应的端口或者端口对应的服务 21 23 25 873 161 111 110 53 123 2049 2.文件atime,ctime,mtime的区 ...