牛客多校第七场 C Bit Compression 思维
链接:https://www.nowcoder.com/acm/contest/145/C
来源:牛客网
- 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| = 2
n
). All characters of s are either 0 or 1.
输出描述:
Output a single integer, the answer to the problem.
输入例子:
2
1001
输出例子:
4
-->
说明
The sequences (XOR, OR), (XOR, AND), (OR, OR), (OR, AND) works. 题意:给你2^n个数,每次可以将前2^(n-1)个数和后2^(n-1)个数进行二进制与、或、异或操作中的一种,问有多少种不同的操作顺序使得最后的结果为一个一?
分析:考虑直接暴力的时间复杂度是3^18,这个数与题目给出的时间限制已经很接近,所以我们只需要在直接暴力的基础上做一点点优化就可以了
考虑如果最后要得到一个一,则数列中一定要还存在一,否则无论与、或、异或操作都无法再得到一
所以我们只需要在暴力的时候判断下剩下的数中是否还有一,没有一就没必要继续操作
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6 + 10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const double pi = acos(-1.0);
string s;
ll t[maxn*2]; //开始将数字存在1<<n后,每次数字合并后产生的1<<(n-1)往前面放,方便合并运算
ll res = 0;
void dfs( ll n ) {
if( n == -1 ) {
if( t[2] == 1 ) { //只剩一个一
res ++;
}
return ;
}
ll k = 1<<n;
for( ll j = 0; j <= 2; j ++ ) {
ll cnt = 0;
for( ll i = 1; i <= k; i ++ ) {
ll tmp = i+k;
if( j == 0 ) {
t[tmp] = t[tmp*2-1]^t[tmp*2];
} else if( j == 1 ) {
t[tmp] = t[tmp*2-1]|t[tmp*2];
} else {
t[tmp] = t[tmp*2-1]&t[tmp*2];
}
if( t[tmp] == 0 ) {
cnt ++;
}
}
if( cnt == k ) { //如果都是零就不可能再出现一
continue;
}
dfs(n-1);
}
}
int main() {
ll n;
cin >> n;
cin >> s;
ll k = 1<<n;
for( ll i = 1; i <= k; i ++ ) {
t[i+k] = s[i-1] - '0';
}
dfs(n-1);
cout << res << endl;
return 0;
}
牛客多校第七场 C Bit Compression 思维的更多相关文章
- Find the median(2019年牛客多校第七场E题+左闭右开线段树)
题目链接 传送门 题意 每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数. 思路 思路很好想,但是卡内存. 当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\). ...
- 2019牛客多校第七场E Find the median 权值线段树+离散化
Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...
- 两两内积为0(牛客多校第七场)-- CDMA
题意: 构造一个n*n的矩阵,元素只能是-1或1,任意两行内积为0(两两相乘加起来和为0). 思路: #define IOS ios_base::sync_with_stdio(0); cin.tie ...
- 2019牛客多校第七场H Pair 数位DP
题意:给你一个3个数A, B, C问有多少对pair(i, j),1 <= i <= A, 1 <= j <= B, i AND j > C或 i XOR j < ...
- 2019牛客多校第七场C-Governing sand(线段树+枚举)
Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...
- 最小表示法——牛客多校第七场A
脑瘫一样暴力,贪心找最小表示的串,判一个串是否是最小表示法时也是暴力地判.. 但是想不通复杂度是怎么算的.. #include<bits/stdc++.h> using namespace ...
- 牛客多校第七场 C Governing sand 线段树
题意: 有一个树林,树林中不同种类的树有不同的数量,高度,砍伐它们的价格.现在要求砍掉一些树,使得高度最高的树占剩下的树的总数的一半以上,求最小花费. 题解: 用线段树维护不同种类树的信息,叶子节点从 ...
- 2019牛客多校第七场E Find the median 离散化+线段树维护区间段
Find the median 题意 刚开始集合为空,有n次操作,每次操作往集合里面插入[L[i],R[i]]的值,问每次操作后中位数是多少 分析 由于n比较大,并且数可以达到1e9,我们无法通过权值 ...
- 牛客多校第七场H Pair 数位dp理解
Pair 题意 给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y& ...
随机推荐
- Scala的常用小技巧
1."RichString.java".stripSuffix(".java") == "RichString" "http:// ...
- 微信小程序中悬浮窗功能的实现(主要探讨和解决在原生组件上的拖动)
问题场景 所谓悬浮窗就是图中微信图标的按钮,采用fixed定位,可拖动和点击. 这算是一个比较常见的实现场景了. 为什么要用cover-view做悬浮窗?原生组件出来背锅了~ 最初我做悬浮窗用的不是c ...
- 100天搞定机器学习|Day15 朴素贝叶斯
Day15,开始学习朴素贝叶斯,先了解一下贝爷,以示敬意. 托马斯·贝叶斯 (Thomas Bayes),英国神学家.数学家.数理统计学家和哲学家,1702年出生于英国伦敦,做过神甫:1742年成为英 ...
- 又拍云叶靖:OpenResty 在又拍云存储中的应用
2019 年 7 月 6 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙·上海站,又拍云平台开发部负责人叶靖在活动上做了<OpenRest ...
- 【Java例题】5.5 映射类的使用
5.映射类的使用.使用HashMap保存英文-中文对照单词词典.单词词典可以增加和删除词汇.输入一个英文单词,翻译成中文并显示.输入一个中文单词,翻译成英文并显示. package chapter6; ...
- CodeForces 938E Max History 题解
参考自:https://blog.csdn.net/dreaming__ldx/article/details/84976834 https://blog.csdn.net/acterminate/a ...
- Selenium+Java - 结合sikuliX操作Flash网页
前言 前天被一个Flash的轮播图,给玩坏了,无法操作,后来请教了下crazy总拿到思路,今天实践了下,果然可以了,非常感谢! 模拟场景 打开百度地图 切换城市到北京 使用测距工具 测量 奥林匹克森林 ...
- Nightwatch——自动化测试(端对端e2e)
背景: 前端页面模拟仿真操作,目的是避免每次更新相关内容重复之前的测试操作,减少不必要的时间投入,以及校验功能的可用性.但是目前元素定位是个问题(每次页面有修改都要重设某些元素定位) 测试分类: 一. ...
- Navicat连接MYsql报错
在Windows中安装mysql8后,使用Navicat连接数据库是出现“ Client does not support authentication protocol requested by s ...
- 第一次Git使用以及码云(Gitee)
下载安装Git,官网下载地址https://git-scm.com/downloads,我用的是Win10版,下载好后一路默认安装,安装时会给你自动添加环境变量,完成后打开cmd,输入git --ve ...