标签:write bit -- splay tom names 结果 typedef 方法
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1406
有n个整数。输出他之中和x相与之后结果为x的有多少个。x从0到1,000,000
http://blog.csdn.net/bahuia/article/details/55653495
还有一种方法。先将每个a和x分为前k位和后20-k位两部分,A0表示某个a的前k位,A1表示后20-k位,同理有X0与X1
设状态:
代码一:
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MS(a) memset(a,0,sizeof(a)) #define MP make_pair #define PB push_back const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; inline ll read(){ ll 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‘); } ////////////////////////////////////////////////////////////////////////// const int maxn = 1e6+1; int n,dp[maxn]; int main(){ n = read(); int mx = 0; for(int i=1; i<=n; i++){ int x = read(); dp[x]++; mx = max(mx,x); } int bit = 0; while(mx){ mx >>= 1; bit++; } for(int j=0; j<bit; j++){ for(int i=1; i<maxn; i++) if(i & (1<<j)) dp[i-(1<<j)] += dp[i]; } dp[0] = n; for(int i=0; i<maxn; i++){ write(dp[i]); puts(""); } return 0; }
代码二:
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MS(a) memset(a,0,sizeof(a)) #define MP make_pair #define PB push_back const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; inline ll read(){ ll 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‘); } ////////////////////////////////////////////////////////////////////////// const int maxn = 1e6+1; int n,dp[(1<<21)]; int main(){ n = read(); for(int i=1; i<=n; i++){ int x = read(); dp[x]++; } // for(int j=1; j<21; j++){ // for(int i=0; i<maxn; i++){ // if(i & (1<<(j-1))){ // dp[i] = dp[i][j-1]; // }else{ // dp[i][j] = dp[i][j-1]+dp[i+(1<<(j-1))][j-1]; // } // } // } // 滚动优化,否则RE for(int j=1; j<21; j++){ for(int i=0; i<maxn; i++){ if(i & (1<<(j-1))){ dp[i] = dp[i]; }else{ dp[i] = dp[i]+dp[i+(1<<(j-1))]; } } } for(int i=0; i<maxn; i++){ write(dp[i]); puts(""); } return 0; }
标签:write bit -- splay tom names 结果 typedef 方法
原文地址:http://www.cnblogs.com/yxg123123/p/7251206.html