HDU4149 Magic Potion (bitwise operation)

Source: Internet
Author: User

Please poke here.

Test instructions: Give the result of x1,x2,x3,x4,x5,x6,x7,x8 and a value m, and then give the sum of x1~x8 and M for the difference or operation, and ask M.

Ideas:

According to the bit operation characteristic, because m maximum is 32 bits, we can infer from the lowest bit sequentially, and use x>>i&1 to take the value of the right number i-1 bit.

Assuming that the first digit of the binary right is 1, the first binary result of all Xi's right start is calculated, and the cumulative TMP is compared with the most right bit of TMP and SUM, and the right of M is 1, otherwise 0;

So x1~x8 's binary most right-hand side has been calculated, and the summation of the rounding and so on.


It's an interesting question ~


Code

/**author:flint_x *created time:2015-07-20 14:19:04 *file name:whust1_I.cpp * * #include <iostream> #include <ss tream> #include <fstream> #include <vector> #include <list> #include <deque> #include < queue> #include <stack> #include <map> #include <set> #include <bitset> #include < algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype># include<cmath> #include <ctime> #include <iomanip>using namespace Std;const double eps (1e-8); typedef  Long long lint; #define CLS (a) memset (A,0,sizeof (a)) #define RISE (I,A,B) for (int i = A; I <= b; i++) #define Fall (I,A,B) for (int i = A; I >= b; i--) int main () {//Freopen ("Input.txt", "R", stdin);//Freopen ("Output.txt", "w", stdout); int T;cin >> t;rise (t,1,t) {int r[10];rise (i,1,9) cin >> R[i];int sum = 0, M = 0;rise (i,0,31) {int tmp = 0;rise (j,1, 8) {tmp + = r[j]>>i&1;} if (sum + tmp)% 2! = (r[9]>>i&1)) {//X&GT;&GT;i&1 equivalent to 1<<i&x tmp = 8-tmp;sum = (sum + tmp)/2;m + = 1<<i;} else sum = (sum + tmp)/2;}    cout << m << Endl;}      return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU4149 Magic Potion (bitwise operation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.