与查询

有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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值