[] Solution to a problem with the query [51nod1406]

[] Solution to a problem with the query [51nod1406]

Portal: query \ ([51nod1406] \)

Description [title]

Given \ (n-\) integers, to \ (X \ in [0,1000000] \) , are determined in this \ (n-\) integer with which \ (X \) after seeking the result is \ ( x \) is how many.

[Sample]

样例输入:
3
2 3 3

样例输出:
3
2
3
2
0
0
...
...
...
0
(一共1000001个数,后面一共999997个0)

【data range】

\(100\%\) \(1 \leqslant N \leqslant 10^6,\) \(1 \leqslant a[i] \leqslant 10^6\)

【analysis】

With \ (dp [i] \) represents \ (I \) requirements and equals \ (I \) of the counted number.

First, a number of requirements with its own, or equal to its own, so for, \ (DP [I] \) sequence is given an initial value of \ (I \) number of occurrences.

For a certain binary number \ (A \) , which is any one of \ (0 \) bit becomes \ (1 \) , to give a binary number \ (B \) .
Found:
with \ (A \) requirements and equals \ (A \) of the same number \ (B \) requirements and is not necessarily equal to \ (B \) ,
with \ (B \) requirements and equals \ (B \) of the same number \ (A \) requirements with a certain equal to \ (A \) .
That \ (b \) program must meet \ (a \) requirements.

So \ (DP \) equation: \ (DP [J] + DP = [J \ Oplus (<<. 1 I)] \) , where \ (J \ in [0,10 ^. 6], \) \ ( J \) \ (\ and (I <<. 1) == 0 \) and \ (j \ oplus (1 << i) \ leqslant 10 ^ 6 \)

Due to excessive output data, you need to write about to lose, otherwise the whole \ (TLE \) .

Time complexity is \ (O (MlogM) \) , where \ (M = 10. 6 ^ \) .

【Code】

#include<algorithm>
#include<cstdio>
#define Re register int
const int N=1e6;
int n,a,dp[N+3];
inline void in(Re &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
inline void out(int x){
    if(x>9)out(x/10);
    putchar(x%10+'0');
}
int main(){
    in(n);
    for(Re i=1;i<=n;i++)in(a),++dp[a];
    for(Re i=0;i<20;i++)
        for(Re j=0;j<=N;++j)
            if(!(j&(1<<i))&&(j^(1<<i))<=N)dp[j]+=dp[j^(1<<i)];
    for(Re j=0;j<=N;++j)out(dp[j]),puts("");
}

Guess you like

Origin www.cnblogs.com/Xing-Ling/p/11348359.html