QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#58482#4625. Simple Math 4AFewSunsAC ✓947ms3968kbC++3.0kb2022-10-26 17:08:172022-10-26 17:08:18

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-26 17:08:18]
  • Judged
  • Verdict: AC
  • Time: 947ms
  • Memory: 3968kb
  • [2022-10-26 17:08:17]
  • Submitted

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
//#pragma GCC optimize(3)
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define inf 8e18
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
ll t,n,l,r,x,f[33][33][33],g[33][33][2];
int main(){
	t=read();
	while(t--){
		n=read();
		l=read();
		r=read();
		x=read();
		fr(i,0,30) fr(j,0,30) fr(k,0,30) f[i][j][k]=-inf;
		ll pos=-1,nn=min(n,30ll);
		if((n-nn)&1) x^=r;
		pfr(i,30,0){
			if((l>>i)!=(r>>i)){
				pos=i;
				break;
			}
		}
		bl ck=0;
		pfr(i,30,pos+1){
			ll tmp=nn&(l>>i)&1;
			if(((x>>i)&1)!=tmp) ck=1;
		}
		if(ck){
			writeln(-1);
			continue;
		}
		if(pos==-1){
			writeln(l*n);
			continue;
		}
		fr(j,0,nn){
			ll k=nn-j;
			if((k&1)!=((x>>pos)&1)) continue;
			f[pos][j][k]=nn*((l>>(pos+1))<<(pos+1))+(k<<pos);
		}
		pfr(i,pos-1,0){
			fr(j,0,nn) fr(k,0,nn-j) g[j][k][0]=g[j][k][1]=-inf;
			ll tmp1=(l>>i)&1,tmp2=(r>>i)&1,tmp3=(x>>i)&1;
			pfr(j,nn,0){
				pfr(k,nn-j,0){
					if(f[i+1][j][k]==-inf) continue;
					fr(p,0,j){
						if(p&&tmp1) continue;
						ll tmp=p+nn-j-k;
						if(!p) tmp=j*tmp1+nn-j-k;
						g[j-p][k][tmp&1]=max(g[j-p][k][tmp&1],f[i+1][j][k]+(tmp<<i));
						if((j+k)<nn) g[j-p][k][(tmp&1)^1]=max(g[j-p][k][(tmp&1)^1],f[i+1][j][k]+((tmp-1)<<i));
					}
				}
			}
			pfr(j,nn,0){
				pfr(k,nn-j,0){
					fr(o,0,1){
						if(g[j][k][o]==-inf) continue;
						fr(p,0,k){
							if(p&&!tmp2) continue;
							ll tmp=k-p;
							if(!p) tmp=k*tmp2;
							if((o^(tmp&1))==tmp3) f[i][j][k-p]=max(f[i][j][k-p],g[j][k][o]+(tmp<<i));
						}
					}
				}
			}
		}
//		pfr(i,pos,0) fr(j,0,30) fr(k,0,30-j) if(f[i][j][k]!=-inf) pf("%lld %lld %lld : %lld\n",i,j,k,f[i][j][k]);
		ll ans=-inf;
		fr(j,0,nn) fr(k,0,nn-j) ans=max(ans,f[0][j][k]);
		if(ans==-inf) writeln(-1);
		else writeln(ans+(n-nn)*r);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 947ms
memory: 3968kb

input:

3000
593718412 146792379 319675298 98752874
790750670 473323241 741594851 303417985
59026530 26273559 791122104 879473132
237003289 172890703 353231651 435848282
702962299 788548665 889286227 276141429
421767491 588606139 713224582 116797840
655572191 79298565 330472197 8059725
820810481 694633380 9...

output:

189797110112597544
-1
46697192292348618
-1
-1
-1
216648381816189679
-1
-1
940637753875148226
272335833285441530
405931836928416932
407144340379573882
-1
-1
487222236560792116
167744473671412834
101595758717448054
2555977668282139
422163359075881944
-1
-1
191148117225033053
-1
-1
-1
54620505681952995...

result:

ok 3000 lines