Dog III VS detective headmaster THE MOVIE

Keywords: C++ Algorithm Dynamic Programming Math


Topic background
I'm the headmaster detective D ? ( X ? X ) \sf D?(X?X) D?(X?X), the supervisor of and childhood sweetheart O ? ? E \sf O??E O??E witnessed the scene of suspicious transactions in the black toilet when he went to the library. At that time, I was only looking at the transaction, but I didn't notice his accomplice approaching from behind. I was surprised by that D e v ? l \sf Dev?l Dev?l force the poison until I wake up! (switching sound quality)

I found my condom getting smaller! If they know D ? ( X ? X ) \sf D?(X?X) D?(X?X) being alive will not only endanger my life, but also endanger the people around me. stay A r e x t r e \sf Arextre At the suggestion of Dr. Arextre, I hid my true identity. When O ? ? E \sf O??E O?? When e asked my name, in a hurry, I changed my name to p r i n c i p l e \rm principle principle . In order to get information about those guys, I moved in. My father was a detective O ? ? E \sf O??E O??E house. I live there. But the uncle was a confused detective. I disguised as an uncle and used my natural reasoning ability to solve many difficult cases on the school board. Thanks to me, my uncle has become a more and more famous headmaster. You think I'm reporting information to him, but I'm him!

Now, I have become a middle school student, and I am forced to follow my classmates R a ? n y B u ? n y \sf Ra?nyBu?ny Ra?nyBu?ny, P ? L \sf P?L P?L, C r a ? ? e d \sf Cra??ed Cra??ed formed a Youth League. A famous detective whose body has become smaller, but his mind is still flexible and omniscient; Headmaster, there will always be only one!

Title Description
The last time the headmaster the doings It has completely aroused the hatred between the two races, and the anger is burning!

stay 100 100 After 100 years of fighting, both sides feel tired. "That's it. We'll win the first World War," said the headmaster. "Let's play a game, n o    g a m e    n o    l i f e \rm no\; game\; no\; life nogamenolife, the winner is the king! "

"Ha ha, I am I Q \rm IQ IQ up to 300 300 300 dog III, you can't beat me ", D ? G \sf D?G D?G is unwilling to show weakness, "what do you say, play?"

yes n n n rockfill, No i i i heap a i a_i ai# a stone. We can choose a pile of stones at a time and take one of them k    ( 1 ⩽ k ⩽ x ) k\;(1\leqslant k\leqslant x) k(1 ⩽ k ⩽ x) stones. If there are no stones when it's one's turn to operate, it will be judged negative.

With that, D ? ( X ? X ) \sf D?(X?X) D?(X?X) began the intense calculation. He has for all x ∈ [ 1 , n ] x\in[1,n] x ∈ [1,n], we all know whether the first hand will win or the second hand will win.

And you, in order not to be put in a condom, have to help D ? G \sf D?G D?G . Please tell it all x ∈ [ 1 , n ] x\in[1,n] Is x ∈ [1,n] the first or the second?

Data range and tips
1 ⩽ a i ⩽ n ⩽ 5 × 1 0 5 1\leqslant a_i\leqslant n\leqslant 5\times 10^5 1⩽ai​⩽n⩽5×105 .


according to s g sg sg theorem You know, just judge ⨁ i = 1 n [ a i   m o d   ( x + 1 ) ] \bigoplus_{i=1}^{n}[a_i\bmod(x+1)] ⨁ i=1n [ai mod(x+1)] Yes No 0 0 0 is enough.

If you're right about everyone a a a calculate your contributions separately and you're finished. But we know that the harmonic series is n ln ⁡ n n\ln n nlnn, so for each x x x enumeration ⌊ a x ⌋ \lfloor{a\over x}\rfloor ⌊ xa ⌋ is feasible!

If you want to enumerate, you can consider disassembly next. So it's statistics [ i x + ( t + 1 ) ⋅ 2 k , i x + t ⋅ 2 k + 1 ) [ix+(t+1)\cdot 2^{k},ix+t\cdot 2^{k+1}) [ix+(t+1) ⋅ 2k,ix+t ⋅ 2k+1). Obviously, this is similar to the suffix sum, that is
f k ( i ) = f k ( i + 2 k + 1 ) + ∑ [ i + 2 k ⩽ a < i + 2 k + 1 ] f_k(i)=f_k(i+2^{k+1})+\sum\big[i+2^{k}\leqslant a<i+2^{k+1}\big] fk​(i)=fk​(i+2k+1)+∑[i+2k⩽a<i+2k+1]

The latter contribution can be sorted directly by count c x = ∑ [ a i = x ] c_x=\sum[a_i=x] The prefix sum of cx = ∑ [ai = x] is obtained. So we O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn) can be obtained f f f array, and then O ( 1 ) \mathcal O(1) O(1) calculate in any section 2 k 2^k Number of occurrences of 2k.

Total complexity O ( n log ⁡ 2 n ) \mathcal O(n\log^2 n) O(nlog2n), if the constant is too ugly T L E \rm TLE TLE. My writing constant is very small and trustworthy.


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int_;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
inline int readint(){
	int a = 0, c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
inline void writeint(int x){
	if(x > 9) writeint(x/10);

const int MaxN = 500005;
const int LogN = 20;
bool c[MaxN], dp[LogN][MaxN];
short logtwo[MaxN];
bool query(int k,int l,int r){
	int t = (r-l)&((2<<k)-1);
	bool res = 0; t = r-t;
	res = dp[k][t]^dp[k][l];
	l = t+(1<<k); // latter half
	if(l <= r) res ^= c[r]^c[l-1];
	return res; // count

void printString(const char *str){
	for(; *str!='\0'; ++str)
int main(){
	int n = readint();
		logtwo[i] = logtwo[i>>1]+1;
	rep(i,1,n) c[readint()] ^= 1;
	rep(i,1,n) c[i] ^= c[i-1];
		int i = n, p = i+(1<<j)-1;
		int k = i+(2<<j); // bound
		for(; i>=0; --i,--p,--k)
			if(k <= n) dp[j][i] = dp[j][k]^c[k-1]^c[p];
			else if(p+1 <= n) dp[j][i] = c[n]^c[p];
	for(int x=2,now,l; x<=n+1; ++x){
		bool win = false, flag;
		for(int j=logtwo[n]; ~j; --j){
			int i = 0, p = i+x-1;
			int yu = (x-1)&((2<<j)-1);
			if(yu>>j){ // yu >= (1<<j)
				l = p-yu+(1<<j); // latter half
				flag = true; // must be considered
			else flag = false;
			bool now = 0; // bool is much faster!
			for(; p<n; i=p+1,p+=x,l+=x){
				/// L is %i and R is %p
				now ^= dp[j][p-yu]^dp[j][i];
				if(flag) now ^= c[p]^c[l-1];
			now ^= query(j,i,n);
			if(now){ win = true; break; }
		if(win) printString("Alice ");
		else printString("Bob ");
	return 0;

Posted by dolce on Tue, 05 Oct 2021 12:10:45 -0700