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 XORbitwise ORand bitwise AND, between any two successive integers and calculate the result. Your job is to
make the result as small as possible.

Input

The first line of the input is T (no
more than 1000),
which stands for the number of test cases you need to solve.

Then T blocks
follow. The first line of each block contains a single number N (1≤N≤100)
indicating the number of unsigned 64-bit
integers. Then n integers
follow in the next line.

Output

For every test case, you should output Case #k: first, where k indicates
the case number and counts from 1.
Then output the answer.

Sample Input

2

3

1 2 3

2

3 6

Sample Output

Case #1: 0

Case #2: 1

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;
typedef unsigned long long int LL;
int n;
LL ans;
LL MAX;
LL a[105];
LL min(LL a,LL b){return (a<b?a:b);}
void dfs(LL num,int cnt)
{
    if(ans==0)
        return;
    if(num==0)
    {
        ans=0;
        return;
    }
    if(cnt==n+1)
    {
        ans=min(ans,num);
        return;
    }
    dfs(num|(~a[cnt]),cnt+1);
    dfs(num&(~a[cnt]),cnt+1);
    dfs(num^(~a[cnt]),cnt+1);
    dfs(num|a[cnt],cnt+1);
    dfs(num&a[cnt],cnt+1);
    dfs(num^a[cnt],cnt+1);
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%llu",&a[i]);
        MAX=1;
		MAX<<=63;
		ans=MAX;
        dfs(a[1],2);
        dfs(~a[1],2);
        printf("Case #%d: %llu\n",++cas,ans);
    }
    return 0;
}
时间: 2024-10-10 02:51:49

UESTC 491 Tricks in Bits的相关文章

cdoj 491 Tricks in Bits

//无脑爆居然能过!!!!! 解:其实正解也是暴力,但是可以证明在n>6时答案一定为零. 第一步:对于任意两个数他们的二进制数要么有一半+的位是相同的,要么有一半+的位是不同的,于是首先使用与运算和且运算一定能使一半以上的位数变成0. 那么设第一步得到的数字为x,接下来我们对x与下一个数c做与运算,x上已经为0的位数一定仍为0. 对于剩下x中为1的位数,如果c中也为1的个数比较多,那么我们首先取反再做运算,如果c中为0的位数比较多那么直接做与运算,如此一定可使x中为1的位数中一半以上的位数变为0

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

2017 UESTC Training for Search Algorithm &amp; String

2017 UESTC Training for Search Algorithm & String A   next[]数组应用 题意:求一个字符串所有前缀出现的次数和. tags:  dp[i-1] = dp[next[i]] + 1. D   字符串next[]数组 题意:求给出字符串的最短循环节. tags:看了题解,原来next[]数组可以这样搞.. 思考KMP的next数组的定义:next[j] = i 代表 s[1...(i-1)] = s[j - i, j-1]. 并且i~j之间已

2017 UESTC Training for Dynamic Programming

2017 UESTC Training for Dynamic Programming A    思维, 或 dp, 很有意思 方法1: 构造法:蛇形安排赛程表算法复杂度:O(N^2)将1-N排成两竖列,每一轮同一行的为对手保持1的位置不变,其他位置按顺(逆)时方向依次旋转1    6          1    2          1    3          1    4          1    5      2    5          3    6          4   

2017 UESTC Training for Graph Theory

2017 UESTC Training for Graph Theory A       思维 题意:给你一个有n个点和m条边的无向连通图,每条边都有一个权值w.我们定义,对于一条路径,它的Charm value为该路径上所有边的权值的最大值与最小值的差.询问从1到n的所有路径的Charm value的最小值. tags:有点思维定式了..一条路径里只要最大最小值,所以边可以重复走.这样我们只要把边从小到大枚举,把第 i 条边作为最小边,然后对于每个 i ,我们按顺序逐一加入比它大的边,直到点

2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North America - Rocky Mountain 开始时貌似是UVAlive挂了,无论交什么都WA,后来转战HDU 这次水题比较多,其中B题据说有更加高级的方法. G题WA了两发,才发现竟然没有输出Case!!! 未完成:D F H I J A - Iterated Difference 水题,模拟迭代即可

【单调队列优化dp】uestc 594 我要长高

http://acm.uestc.edu.cn/#/problem/show/594 [AC] 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=5e4+2; 5 const int inf=0x3f3f3f3f; 6 int n,c; 7 int cur; 8 int dp[2][maxn]; 9 int q[maxn]; 10 int main() 11 { 1

分块基础练习 UESTC 1324

http://acm.uestc.edu.cn/#/problem/show/1324 思路:基础分块,这个是一个特别简单的分块,就当做是一个练习了.然后这题也是很简单的单点线段树更新. //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000") #def

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