Fedor and New Game (XOR operation)

Topic links: http://codeforces.com/problemset/problem/467/B

 

Title effect: player has m + 1 th and n types of soldiers. Each player is assigned a number xi, xi as binary number and then, if the predetermined j-th bit is 1, indicating that the player has this type j soldiers. Fedor is the m + 1-th player, asked him to talk to the front of the m number of players to become friends of. Become friends of the two conditions are different number of soldiers who are being compared not more than the k.

 

Ideas:

Because different number of bits to be counted. So thought XOR operation , which can be the same both portions becomes 0, 1 becomes different portions

Then we could count the number 1 just fine!

As if to count the number 1, we can consider this number 1 and bitwise AND operation.

 

For the first time with this magical feeling good!

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <string>
 4 #include <iostream>
 5 #include <stdlib.h>
 6 #include <algorithm>
 7 
 8 using namespace std;
 9 const int maxn = 1000 + 10;
10 int a[maxn];
11 
12 
13 
14 int main()
15 {
16 #ifndef ONLINE_JUDGE
17     freopen("../in.txt","r",stdin);
18 #endif
19     int n, m, k;
20     while (scanf("%d%d%d", &n, &m, &k) != EOF)
21     {
22         for (int i = 0; i < m; i++)
23             scanf("%d", &a[i]);
24         scanf("%d", &a[m]);
25         int ans = 0;
26         for (int i = 0; i < m; i++)
27         {
28             int tmp = a[i] ^ a[m];
29             int cnt = 0;
30             for (; tmp; tmp >>= 1)
31             {
32                 if (tmp & 1)
33                     cnt++;
34             }
35             if (cnt <= k)
36                 ans++;
37         }
38         printf("%d\n", ans);
39     }
40     return 0;
41 }

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/11210747.html