QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211657 | #4625. Simple Math 4 | yiyiyi# | AC ✓ | 1172ms | 13608kb | C++14 | 1.4kb | 2023-10-12 20:03:00 | 2023-10-12 20:03:00 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define re register
#define inf 0x3f3f3f3f
#define N 10202000
using namespace std;
const int mo=998244353;
inline int read(){
int x=0,w=0;char ch=getchar();
while (!isdigit(ch))w|=ch=='-',ch=getchar();
while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return w?-x:x;
}
int n,m,l,r,x,f[32][200][200];
int dfs(int k,int lim1,int lim2){
if (k==-1)return 0;
if (~f[k][lim1][lim2])return f[k][lim1][lim2];
int dn=lim1?((l>>k)&1):0,up=lim2?((r>>k)&1):1,ans=-1;
if (((x>>k)&1)){
for (int i=1;i<=m;i+=2){
if (i<lim1&&dn)continue;
if (i>m-lim2&&!up)continue;
int t=dfs(k-1,dn?lim1:max(lim1-i,0ll),up?max(lim2-(m-i),0ll):lim2);
if (t==-1)continue;
ans=max(ans,i*(1ll<<k)+t);
}
}else{
for (int i=0;i<=m;i+=2){
if (i<lim1&&dn)continue;
if (i>m-lim2&&!up)continue;
int t=dfs(k-1,dn?lim1:max(lim1-i,0ll),up?max(lim2-(m-i),0ll):lim2);
if (t==-1)continue;
ans=max(ans,i*(1ll<<k)+t);
}
}
return f[k][lim1][lim2]=ans;
}
signed main(){
int T=read();
while (T--){
n=read(),l=read(),r=read(),x=read();
if (n==1){
if (l<=x&&x<=r)
printf("%lld\n",x);
else puts("-1");
continue;
}
memset(f,-1,sizeof(f));
m=min(n,30ll);
if ((m^n)&1)++m;
int ans=dfs(30,m,m);
if (ans==-1)puts("-1");
else printf("%lld\n",ans+(n-m)*r);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1172ms
memory: 13608kb
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