Topic link: http://codeforces.com/problemset/problem/467/B
The main idea of the topic: there are m + 1 player and n types Types of soldiers. Each player is given a number xi, and then xi is regarded as a binary number. It is stipulated that if the jth bit is 1, it means that this player has j type of soldiers. Fedor is the m + 1 player. Ask him the number of friends who can become friends with the previous m players. The condition for becoming friends is that the number of different soldiers of the two people being compared is not more than k.
Thinking:
Because different digits need to be counted. So think of exclusive OR, it can change the same part of If it is 0, the different parts become 1.
Then we can count the number of 1 again!
As for counting the number of 1, we can consider this number and 1 to perform a bitwise AND operation.
I used this feeling for the first time so amazing!
1 #include
2 #include <string.h>
3 #include <string>
4 #include
5 #include
6 #include
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)
23 scanf("%d", &a[i]);
24 scanf("%d", &a[m]);
25 int ans = 0;
26 for (int i = 0; i)
27 {
28 int tmp = a[i] ^< span style="color: #000000;"> 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 ", ans);
39 }
40 return 0;
41 }
1 #include
2 #include <string.h>
3 #include <string>
4 #include
5 #include
6 #include
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)
23 scanf("%d", &a[i]);
24 scanf("%d", &a[m]);
25 int ans = 0;
26 for (int i = 0; i)
27 {
28 int tmp = a[i] ^< span style="color: #000000;"> 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 ", ans);
39 }
40 return 0;
41 }