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求解

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<stack>
11 #include<string>
12 #define LL long long
13 #define ULL unsigned long long
14
15 const int MAXN=0;
16 const int MAXM=0;
17 const int INF=2000000000;//careful because of floyed and so on
18 const int MOD=1000000007;
19 const unsigned long long UINF=18000000000000000000;
20
21 using namespace std;
22
23 int n,T;
24 ULL a[107];
25 ULL ans;
26
27 void dfs(int t,ULL num){
28     if (t>n){
29             ans=min(ans,num);
30             return;
31     }
32     if (ans==0) return;
33     if (num==0){
34             ans=0;
35             return;
36     }
37     dfs(t+1,num&(a[t]));
38     dfs(t+1,num&(~a[t]));
39     dfs(t+1,num^(a[t]));
40     dfs(t+1,num^(~a[t]));
41     dfs(t+1,num|(a[t]));
42     dfs(t+1,num|(~a[t]));
43 }
44
45 int main(){
46     scanf("%d",&T);
47     for (int cas=1;cas<=T;cas++){
48             scanf("%d",&n);
49             ans=UINF;
50             for (int i=1;i<=n;i++) scanf("%llu",&a[i]);
51             if (n>6){
52                     ans=0;
53             }
54             else{
55                     dfs(2,a[1]);
56                     dfs(2,~a[1]);
57             }
58             printf("Case #%d: %llu\n",cas,ans);
59     }
60     return 0;
61 }
62 /*
63 2
64 3
65 1 2 3
66 2
67 3 6
68 */

时间: 2024-08-24 12:31:47

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 Status Description Given N unsigned 64-bit integers, you can bitwise NOT each or not. Then you need to add operations selected from bitwise XOR, bitwise O

Advanced Configuration Tricks

Advanced Configuration Tricks Configuration of zend-mvc applications happens in several steps: Initial configuration is passed to the Application instance and used to seed the ModuleManager and ServiceManager. In this tutorial, we will call this conf

[LeetCode]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

191. 求1的位数 Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

cdoj 1334 郭大侠与Rabi-Ribi 贪心+数据结构

郭大侠与Rabi-Ribi Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 最近郭大侠迷上了玩Rabi-Ribi这个游戏. Rabi-Ribi呢,是一个打兔子的动作冒险游戏,萌萌哒的兔子在地上跑来跑去,好萌好萌呀~ 这个游戏是这样玩的,郭大侠作为一个主角,拿着一个小锤子,他的目标是敲晕兔子,然后最后把这些敲晕的兔子都带回家. 当然咯,郭大侠想带回的兔

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

Java [Leetcode 190]Reverse Bits

题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 解题思路: 移位操作. 代码如下: publi

CDOJ 1273 God Qing&#39;s circuital law

暴力枚举+idea.做的时候mod写错了,写成了1000000009,找了两个多小时才发现...... a[1],a[2],a[3]....a[N] b[1],b[2],b[3]....b[N] 首先需要枚举b[1]...b[N]与a[1]进行组合. 然后对a[2]...a[N]从小到大排序 对b[1],b[2],b[3]....b[N] 除当前与a[1]组合的以外,剩下的从大到小排序 然后找出每一个a[i]在不破坏a[0]最大值的情况下最大能与哪一个b[i]配对. 然后从第N个人开始往第2个人