cdoj 491 Tricks in Bits
//无脑爆居然能过!!!!!
解:其实正解也是暴力,但是可以证明在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
*/
cdoj 491 Tricks in Bits的更多相关文章
- UESTC 491 Tricks in Bits
Tricks in Bits Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit ...
- Advanced Configuration Tricks
Advanced Configuration Tricks Configuration of zend-mvc applications happens in several steps: Initi ...
- Codeforces Round #491 (Div. 2)
Codeforces Round #491 (Div. 2) https://codeforces.com/contest/991 A #include<bits/stdc++.h> us ...
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- HDU 5294 Tricks Device 网络流 最短路
Tricks Device 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 Description Innocent Wu follows D ...
- CDOJ 1324 卿学姐与公主(分块)
CDOJ 1324 卿学姐与公主(分块) 传送门: UESTC Online Judgehttp://acm.uestc.edu.cn/#/problem/show/1324 某日,百无聊赖的卿学姐打 ...
- CDOJ 1330 柱爷与远古法阵(高斯消元)
CDOJ 1330 柱爷与远古法阵(高斯消元) 柱爷与远古法阵 Time Limit: 125/125MS (Java/Others) Memory Limit: 240000/240000K ...
- 2015 Multi-University Training Contest 1 Tricks Device
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- CSS之清除浮动
一.清除浮动的目的. 1.当一个父元素的高度不写或为auto时,而且这个父元素内又有浮动的子元素,那么这时候该父元素的高度将不会自动适应子元素的高度,也可以说高度是0px; 有如下代码: <di ...
- Median of Two Sorted Arrays 解答
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- Longest Substring Without Repeating Characters 解答
Question Given a string, find the length of the longest substring without repeating characters. For ...
- C++ classics
common Business-Oriented LanguageBASIC(Beginner's All-purpose Symbolic Instruction Code)1972 C1983 C ...
- C语言使用正则表达式
http://blog.chinaunix.net/uid-479984-id-2114941.html C语言使用正则表达式 据说一个好的程序员是会使用DB和Regular Expression的程 ...
- JAVA常见异常集锦(持续更新)
No1:Nested in org.springframework.beans.factory.parsing.BeanDefinitionParsingException 2013-07-02 10 ...
- 使用 React 和 Flux 创建一个记事本应用
React,来自 Facebook,是一个用来创建用户界面的非常优秀的类库.唯一的问题是 React 不会关注于你的应用如何处理数据.大多数人把 React 当做 MV* 中的 V.所以,Facebo ...
- Flashback Version/Transaction Query
1.应用Flashback Version Query查询记修改版本 SQL> select dbms_flashback.get_system_change_number from dual; ...
- 【巧妙消维DP】【HDU2059】龟兔赛跑
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Android studio教程:[3]修改背景主题
android studio开发环境的背景主题是可以更改的,现在都流行黑色背景,这样让软件显得更高端大气的,更加赏心悦目,但最主要的还是看起来更舒适更顺眼.下面就教大家如何更改背景主题. 工具/原料 ...