CF1725H Hot Black Hot White

Luogu 链接
CodeForces 链接
Virtual Judge 链接

题意

题目描述

给定一个正偶数 \(n\)\(n\) 个正整数 \(a_1,a_2,\dots,a_{n-1},a_n\),要求把它们分为大小都为 \(\frac{n}{2}\) 的两组,使得它们满足下述要求:

  • 对于任意不同组的 \(a_i\)\(a_j\),都有 \(\operatorname{concat}(a_i,a_j)\times \operatorname{concat}(a_j,a_i)+a_i\times a_j\not\equiv Z\pmod3\)
  • \(\operatorname{concat}(x,y)\) 表示将十进制下的 \(x\) 接在十进制下的 \(y\) 的左边形成的新十进制数。例如,\(\operatorname{concat}(10,24)=1024\)

输入格式

第一行一个正整数(\(2\le n\le10^5,2\mid n\)),含义见题目描述
第二行 \(n\) 个正整数 \(a_i\),含义见题目描述


输出格式

若存在 \(Z\)\(0\le Z\le2\))和一个分组方案满足题述条件,则在第 \(1\) 行输出 \(Z\),在第 \(2\) 行输出一个字符串 \(s\),其中 \(s_i=0\) 表示第 \(i\) 个数分到第一组,\(s_i=1\) 表示第 \(i\) 个数分到第二组;否则直接输出 \(-1\)

思路

首先,显然有 \(\operatorname{concat}(a_i,a_j)\times \operatorname{concat}(a_j,a_i)+a_i\times a_j\equiv (a_i+a_j)^2+a_i\times a_j\pmod3\)

所以我们可以把每个数都模 \(3\) 后再计算。

对于 \(x,y\in\{0,1,2\}\),我们通过计算可以得到下表:

0 1 2
0 0 1 1
1 1 2 2
2 1 2 2

通过观察,我们发现:若将所有 \(0\) 都分为同一组,则计算结果将不会出现 \(0\);若将所有 \(1\)\(2\) 都分为同一组,则计算结果将不会出现 \(2\)

因此,我们可以得到下述构造方案:

  • \(cnt\) 等于满足 \(a_i\bmod 3=0\) 的个数。
  • \(cnt<\frac{n}{2}\),则令 \(Z\) 等于 \(0\),将所有满足 \(a_i\bmod3=0\) 的分为一组,剩下的随缘分配。
  • \(cnt\ge\frac{n}{2}\),则令 \(Z\) 等于 \(2\),将所有满足 \(a_i\bmod3\neq0\) 的分为一组,剩下的随缘分配。

程序

AC 记录

#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<cmath>
#include<iomanip>
#include<string>
#include<stack>
#include<random>
#include<utility>
#define re register
#define ll long long
#define ull unsigned long long
#define vl __int128
#define ld long double
#define LL 2e18
#define INT 1e9
#define INF 0x3f3f3f3f
#define MP make_pair
#define pb push_back
#define lb(x) (x&(-x))
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
int T=1;
using namespace std;
pair<int*,int*> pair_int;pair<ll*,ll*> pair_ll;pair<ull*,ull*> pair_ull;pair<double*,double*> pair_double;pair<ld*,ld*> pair_ld;pair<char*,char*> pair_char;pair<bool*,bool*> pair_bool;
inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
template<typename Tp>inline void read(Tp &x){x=0;bool z=true;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=(x<<1)+(x<<3)+(a^48);x=(z?x:~x+1);}
inline void read(double &x){x=0.0;bool z=true;double y=0.1;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(ld &x){x=0.0;bool z=true;ld y=0.1;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(char &x){for(x=gc();blank(x)&&(x^-1);x=gc());}
inline void read(char *x){char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())*x++=a;*x=0;}
inline void read(string &x){x="";char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
template<typename T>inline void read(pair<T*,T*> p){T *begin,*end,*i;begin=p.first,end=p.second;if(begin<end)for(i=begin;i<end;++i)read(*i);else for(i=begin-1;i>=end;--i)read(*i);}
template<typename T,typename ...Tp>inline void read(T &x,Tp &...y){read(x),read(y...);}
template<typename Tp>inline void write(Tp x){if(!x)return pc(48),void();if(x<0)pc('-'),x=~x+1;int len=0;char tmp[64];for(;x;x/=10)tmp[++len]=x%10+48;while(len)pc(tmp[len--]);}
inline void write(const double x){int a=6;double b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;double y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const ld x){int a=6;ld b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;ld y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const pair<int,double>x){int a=x.first;if(a<7){double b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;double y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*lf",a,x.second);}
inline void write(const pair<int,ld>x){int a=x.first;if(a<7){ld b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;ld y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*Lf",a,x.second);}
inline void write(const char x){pc(x);}
inline void write(const bool x){pc(x?49:48);}
inline void write(char *x){fputs(x,stdout);}
inline void write(const char *x){fputs(x,stdout);}
inline void write(const string &x){fputs(x.c_str(),stdout);}
template<typename T>inline void write(pair<T*,T*> p){T *begin,*end,*i;begin=p.first,end=p.second;for(i=begin;i<end;++i)write(*i),write(' ');}
template<typename T>inline void write(pair<pair<T*,T*>,char> p){T *begin,*end,*i;begin=p.first.first,end=p.first.second;char c=p.second;for(i=begin;i<end;++i)write(*i),write(c);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y){write(x),write(y...);}
template<typename T>inline void init(T *begin,T *end,const T& val=T()){T* i;for(i=begin;i<end;++i)*i=val;}
template<typename T>inline T max(T *begin,T *end){T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*ans<*i)ans=i;return *ans;}
template<typename T>inline T min(T *begin,T *end){T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*i<*ans)ans=i;return *ans;}
template<typename T>inline T calc_sum(T *begin,T *end,const T& val=T()){T ans=val,*i;for(i=begin;i<end;++i)ans+=*i;return ans;}
template<typename T>inline bool is_equal(T *begin,T *end,const T& val=T()){T *i;for(i=begin;i<end;++i)if(*i!=val)return false;return true;}

ll mod=0;
const int MAXN=1e5;
const int N=MAXN+10;
//#define DEBUG
//#define more_text

int n,a[N];

int cnt;

void SOLVE(int _){
	read(n);read(pair_int=MP(a+1,a+n+1));
	for(int i=1;i<=n;++i)if(a[i]%3==0)++cnt;
	if(cnt<n>>1){
		write("0\n");
		for(int i=1,j=(n>>1)-cnt;i<=n;++i){
			if(!(a[i]%3))write('0');
			else{
				if(j)write('0'),--j;
				else write('1');
			}
		}
	}
	else{
		write("2\n");
		for(int i=1,j=cnt-(n>>1);i<=n;++i){
			if(a[i]%3)write('0');
			else{
				if(j)write('0'),--j;
				else write('1');
			}
		}
	}
}
/*
Input:

Output:

Outline:

*/
int main(){
	#ifdef DEBUG
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
	#endif
	#ifdef more_text
	read(T);
	#endif 
	for(int i=1;i<=T;++i)SOLVE(i);
	#ifdef DEBUG
	fclose(stdin);fclose(stdout);
	#endif
	return 0;
}
posted @ 2025-03-09 20:56  LXcjh4998  阅读(15)  评论(0)    收藏  举报