有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
一个数 i 和一个数 x 与 等于 i 那么 i 一定是 x 的子集 处理方法类似于背包 从大到小枚举避免冲突
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL ;
const int N = 1e6+10;
const int inf = 2147483647;
const LL mod = 1e9+7;
int dp[N];
int read()
{
int x=0,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 x)
{
int a1=0,a[20];
a[1]=0;
if (!x) a1++;
while (x)
{
a[++a1]=x%10;x/=10;
}
for (int i=a1;i>=1;i--)
putchar(a[i]+'0');
puts("");
}
int main()
{
int n,x;
scanf("%d", &n);
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++) x=read(),dp[x]++;
for(int j=20; j>=0; j--)
for(int i=0; i<=1000000; i++)
if(i&(1<<j)) dp[i^(1<<j)]+=dp[i];
for(int i=0;i<=1000000;i++) write(dp[i]);
return 0;
}