QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1478#873631#9682. nim 游戏JohnAlfnovlgvcSuccess!2025-01-26 19:36:422025-01-26 19:36:42

Details

Extra Test:

Time Limit Exceeded

input:

24 2
99999 1000
219023094354495928 889115471142505106 734262802011767667 248030652959169780 11310834021373402 915466521440437674 984950787530910255 183576971104218221 530249026600698230 87679788178204217 563553067779697451 874838112044019965 310609512380023295 217115927503210278 877275279236811573 7...

output:

18930664935104
1000
29
1521 4305 11297 12548 14381 28402 29851 35666 38260 45727 48905 52484 52808 58946 66041 70480 71070 71698 72906 77815 82502 83587 85040 86351 89458 90153 95523 96457 98095 
1215008053 219834024 83984 95433032758 292647 1 213 1 1 30 501 1 404074779554 336 6 7879695 1 409475067 ...

result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873631#9682. nim 游戏lgvc97 759ms58452kbC++234.6kb2025-01-26 18:53:592025-01-26 19:38:32

answer

#include <bits/stdc++.h>
#define int long long
static char buf[1000000],*paa=buf,*pd=buf;
static char buf2[1000000],*pp=buf2;
#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline void pc(char ch){
	if(pp-buf2==1000000) fwrite(buf2,1,1000000,stdout),pp=buf2;
	*pp++=ch;
}
inline void pcc(){
	fwrite(buf2,1,pp-buf2,stdout);
	pp=buf2;
}
inline int read(void){
	int w=1;
	register int x(0);register char c(getchar());
	while(c<'0'||c>'9'){if(c=='-') w=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return w*x;
}
void write(int x){
	static int sta[20];
	int top=0;
	do{
		sta[top++]=x%10,x/=10;
	}while(x);
	while(top) pc(sta[--top]+48);
}
void we(int x){
	write(x);
	pc('\n');
}
int C,T,N,M,a[100009],b[100009],vf[100009],ss,p[62][100009];
struct n_t{
	int a,b;
};
std::vector<n_t> t[20009];
int ct,tx;
int qr(std::vector<n_t> tq) {
	int as=ss;
	for(int i=0;i<tq.size();i++) {
		as^=a[tq[i].a];
		as^=(a[tq[i].a]+tq[i].b);
	}
	return as;
}
inline bool cmp(n_t x,n_t y) {
	return x.a<y.a;
}
inline bool fd(int x,std::vector<n_t> y) {
	for(int i=0;i<y.size();i++) {
		if(y[i].a==x) return 1;
	}
	return 0;
}
int vc(std::vector<n_t> tq) {
	tx++;
	for(int i=0;i<tq.size();i++) vf[tq[i].a]=tx;
	return tx;
}
int sv(std::vector<n_t> tq) {
	int aq=0;
	int ti=vc(tq);
	int xx=qr(tq);
	for(int i=60;i>=0;i--) {
		if((xx>>i)&1) {
			bool zt=0;
			for(int j=1;j<=N;j++) {
				int t=p[i][j];
				if((a[t]>>i)%2==0) {
					if(vf[t]==ti) continue;
					int x=(a[t]&((1ll<<i)-1));
					xx^=(a[t]);
					xx^=(a[t]+(1ll<<i)-x);
					aq+=(1ll<<i)-x;
					tq.push_back((n_t){t,(1ll<<i)-x});
					vf[t]=ti;
					zt=1;
					break;
				} else {
					break;
				}
			}
			if(!zt) {
				for(int j=0;j<tq.size();j++) {
					int x=tq[j].b+a[tq[j].a];
					if(x&((1ll<<i+1)-1)) continue;
					xx^=x;
					xx^=(x+(1ll<<i));
					tq[j].b+=(1ll<<i);
					aq+=(1ll<<i);
					zt=1;
					break;
				}
				if(!zt) {
					return (1ll<<62);
				}
			}
		}
	}	
	return aq;	
}
std::unordered_map<int,int> vi;
int tc;
void cl(std::vector<n_t> tq) {
	if(ct==M) return;
	std::sort(tq.begin(),tq.end(),cmp);
	int vm=tq.size();
	for(int j=0;j<tq.size();j++) {
		vm=(vm*137+tq[j].a)%10000000000000061ll;
		vm=(vm*137+tq[j].b)%10000000000000061ll;
	}
	if(vi[vm]==tc) return;
	vi[vm]=tc;
	ct++;
	t[ct]=tq;
}
bool qj(int i,std::vector<n_t> tq,int va) {	
	if(va!=sv(tq)) return 0;
	if(i==-1) {
		cl(tq);
		return 1;
	}
	if((qr(tq)>>i)&1) {
		//if(i==0) printf("!!!\n");
		int ans=(1ll<<61);
		for(int j=1;j<=N;j++) {
			int t=p[i][j];
			if(fd(t,tq)) continue;
			if((a[t]>>i)%2==0) {
			//	assert(t);
				int x=(a[t]&((1ll<<i)-1));
				tq.push_back((n_t){t,(1ll<<i)-x});
				if(!qj(i-1,tq,va-(1ll<<i)+x)) return 1;
				tq.pop_back();
				if(ct==M) return 1;
			} else break;
		}
		for(int j=0;j<tq.size();j++) {
			int x=tq[j].b+a[tq[j].a];
			if(x&((1ll<<i+1)-1)) continue;
			tq[j].b+=(1ll<<i);
			if(!qj(i-1,tq,va-(1ll<<i))) return 1;
			if(ct==M) return 1;
			tq[j].b-=(1ll<<i);
		}
	} else {
		qj(i-1,tq,va);
	}
	return 1;
}
#define INF_LL 0x3f3f3f3f3f3f3f3f
int tp;
bool cmq(int x,int y) {
	x=a[x];
	y=a[y];
	if(((x^y)>>tp)&1) 
		return (x&((1ll<<tp+1)-1))<(y&((1ll<<tp+1)-1));
	return (x&((1ll<<tp)-1))>(y&((1ll<<tp)-1));
}
signed main(void) {
	C=read();T=read();
	while(T--) {
		tc++;
		N=read();M=read();
		ct=0;
		ss=0;
		for(int i=1;i<=N;i++) {
			a[i]=read();
			b[i]=a[i];
			ss^=a[i];
		}
		if(ss==0) {
			printf("0\n1\n0\n\n\n");
			continue;
		}
		for(int i=0;i<=60;i++) {
			for(int j=1;j<=N;j++) p[i][j]=j;
			tp=i;
			std::sort(p[i]+1,p[i]+N+1,cmq);
		}
		std::vector<n_t> tq;
		int ans=sv(tq);
		for(int i=0;i<=60;i++) {
			std::vector<n_t> tq;
			if(ss>=(1ll<<i+1)) continue;
			int t=p[i][1];
			if((a[t]>>i)&1) continue;
			int x=(a[t]&((1ll<<i)-1));
			tq.push_back((n_t){t,(1ll<<i)-x});
			ans=std::min(ans,sv(tq)+(1ll<<i)-x);
		}
		qj(60,tq,ans);
		for(int i=0;i<=60;i++) {
			if(ct==M) break;
			if(ss>=(1ll<<i+1)) continue;
			std::vector<n_t> tq;
			for(int j=1;j<=N;j++) {
				int t=p[i][j];
				if((a[t]>>i)&1) break;
				int x=(a[t]&((1ll<<i)-1));
				tq.push_back((n_t){t,(1ll<<i)-x});
				if(!qj(i,tq,ans-(1ll<<i)+x)) break;
				if(ct==M) break;
				tq.pop_back();
			}
		}
//		qj(60,tq,ans);
		printf("%lld\n%lld\n",ans,ct);
		for(int i=1;i<=ct;i++) {
			printf("%lld\n",(int)t[i].size());
			for(int j=0;j<t[i].size();j++) printf("%lld ",t[i][j].a);
			printf("\n");
			for(int j=0;j<t[i].size();j++) printf("%lld ",t[i][j].b);
			printf("\n");
			t[i]=t[0];
		}	
	}
}