QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#574903 | #2519. Number with Bachelors | TulipeNoire | WA | 45ms | 23128kb | C++14 | 2.6kb | 2024-09-19 08:29:45 | 2024-09-19 08:29:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=25;
using uLL=unsigned long long;
using i128=__int128;
int B;
uLL qk;
int b[N],cnt;
uLL f[2][N][65536];
bool vis[2][N][65536];
uLL dfs(int tp,int now,int S,int first,int up) {
if (!now) return 1;
if (up&&!first&&vis[tp][now][S]) return f[tp][now][S];
uLL res=0;
for (int i=0;i<B;i++) {
if ((S>>i)&1) continue;
if (first&&!i) continue;
if (!up) {
if (i<b[now]) res+=dfs(tp,now-1,S|(1<<i),0,1);
if (i==b[now]) res+=dfs(tp,now-1,S|(1<<i),0,0);
} else res+=dfs(tp,now-1,S|(1<<i),0,1);
}
if (up&&!first) vis[tp][now][S]=1,f[tp][now][S]=res;
// cout<<now<<' '<<S<<' '<<first<<' '<<up<<' '<<res<<"\n";
return res;
}
inline uLL calc(uLL x) {
cnt=0;
while (x) {
b[++cnt]=x%B,x/=B;
}
uLL res=dfs(B==10,cnt,0,1,0);
for (int i=cnt-1;i>=0;i--) res+=dfs(B==10,i,0,1,1);
return res;
}
inline bool check(uLL x) {
return calc(x)>=qk;
}
uLL bs(uLL l,uLL r) {
if (l==r) return l;
if (l+1==r) {
if (check(l)) return l;
return r;
}
uLL mid=(((i128)l)+r)/2;
if (check(mid)) return bs(l,mid);
return bs(mid+1,r);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int tac;
cin>>tac;
for (int tt=1;tt<=tac;tt++) {
char tp;
int op;
string l,r,x;
cin>>tp>>op;
qk=0;
if (tp=='h') B=16;
else B=10;
// if (tt==92) {
// cout<<tp<<'_'<<op<<'_';
// }
if (op) {
cin>>x;
// if (tt==92) {
// cout<<x<<"\n";
// }
for (auto o:x) {
if (isdigit(o)) qk=qk*B+o-'0';
else qk=qk*B+o-'a'+10;
}
uLL maxn=0xffffffffffffffff;
uLL res=bs(0,maxn);
if (!res) {
cout<<"0\n";
continue;
}
if (res==maxn) {
cout<<"-\n";
} else {
int bb[N]={},ccnt=0;
while (res) bb[++ccnt]=res%B,res/=B;
for (int i=ccnt;i;i--) {
if (bb[i]>=10) cout<<(char)('a'+bb[i]-10);
else cout<<(char)('0'+bb[i]);
}
cout<<"\n";
}
} else {
cin>>l>>r;
// if (tt==92) {
// cout<<l<<'_'<<r<<"\n";
// }
uLL ql=0,qr=0;
for (auto o:l) {
if (isdigit(o)) ql=ql*B+o-'0';
else ql=ql*B+o-'a'+10;
}
for (auto o:r) {
if (isdigit(o)) qr=qr*B+o-'0';
else qr=qr*B+o-'a'+10;
}
uLL res;
if (ql) res=calc(qr)-calc(ql);
else res=calc(qr);
// cout<<ql<<' '<<qr<<"\n";
if (!res) {
cout<<"0\n";
continue;
}
int bb[N]={},ccnt=0;
// cout<<res<<"\n";
while (res) bb[++ccnt]=res%B,res/=B;
for (int i=ccnt;i;i--) {
if (bb[i]>=10) cout<<(char)('a'+bb[i]-10);
else cout<<(char)('0'+bb[i]);
}
cout<<"\n";
}
}
return 0;
}
/*
2
h 1 147a
d 0 25 71
*/
详细
Test #1:
score: 0
Wrong Answer
time: 45ms
memory: 23128kb
input:
6 d 0 10 20 h 0 10 1f d 1 10 h 1 f d 1 1000000000 h 1 ffffffffffffffff
output:
9 e 9 e - -
result:
wrong answer 1st lines differ - expected: '10', found: '9'