QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1481#868634#9682. nim 游戏JohnAlfnovIvanZhang2009Failed.2025-01-26 20:57:212025-01-26 20:57:21

Details

Extra Test:

Standard Program Time Limit Exceeded

input:

24 2
99999 5000
1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 107374182...

output:


result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#868634#9682. nim 游戏IvanZhang2009100 ✓317ms49416kbC++144.3kb2025-01-24 19:54:352025-01-31 21:38:56

answer

#include<bits/stdc++.h>
using namespace std;
#define MOD         998244353
#define speMOD      2933256077ll
#define int         long long
#define pii         pair<int,int>
#define all(v)      v.begin(),v.end()
#define pb          push_back
#define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
#define over(x)     {cout<<(x)<<endl;return;}
#define lowbit(x)   ((x)&(-(x)))
#define cntbit(x)   __builtin_popcount(x)
#define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
#define lbound(v,x) lower_bound(all(v),x)-v.begin()
#define endl        '\n'
mt19937 sd(random_device{}());
int qpow(int a,int b,int m=MOD,int res=1){
	a%=m;
	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
	return res;
}
int exgcd(int x,int y,int &a,int &b){
	if(y==0){
		a=1;b=0;
		return x;
	}
	int d=exgcd(y,x%y,a,b);
	int z=b;
	b=a-b*(x/y);
	a=z;
	return d;
}
int _x_,_y_;
inline int inverse(int x,int y=MOD){
	exgcd(x,y,_x_,_y_);
	return (_x_+y)%y;
}
int fac[2000005],inv[2000005];
void init(int n){
	fac[0]=inv[0]=1;
	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
	inv[n]=qpow(fac[n],MOD-2);
	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
}
int binom(int x,int y){
	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
int ID;
int n,m;
vector<vector<pii>>ans;
int a[200005];
pii r[200005];
int T;
void add(){
	map<int,int>mp;
	REP(i,0,62)mp[r[i].first]+=r[i].second;
	vector<pii>res;
	for(auto [i,j]:mp)if(i!=-1)res.pb({i,j});
	ans.pb(res);
}
#define check(x) if((int)ans.size()==m)return x;
int c,XOR;
bool dfs2(int x,int cur,int spe){
	check(0);
	if(cur>c)return 0;
	if(x==-1){
		if(cur==c)add();
		return cur==c;
	}
	vector<pii>t;
	r[x]={-1,-1};
	if(spe==x){
		if((XOR>>x)&1)return 0;
		REP(i,0,n)if(!((a[i]>>x)&1)){
			int y=a[i]&((1ll<<x)-1);
			t.pb({(1ll<<x)-y,i});
		}
		if(t.size()<2)return 0;
		sort(all(t));
		int f=0,g=0;
		REP(X,0,t.size()){
			auto [i,j]=t[X];XOR^=a[j]^(a[j]+i);
			r[x]={j,i};a[j]+=i;
			f=0;
			REP(y,X+1,t.size()){
				auto [I,J]=t[y];
				r[61]={J,I};a[J]+=I;XOR^=a[J]^(a[J]-I);
				if(!dfs2(x-1,cur+i+I,spe)){r[61]={-1,-1},a[J]-=I;XOR^=a[J]^(a[J]+I);break;}
				f=1;a[J]-=I;XOR^=a[J]^(a[J]+I);check(f);
			}
			a[j]-=i;g|=f;XOR^=a[j]^(a[j]+i);
			check(g);
			if(!f)return g;
		}
		return g;
	}
	if(!((XOR>>x)&1))return dfs2(x-1,cur,spe);
	REP(i,0,n)if(!((a[i]>>x)&1)){
		int y=a[i]&((1ll<<x)-1);
		t.pb({(1ll<<x)-y,i});
	}
	sort(all(t));
	int f=0;
	for(auto [i,j]:t){
		XOR^=a[j]^(a[j]+i);
		r[x]={j,i};a[j]+=i;
		if(!dfs2(x-1,cur+i,spe))return a[j]-=i,XOR^=a[j]^(a[j]+i),f;
		a[j]-=i;f=1;XOR^=a[j]^(a[j]+i);
		check(f);
	}
	return f;
}
int re[200005];
void Main() {
	cin>>n>>m;
	ans.clear();
	REP(i,0,n)cin>>a[i];
	vector<int>b;
	REP(i,0,n)b.pb(a[i]);
	cerr<<clock()<<endl;
	c=0;
	int fl=60,xx=0;
	REP(i,0,n)xx^=a[i];
	REP(i,0,60)re[i]=-1;
	r[60]=r[61]={-1,-1};
	for(int i=59;i>=0;--i){
		int f=0;
		for(auto &j:b)if((j>>i)&1)f^=1;
		int x=-1,mx=0;
		REP(j,0,n)if(b[j]<(1ll<<i)&&b[j]>=mx)mx=b[j],x=j;
		re[i]=0;
		REP(j,0,n)if(b[j]<(1ll<<i))++re[i];
		if(f){
			if(x<0){
				fl=i;
				break;
			}
			c+=(1ll<<i)-b[x];b[x]=0;
		}
		for(auto &j:b)if((j>>i)&1)j^=1ll<<i;
	}
	cerr<<clock()<<endl;
	re[60]=n;
	if(fl!=60){
		c=9e18;
		REP(t,fl+1,61)if(re[t]>=2){
			int s=0;
			b.clear();
			REP(i,0,n)b.pb(a[i]);
			for(int i=60;i>=0;--i){
				int f=0;
				for(auto &j:b)if((j>>i)&1)f^=1;
				int x=-1,mx=0;
				REP(j,0,n)if(b[j]<(1ll<<i)&&b[j]>=mx)mx=b[j],x=j;
				if(f||t==i){s+=(1ll<<i)-b[x];b[x]=0;}
				if(!f&&t==i){
					mx=0;
					REP(j,0,n)if(b[j]<(1ll<<i)&&b[j]>=mx)mx=b[j],x=j;
					s+=(1ll<<i)-b[x];b[x]=0;
				}
				for(auto &j:b)if((j>>i)&1)j^=1ll<<i;
			}
			c=min(c,s);
			re[t]=s;
		}else re[t]=-1;
		cerr<<clock()<<endl;
		for(int i=60;i>fl;--i)if(re[i]==c){
			XOR=xx;dfs2(60,0,i);
		}
	}else XOR=xx,dfs2(59,0,-1);
	cout<<c<<endl<<ans.size()<<endl;
	for(auto i:ans){
		cout<<i.size()<<endl;
		for(auto [j,k]:i)cout<<j+1<<' ';
		cout<<endl;
		for(auto [j,k]:i)cout<<k<<' ';
		cout<<endl;
	}
}
void TC() {
    int tc=1;
    cin>>ID>>tc;
	while(tc--){
		Main();
		cout.flush();
	}
	cerr<<clock()<<' '<<T<<endl;
}
signed main() {
	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
}
/*
1. CLEAR the arrays (ESPECIALLY multitests)
2. DELETE useless output
 */