牛客网暑期ACM多校训练营7: C. Bit Compression(DFS+预处理)
题目描述
A binary string s of length N = 2n is given. You will perform the following operation n times :
- Choose one of the operators AND (&), OR (|) or XOR (^). Suppose the current string is S = s1s2...sk. Then, for all , replace s2i-1s2i with the result obtained by applying the operator to s2i-1 and s2i. For example, if we apply XOR to {1101} we get {01}.
After n operations, the string will have length 1.
There are 3n ways to choose the n operations in total. How many of these ways will give 1 as the only character of the final string.
输入描述:
The first line of input contains a single integer n (1 ≤ n ≤ 18).The next line of input contains a single binary string s (|s| = 2n). All characters of s are either 0 or 1.
输出描述:
Output a single integer, the answer to the problem.
输入
2 1001
输出
4
直接爆搜复杂度是O(18*3^18)的,不剪枝会超时
仔细分析可以发现每次递归都会使当前数组长度缩小一半,倒数第5次搜完后长度就变为16了
所以可以预处理2^16种情况下的答案,这样就可以使搜索的深度-3,复杂度变为O(16*2^16+18*3^14),可以过
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
char str[300005];
LL sum;
bool p[300005], P[19][300005];
int ans[66666], jud, er[21] = {1};
void Sech(int id, int len, int all)
{int i, j, now, cnt;if(id+3==len && len>=5){sum += ans[jud];return;}if(id==len+1){sum += p[1];return;}for(i=1;i<=3;i++){cnt = 0;for(j=1;j<=all;j++)P[id][j] = p[j], cnt += p[j];if(cnt==0)return;cnt = jud = 0;for(j=1;j<=all;j+=2){if(i==1) p[++cnt] = p[j]|p[j+1];if(i==2) p[++cnt] = p[j]&p[j+1];if(i==3) p[++cnt] = p[j]^p[j+1];jud = jud*2+p[cnt];}Sech(id+1, len, cnt);for(j=1;j<=all/2;j++)p[j] = P[id][j];}
}
int main(void)
{int n, len, i, j, now;for(i=1;i<=18;i++)er[i] = er[i-1]*2;scanf("%d", &n);len = min(er[n], 16);for(i=0;i<(1<<len);i++){for(j=0;j<=len-1;j++){p[j+1] = 0;if(i&(1<<j))p[j+1] = 1;}now = 0;for(j=1;j<=len;j++)now = now*2+p[j];sum = 0;Sech(1, min(n, 4), len);ans[now] = sum;}if(n<=4){now = 0;scanf("%s", str+1);for(i=1;str[i]!=0;i++)now = now*2+str[i]-'0';printf("%d\n", ans[now]);}else{sum = 0;scanf("%s", str+1);for(i=1;str[i]!=0;i++)p[i] = (str[i]-'0')==1;Sech(1, n, i-1);printf("%lld\n", sum);}
}
牛客网暑期ACM多校训练营7: C. Bit Compression(DFS+预处理)相关推荐
- 牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph
牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph 做法:\(G'\) 中的对应原图两条边(a,b) (c,d)的最短路为: \[ w[a][b] + w[c][d] ...
- 牛客网暑期ACM多校训练营(第九场)
牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...
- 牛客网暑期ACM多校训练营(第五场)
牛客网暑期ACM多校训练营(第五场) A. gpa 二分答案,然后就转化为是否满足 \(\frac {\sum s[i]c[i]}{\sum s[i]} ≥ D\), \(\sum s[i]c[i] ...
- 牛客网暑期ACM多校训练营(第三场)
牛客网暑期ACM多校训练营(第三场) A. PACM Team 01背包,输出方案,用bool存每种状态下用的哪一个物品,卡内存.官方题解上,说用char或者short就行了.还有一种做法是把用的物品 ...
- 牛客网暑期ACM多校训练营(第一场)
牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...
- 牛客网暑期ACM多校训练营(第十场)D Rikka with Prefix Sum
链接:https://www.nowcoder.com/acm/contest/148/D 来源:牛客网 题目描述 Prefix Sum is a useful trick in data struc ...
- 牛客网暑期ACM多校训练营(第三场)A.PACM Team(多重01背包)
链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 题目描述 Eddy was a contestant participating in ACM ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
最新文章
- jQuery设置样式 css
- Safari 不能播放Video ,Chrome等可以 问题解决。
- 浅谈C++ 标准库中的异常 —— stdexcept类
- Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案
- swift语言和python区别_Swift为什么能成为编程语言中的黑马?
- rabbitmq——镜像队列
- 计算机网络【4】传输层
- Hadoop HBase概念学习系列之hbase shell中执行java方法(高手必备)(二十五)
- 共享您的数据!…从SharePoint加载SQL数据
- Julia : Jupyter notebook 中试玩Julia
- 美通企业日报 | 阿迪发布星战系列球鞋;安进27亿美元收购百济神州20.5%股份
- 群体智能优化算法之粒子群优化算法
- 清华大学计算机直硕生,推免数据 | 清华大学计算机系三大巨变——直硕生减半,本校增多,211增多...
- ML-Agents案例之金字塔
- TRIZ创新方法——因果链分析
- FDTD Solutions时域有限差分法仿真学习相关操作(一)——GDS导出
- 题目0121-机器人走迷宫
- mysql 创建外部表_Greenplum
- 网页版简单点名系统的实现(HTML5、CSS、JS、jQuery)
- 美通社2022年9月最受关注新闻稿 | 星巴克、麦当劳、默沙东、宁德时代、腾讯音乐等发布重磅消息...