链接:https://www.nowcoder.com/acm/contest/145/C
来源:牛客网

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| = 2

n

). All characters of s are either 0 or 1.

输出描述:

Output a single integer, the answer to the problem.

输入例子:
2
1001
输出例子:
4

-->

示例1

输入

复制

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 思维的更多相关文章

  1. Find the median(2019年牛客多校第七场E题+左闭右开线段树)

    题目链接 传送门 题意 每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数. 思路 思路很好想,但是卡内存. 当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\). ...

  2. 2019牛客多校第七场E Find the median 权值线段树+离散化

    Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...

  3. 两两内积为0(牛客多校第七场)-- CDMA

    题意: 构造一个n*n的矩阵,元素只能是-1或1,任意两行内积为0(两两相乘加起来和为0). 思路: #define IOS ios_base::sync_with_stdio(0); cin.tie ...

  4. 2019牛客多校第七场H Pair 数位DP

    题意:给你一个3个数A, B, C问有多少对pair(i, j),1 <= i <= A, 1 <= j <= B, i AND j > C或 i XOR j < ...

  5. 2019牛客多校第七场C-Governing sand(线段树+枚举)

    Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...

  6. 最小表示法——牛客多校第七场A

    脑瘫一样暴力,贪心找最小表示的串,判一个串是否是最小表示法时也是暴力地判.. 但是想不通复杂度是怎么算的.. #include<bits/stdc++.h> using namespace ...

  7. 牛客多校第七场 C Governing sand 线段树

    题意: 有一个树林,树林中不同种类的树有不同的数量,高度,砍伐它们的价格.现在要求砍掉一些树,使得高度最高的树占剩下的树的总数的一半以上,求最小花费. 题解: 用线段树维护不同种类树的信息,叶子节点从 ...

  8. 2019牛客多校第七场E Find the median 离散化+线段树维护区间段

    Find the median 题意 刚开始集合为空,有n次操作,每次操作往集合里面插入[L[i],R[i]]的值,问每次操作后中位数是多少 分析 由于n比较大,并且数可以达到1e9,我们无法通过权值 ...

  9. 牛客多校第七场H Pair 数位dp理解

    Pair 题意 给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y& ...

随机推荐

  1. 【Sublime】设置显示编码格式

    Mac 上的 Sublime 显示编码格式,设置方法: 右下角显示的 UTF-8 就是当前的编码格式. 添加如下代码: { "font_size": 18, // Display ...

  2. 第三章、Go-内建容器

    3.1.数组 (1)数组的定义 package main import ( "fmt" ) func main() { //用var定义数组可以不用赋初值 var arr1 [5] ...

  3. Linux下zookeeper下载与安装教程

    原文连接:(http://www.studyshare.cn/blog-front//blog/details/1169/0)一.下载 官网下载:点这里  百度网盘下载:点这里 官网下载图示: jav ...

  4. 后端开发实践系列之二——领域驱动设计(DDD)编码实践

    Martin Fowler在<企业应用架构模式>一书中写道: I found this(business logic) a curious term because there are f ...

  5. 关于FFT分析音频的学习

    本文部分知识从以下文章学习: https://zhuanlan.zhihu.com/p/19763358 傅里叶变换的知识 https://www.cnblogs.com/RabbitHu/p/FFT ...

  6. hive数仓客户端界面工具

    1.Hive的官网上介绍了三个可以在Windows中通过JDBC连接HiveServer2的图形界面工具,包括:SQuirrel SQL Client.Oracle SQL Developer以及Db ...

  7. FTP服务端部署

    FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...

  8. Hyper-V虚拟机上安装Ubuntu16.04/Ubuntu18.04.2LTS,搭建GitLab

    我的电脑系统是win10,内存8g如下 一开始是装的Ubuntu18.04.2LTS, gitlab-ce_12.1.3-ce.0_amd64.deb,每次能够安装成功,但是修改完ip后,运行gitl ...

  9. React躬行记(13)——React Router

    在网络工程中,路由能保证信息从源地址传输到正确地目的地址,避免在互联网中迷失方向.而前端应用中的路由,其功能与之类似,也是保证信息的准确性,只不过来源变成URL,目的地变成HTML页面. 在传统的前端 ...

  10. pythonday04数据类型(二)

    今日内容: 1.列表 2.元组 3.py2与py3的区别 4解释器/编译器 5.练习题 1.列表 想要表示多个”事物“,可以使用列表 users = ["李邵奇","奇航 ...