首页 > ACM题库 > HDU-杭电 > HDU 4149-Magic Potion[解题报告]HOJ
2015
04-16

HDU 4149-Magic Potion[解题报告]HOJ

Magic Potion

问题描述 :

In a distant magic world, there is a powerful magician aswmtjdsj. One day,aswmtjdsj decide to teach his prentice ykwd to make some magic potions. The magic potion is made by 8 kinds of materials, what aswmtjdsj need to do is to tell ykwd how many each kind of materials is required. In order to prevent others from stealing these formulas, he decide to encrypt the formula. Assuming the amount of the eight kinds of materials are x1, x2, … x8, aswmtjdsj will use a number m to encrypt, and finally tell ykwd nine numbers:x1 xor m, x2 xor m ,…., x8 xor m, (x1 + x2 +…+ x8) xor m . ykwd is too lazy,however,to calculate the value of the number m, so he asks you to help him to find the number m.

输入:

The first line is an integer t, the number of test cases.
Each of the next t lines contains 9 integers, respectively, x1 xor m, x2 xor m ,…., x8 xor m, (x1 + x2 +…+ x8) xor m, each of the 9 numbers is less or equal to 231-1.

输出:

The first line is an integer t, the number of test cases.
Each of the next t lines contains 9 integers, respectively, x1 xor m, x2 xor m ,…., x8 xor m, (x1 + x2 +…+ x8) xor m, each of the 9 numbers is less or equal to 231-1.

样例输入:

2
1 2 3 4 5 6 7 8 36
5 5 5 5 5 5 5 5 123

样例输出:

0
11

Hint
The XOR operation takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result of each digit is 1 if the two bits are different, and 0 if they are the same. For example: 0101 (decimal 5) XOR 0011 (decimal 3) = 0110 (decimal 6)

题意:

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/houserabbit/article/details/36680535?utm_source=tuicool


  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。