有n个整数。输出他之中和x相与之后结果为x的有多少个。x从0到1,000,000
Input
第一行输入一个整数n。(1<=n<=1,000,000). 第二行有n个整数a[0],a[1],a[2],...a[n-1],以空格分开.(0<=a[i]<=1,000,000)
Output
对于每一组数据,输出1000001行,第i行对应和i相与结果是i的有多少个数字。
Input示例
3 2 3 3
Output示例
3 2 3 2 0 0 …… 后面还有很多0
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
const int MAXN = 1e6 + 1;
int read()
{
int x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
{
f = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void write(int a)
{
if (a < 0)
{
putchar('-');
a = -a;
}
if (a >= 10)
{
write(a / 10);
}
putchar(a % 10 + '0');
}
int cnt[MAXN + 10];
int main()
{
int n, maxVal, x;
memset(cnt, 0, sizeof(cnt));
n = read();
maxVal = 0;
for (int i = 0; i < n; i++)
{
x = read();
maxVal = max(x, maxVal);
cnt[x]++;
}
int MAX_DIG = 0;
int temp = maxVal;
while (temp >>= 1)
{
MAX_DIG++;
}
for (int j = MAX_DIG; j >= 0 ; j--)
{
for (int i = 1; i <= maxVal; i++)
{
if (i & (1 << j))
{
cnt[i - (1 << j)] += cnt[i];
}
}
}
cnt[0] = n;
for (int i = 0; i < MAXN; i++)
{
write(cnt[i]);
puts("");
}
return 0;
}