QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#572753 | #2519. Number with Bachelors | lifan | AC ✓ | 215ms | 44544kb | C++14 | 2.1kb | 2024-09-18 16:14:13 | 2024-09-18 16:14:14 |
Judging History
answer
//
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
int n,m,num[25],len;//memest会死哎,但是flg=1的状态唯一应该不用记录都是对的
ull f[20][1<<17][2],K;
ull dfs(int cnt,int S,int flg){
if(cnt<0)return 1;
if(!flg&&f[cnt][S][K==10]!=-1)return f[cnt][S][K==10];
int up=flg?num[cnt]:K-1;ull res=0;
for(int i=0;i<=up;++i)if(((S>>i)&1)==0)res+=dfs(cnt-1,(S==0&&i==0)?0:(S|(1<<i)),flg&(i==up));
if(!flg)f[cnt][S][K==10]=res;
return res;
}
ull solve(ull x){
ull tmp=x,len=0;
while(tmp)num[len++]=tmp%K,tmp/=K;
return dfs(len-1,0,1);
}
ull read(){
string s;cin>>s;
ull x=0;
for(int i=0;i<s.size();++i){
x=x*K;
if(s[i]>='0'&&s[i]<='9')x+=s[i]-'0';
else x+=s[i]-'a'+10;
}return x;
}
int out[55];
void Print(ull x){
// string a="";
if(!x){
cout<<"0\n";return ;
}
if(K==10){
cout<<x<<"\n"; return ;
}
int c=0;
while(x){
out[++c]=x%K;x/=K;
}
reverse(out+1,out+c+1);
for(int i=1;i<=c;++i){
if(out[i]<10)cout<<out[i];
else {
char x='a'+out[i]-10;
cout<<x;
}
}
cout<<"\n";
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
memset(f,-1,sizeof f);
ull T;cin>>T;
while(T--){
char op;cin>>op;
if(op=='d')K=10;
else K=16;
ull opt;cin>>opt;
if(opt){
ull k=read();
ull l=0,r=0;--r;
// cout<<r<<'\n';
if(solve(r)<k){
cout<<"-\n";continue;
}
while(l<r){
ull mid=l+r>>1;
// cout<<l<<" "<<r<<"\n";
if(solve(mid)<k)l=mid+1;
else r=mid;
}
Print(l);
}
else {
ull l=read(),r=read();
// cout<<l<<" "<<r<<"\n";
ull res=solve(r)-(l?solve(l-1):0);
Print(res);
}
}
}
/*
6
d 0 10 20
h 0 10 1f
d 1 10
h 1 f
d 1 1000000000
h 1 ffffffffffffffff
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 45ms
memory: 44540kb
input:
6 d 0 10 20 h 0 10 1f d 1 10 h 1 f d 1 1000000000 h 1 ffffffffffffffff
output:
10 f 9 e - -
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 215ms
memory: 44544kb
input:
50000 h 1 147a d 0 25 71 d 1 3587 d 0 26922 51887 d 1 711 d 0 3 5 h 0 7bf2defab442a0b1 f299a4cf1d4d9bed d 0 6961 91018 d 1 4 d 1 876 h 1 12cc5d3370f99120 d 1 161315 h 0 25f 6959 d 0 467 516 d 1 298 h 1 70260cdc2da73281 h 1 928e17d65d764ca2 h 1 c8ec8a7b67605e51 d 1 91697 d 0 4941925161850941148 89850...
output:
1b36 43 6587 7710 953 3 8daab378500 26054 3 1356 - 946307 4681 40 387 - - - 491850 0 1 29 - 4605298 1 1 - 15b4 175f 9b944134000 124b7 6279 9 6257 - 39be22a900 5c636b59300 146ce 2a55 - 0 - 7 d 6 2041 - 1c94afe7300 0 5 9149 16540973 1389 125 0 - 3bc31189480 424 66800 7 - - 1e6 0 0 48b6 9 - 2b0 5019 14...
result:
ok 50000 lines