51 nod 1406 与查询(思维)

题目来源:  CodeForces
基准时间限制:2 秒 空间限制:131072 KB 分值: 80  难度:5级算法题
 收藏
 关注

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值