cdoj 491 Tricks in Bits
2024-09-03 23:45:28
//无脑爆居然能过!!!!!
解:其实正解也是暴力,但是可以证明在n>6时答案一定为零。
第一步:对于任意两个数他们的二进制数要么有一半+的位是相同的,要么有一半+的位是不同的,于是首先使用与运算和异或运算一定能使一半以上的位数变成0。
那么设第一步得到的数字为x,接下来我们对x与下一个数c做与运算,x上已经为0的位数一定仍为0。
对于剩下x中为1的位数,如果c中也为1的个数比较多,那么我们首先取反再做运算,如果c中为0的位数比较多那么直接做与运算,如此一定可使x中为1的位数中一半以上的位数变为0
即每部都可以使1的个数减少一半以上,因此对于64位二进制数,最多只需要7步就可以让所以的位数全部变为0.
所以对于n>6直接输出0.对于n<=6,dfs求解
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string>
#define LL long long
#define ULL unsigned long long const int MAXN=;
const int MAXM=;
const int INF=;//careful because of floyed and so on
const int MOD=;
const unsigned long long UINF=; using namespace std; int n,T;
ULL a[];
ULL ans; void dfs(int t,ULL num){
if (t>n){
ans=min(ans,num);
return;
}
if (ans==) return;
if (num==){
ans=;
return;
}
dfs(t+,num&(a[t]));
dfs(t+,num&(~a[t]));
dfs(t+,num^(a[t]));
dfs(t+,num^(~a[t]));
dfs(t+,num|(a[t]));
dfs(t+,num|(~a[t]));
} int main(){
scanf("%d",&T);
for (int cas=;cas<=T;cas++){
scanf("%d",&n);
ans=UINF;
for (int i=;i<=n;i++) scanf("%llu",&a[i]);
if (n>){
ans=;
}
else{
dfs(,a[]);
dfs(,~a[]);
}
printf("Case #%d: %llu\n",cas,ans);
}
return ;
}
/*
2
3
1 2 3
2
3 6
*/
最新文章
- 关于URLEnCode,URLDeCode,Base64,公钥私钥
- hdu 1312 Red and Black
- C#获取HTML文件指定DIV内容
- 使用NPOI完成导出Excel文件
- CF 353C Find Maximum #205 (Div. 2)
- jquery取消事件冒泡和取消默认行为
- 573 The Snail(蜗牛)
- 第八十八节,html5+css3pc端固定布局,搜索区,插入大图,搜索框
- 准备着手学习python
- Oracle集合操作
- C#创建安装、卸载部署程序
- Windows使用tail命令跟踪日志
- PAT 1032 挖掘机技术哪家强
- Change tab position of PageControl to bottom
- 链表用途&;&;数组效率&;&;链表效率&;&;链表优缺点
- Codeforces Round #370 (Div. 2) D. Memory and Scores 动态规划
- Revit API创建标高,单位转换
- 典型案例收集-使用OpenVPN连通多个机房内网(iptables+静态路由)
- testng日志 ITestListener
- Mac下使用Parallels Desktop安装CentOS操作系统
热门文章
- 对发给Application.Handle消息的三次执行(拦截)消息的过程
- Linux下的命令行上网
- hibernate安装和配置和第一个Hibernate应用
- Median of Two Sorted Arrays 解答
- poj 2446 (二分匹配)
- SQL DCL数据控制语言,用来定义訪问权限和安全级别;
- Android 自动更新 + IIS7 添加APK mime
- 写一个Windows上的守护进程(1)开篇
- html5的本地存储localStorage和sessionStorage
- iframe自适应高度的多种方法方法小结(转)