Magic Potion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 488    Accepted Submission(s): 287

Problem Description
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.

 
Input
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.
 
Output
For each test case you should output the value of m in a single line, you can assume that 0 <= m <= 231-1.
 
Sample Input
2
1 2 3 4 5 6 7 8 36
5 5 5 5 5 5 5 5 123
 
Sample Output
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)

 
刚开始接触这种题时, 一点头绪都没有,但是通过自己查资料,写几个简单的案例模拟一下过程,其实还是很容易理解的。
一下子没想出来没有关系,多试试总会有答案。
主要是弄清楚异或运算 其实和位运算有关系的,将数字转换为二进制自己试试,思路会清晰多。

//x << N: 左移N位就相当于原数乘以2的N次方; x >> N : 右移N位 就相当于原数除以2的N次方。
//x 异或 m,设 y = x << m, 即 y 就等于将x 左移(<<) m 位 ,这点很重要!
//设原来的数字为 xi 与 m 异或后 xi ^m = bi(1 <= i <= 9), (x1+x2+...+x8)^ m = b9 相当于(b1+b2+...+b8) = b9;
//因此 将(b1+b2+...+b8) 每一位与b9的每一位比较,若不相同, 即 意味着原数向左移了 j 位 ,将移动的位数相加即为 m的值
//很容易想到 若xi没有移位的话, (b1+b2+...+b8)^m == b9
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
  int t;
  int sum, m, tmp;
  cin >> t;
  while(t--)
  {
    int r[10];
    for(int i = 1; i <= 9; i++)
    {
      cin >> r[i];
    }
    sum = m = 0;
    for(int j = 0; j <= 31; j++)
    {
      tmp = 0;
      for(int k = 1; k <= 8; k++)
      {
        tmp += r[k]>>j&1; //(r[k] / 2^j) & 1, 与m异或后的八个数的和 从右至左 取出它的值,与第九个数的第j位比较
      }
      if((sum + tmp)%2 != (r[9]>>j&1))//若不同,将其转换为原数的第i位具有的值, 再求出它的进位值 ,并将m加上 (1左移相应的位数 j )
      {
        tmp = 8 - tmp;
        sum = (sum + tmp) / 2;
        m += 1 << j; 
      }
      else
      {
         sum = (sum + tmp) / 2;//若相同 将进位的用sum加上去,继续下一位
      }
    }
    cout << m << endl;
  }
  return 0;
}

hdu4149 Magic Potion的更多相关文章

  1. Gym 101981I - Magic Potion - [最大流][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem I]

    题目链接:http://codeforces.com/gym/101981/attachments There are n heroes and m monsters living in an isl ...

  2. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

  3. Gym101981I Magic Potion(最大流)

    Problem I. Magic Potion There are n heroes and m monsters living in an island. The monsters became v ...

  4. HDU 4149 Magic Potion

    意甲冠军: a[i] ^ x = f[i] ( i = 1...8 ) 和 ( a[1] + a[2] + ... + a[8] ) ^ x = f[9] 如今f为已知  求x 思路: 从低位到高位确 ...

  5. Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流

    题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...

  6. 2018 ACM/ICPC 南京 I题 Magic Potion

    题解:最大流板题:增加两个源点,一个汇点.第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1). 每个怪物连终点,边权为1: 参 ...

  7. 2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)

    http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只 ...

  8. Magic Potion(网络流)

    原题链接 2018南京的铜牌题,听说学长他们上来就A了,我这个图论选手也就上手做了做,结果一言难尽...... 发此篇博客希望自己能牢记自己的菜... 本题大意:有n个heros和m个monsters ...

  9. 2018icpc南京/gym101981 I Magic Potion

    题意: 若干个勇士,每个勇士只能杀特定的怪物.每个勇士只能杀1个怪,但是有一些药,喝了药之后能再杀一个,每个勇士只能喝一瓶药.问你最多杀多少怪. 题解: 按照如下建图套网络流板即可. 网上有题解说套D ...

随机推荐

  1. iOS开发中一些有用的小代码

    1.判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email {   NSString *emailRegex = @&q ...

  2. Java数据持久层框架 MyBatis之API学习七(动态 SQL详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  3. SpringMVC的filter怎么使用Autowired依赖注入bean

      有的时候根据我们业务的需要,我们需要在web项目中定义一个自己的filter,并想在这个filter中使用@Autowired注入bean供我们使用.如果直接使用的话是不行的,需要我们在xml文件 ...

  4. 2018年web前端学习路线图

    前端的的技术一直在变化,更新和变革,现在基本是三驾马车(vue,angualr,react)主导整个前端框架,但是无论对于新人或者有经验的程序员,这些知识在必须掌握 前端必会技能 上图罗列了整个前端的 ...

  5. linux_RAID

    什么是RAID? 磁盘阵列,把多个磁盘组合成一个磁盘组,在逻辑上看起来就是一块大的磁盘,提供单个物理磁盘的存储量和更高的存储性能,同时提供不同级别的冗余备份的一种技术,不同的RAID技术对应不同级别 ...

  6. JDK自带的缓存--包装类的缓存

    关于包装类和基本类型的文章我前面已经整理过了,这里大致罗列下重点.我们主要是想研究下包装类的缓存. 1,自动装箱和自动拆箱 java1.5之后就提供了自动装箱和自动拆箱的功能,当然我们也可以手动来装箱 ...

  7. Servlet--超链接,表单提交,重定向,转发4种情况的路径

    实际编码中我们经常写路径,写路径既可以写相对路径,也可以写绝对路径.我2年以前我就养成了习惯,只要是写路径我从来都是写绝对路径,因为万一将来我们的项目的目录发生变化,原来要是写相对路径的话就会有路径依 ...

  8. JAVA中限制接口流量、并发的方法

    JAVA中限制接口流量可以通过Guava的RateLimiter类或者JDK自带的Semaphore类来实现,两者有点类似,但是也有区别,要根据实际情况使用.简单来说, RateLimiter类是控制 ...

  9. TP手册学习第四内置天

    比较标签: eq:等于    heq:恒等于    gt:大于    lt:小于   (前面加上n则为否,如neq表示不等于)使用方法:{gt name="name" value= ...

  10. lodash源码分析之baseFindIndex中的运算符优先级

    我悟出权力本来就是不讲理的--蟑螂就是海米:也悟出要造反,内心必须强大到足以承受任何后果才行. --北岛<城门开> 本文为读 lodash 源码的第十篇,后续文章会更新到这个仓库中,欢迎 ...