HDU 4149 Magic Potion

题意:

a[i] ^ x = f[i] ( i = 1...8 ) 和 ( a[1] + a[2] + ... + a[8] ) ^ x = f[9] 现在f为已知  求x

思路:

从低位到高位确定x值  做法见注释

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int f[20],t,ans;

int main()
{
    int i,k,j;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        f[0]=0; //用来存a[1]^a[2]^...^a[8]
        for(i=1;i<=8;i++)
        {
            scanf("%d",&f[i]);
            f[0]^=f[i];
        }
        scanf("%d",&f[9]);
        for(i=k=0;i<32;i++)
        {
            //printf("%d %d %d\n",i,ans,k);
            if(f[0]&(1<<i)) j=(k+1)%2; // k为进位 j为a[1]+a[2]+...+a[8]的第i位
            else j=k%2;
            if(f[9]&(1<<i)) //可以求出x的第i位是不是1
            {
                if(!j) ans|=(1<<i);
            }
            else
            {
                if(j) ans|=(1<<i);
            }
            for(j=1;j<=8;j++) //利用前8个等式和求出的x的第i位确定a的第i位
            {
                if(f[j]&(1<<i))
                {
                    if(ans&(1<<i));
                    else k++;
                }
                else
                {
                    if(ans&(1<<i)) k++;
                }
            }
            k/=2; //再次进位
        }
        printf("%d\n",ans);
    }
    return 0;
}

HDU 4149 Magic Potion,布布扣,bubuko.com

时间: 2024-10-09 23:10:31

HDU 4149 Magic Potion的相关文章

hdu 5125 magic balls(dp)

题目链接:hdu 5125 magic balls #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; int N, M, dp[maxn][maxn], A[maxn], B[maxn]; int vec[maxn][maxn], c[maxn]; void init () { scanf("%d%d&quo

HDU 2274 Magic WisKey

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2274 Magic WisKey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 568    Accepted Submission(s): 323 Problem Description On New Year Festival, Liu

HDU 5834 Magic boy Bi Luo with his excited tree

题目:Magic boy Bi Luo with his excited tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意:给一棵树,在树的每个结点可以收获一定的积分,每一条边会消耗一定的积分,每个结点积分取一次便完,但每次路过一条边,都会消耗积分,问从每一个结点出发,最多能捞多少积分.N范围10万,样例数1万. 思路: 不难的一道题,但细节较多,容易出错. 树形DP. 从i 点出发,大致可以分为向上 和 向下两种,先考虑向下的情

[搜索] hdu 4016 Magic Bitwise And Operation

主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=4016 Magic Bitwise And Operation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1315    Accepted Submission(s): 504 Problem Description Given n

hdu A Magic Lamp

http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2556    Accepted Submission(s): 999 Problem Description Kiki likes traveling. One day

hdu 4323 Magic Number( DP )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4323 Magic Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1728    Accepted Submission(s): 705 Problem Description There are many magic num

HDU 4648 Magic Pen 6

Magic Pen 6 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2057    Accepted Submission(s): 706 Problem Description In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a

hdu 4605 Magic Ball Game (在线主席树/离线树状数组)

hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球到达某个节点上,如果x==wi,那么球停在这个节点上 .当然,这个点是叶子节点也会停止 . 如果x<wi,那么有1/2的概率走向左子树,有1/2的概率走向右子树 . 如果x>wi,那么有1/8的概率走向左子树,有7/8的概率走向右子树 . 问球经过v节点的概率 .(停在v节点也算) 解法: 在线的话每一个节点建一棵根节点到该节点的线段树,离线的话就先

HDU 5125 magic balls(线段树+DP)

magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 323    Accepted Submission(s): 90 Problem Description The town of W has N people. Each person takes two magic balls A and B every day.