[CSP-S Simulation Test]: Race (Mathematics + Trie tree)

Title Description

The annual Games began. There are two contestants $ N $, $ I $ a player of a capacity value (guaranteed $ A [i] $ pairwise disjoint), the game had conducted days. At $ J $ day ($ 0 \ leqslant j \ leqslant 2 ^ {m-1} $) game, the $ I $ a player score of $ A [i] \ xor \ j $, and from the large small ranking, the ranking of $ x $ ($ x $ $ $ 0 from the start) is obtained integral Alumni, you need to determine each student's final total points and $ q [i] $ ^ {10} $ mold 9 + 7 results $ $ p [i] $. In order to avoid the output file is too large, as long as you export $ p [i] $ XOR and can be.


Input Format

The first line of two integers, representing $ N, M $.
The next line N $ $ integer, representative of the number of $ I $ $ A [i] $.


Output Format

An integer that represents the answer.


Sample

Sample input:

3 2
0 1 2

Sample output:

8


Data range and tips

For $ 10 \% $ data, $ M \ leqslant 5 $.
For $ 30 \% $ data, $ N \ leqslant 100 $.
For $ 50 \% $ data, $ N \ leqslant 1,000 $.
For $ 100 \% $ data, $ N \ leqslant 200,000, M \ leqslant 30, A [i] <2 ^ m $.


answer

Consider how to obtain $ x $ answers. Square is equivalent to enumerate two people (can be the same), these two people came in the days of $ x $ in front of the answers included. So for $ x $, binary if we find $ f [i] $ value is the ability of the first $ i + 1 $ to $ M-1 $ and bit his equal and are different from the first position people have $ i $ number, then these people ahead of him whether the determined only by the first $ I $ bits, a total of $ 2 ^ {(M-1)} $ days, and does not require two people enumerated from these, since direct square It can be. As long as we enumerate $ I $ and $ j $, $ f [i] $ these people $ f [j] $ these people simultaneously discharge a few days before him is $ 2 ^ {(M-2)} $, so the $ 2 \ times f [i] \ times f [j] \ times 2 ^ {(M-2)} $ recognized answer. There are many ways to achieve specific, you can use $ trie $ tree implementation.

Time complexity: $ \ Theta (N) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,m;
int trie[10000000][2],cnt;
long long size[10000000];
long long ans;
void insert(int x)
{
	int p=0;
	for(int i=m-1;i>=0;i--)
	{
		int c=(x>>i)&1;
		if(!trie[p][c])trie[p][c]=++cnt;
		p=trie[p][c];
		size[p]++;
	}
}
void dfs(int x,long long sum,long long num)
{
	if(trie[x][0])dfs(trie[x][0],(sum+(size[trie[x][1]]*size[trie[x][1]]%1000000007*(1<<m-1)%1000000007+size[trie[x][1]]*num%1000000007*(1<<m-1)%1000000007)%1000000007)%1000000007,num+size[trie[x][1]]);
	if(trie[x][1])dfs(trie[x][1],(sum+(size[trie[x][0]]*size[trie[x][0]]%1000000007*(1<<m-1)%1000000007+size[trie[x][0]]*num%1000000007*(1<<m-1)%1000000007)%1000000007)%1000000007,num+size[trie[x][0]]);
	if(!trie[x][0]&&!trie[x][1])ans^=sum;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		int a;scanf("%d",&a);
		insert(a);
	}
	dfs(0,0,0);
	printf("%lld",ans);
	return 0;
}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11566898.html