QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#574888 | #2519. Number with Bachelors | Others | AC ✓ | 175ms | 97408kb | C++14 | 3.5kb | 2024-09-19 07:57:23 | 2024-09-19 07:57:27 |
Judging History
answer
//
#include <bits/stdc++.h>
#define ll __int128
#define int __int128
#define db long double
#define x first
#define y second
#define wdnmd const int mod=::mod;
#define eps 1e-12
#define lb(i) ((i)&(-(i)))
#define wr(x,ch) write(x),putchar(ch)
using namespace std;
typedef pair<int,char> paic;
typedef pair<int,int> pai;
typedef pair<pai,int> paii;
typedef pair<pai,pai> ppai;
typedef pair<ppai,int> ppaii;
pai operator+(const pai &x,const pai &y) {return pai(x.x+y.x,x.y+y.y);}
pai operator-(const pai &x,const pai &y) {return pai(x.x-y.x,x.y-y.y);}
#define gh() getchar()
inline long long read() {
char ch=gh();
long long x=0;
char t=0;
while(ch<'0'||ch>'9') t|=ch=='-',ch=gh();
while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=gh();
return t?-x:x;
}
void write(ll x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);
putchar(x%10^48);
return ;
}
const int one=1;
ll dp[2][45][1<<10],dp2[2][45][1<<16],sta[45],len;
int l,r,k;
char typ[12],Typ[12],num[105];
ll dfs1(ll x,ll f1,ll f0,ll S) {
if(x==0) return 1;
if(!f1&&dp[f0][x][S]!=-1) return dp[f0][x][S];
ll ans;
if(f0) ans=dfs1(x-1,0,1,0);
else ans=(S&1)?0:dfs1(x-1,f1&(!sta[x]),0,S|1);
for(ll i=1;i<10;i++) {
if(S>>i&1) continue;
if(f1&&i>sta[x]) break;
ans+=dfs1(x-1,f1&(i==sta[x]),0,S|(1<<i));
}
if(!f1) return dp[f0][x][S]=ans;
return ans;
}
ll dfs2(ll x,ll f1,ll f0,ll S) {
if(x==0) return 1;
if(!f1&&dp2[f0][x][S]!=-1) return dp2[f0][x][S];
ll ans;
if(f0) ans=dfs2(x-1,0,1,0);
else ans=(S&1)?0:dfs2(x-1,f1&(!sta[x]),0,S|1);
for(int i=1;i<16;i++) {
if(S>>i&1) continue;
if(f1&&i>sta[x]) break;
ans+=dfs2(x-1,f1&(i==sta[x]),0,S|(1<<i));
}
if(!f1) return dp2[f0][x][S]=ans;
return ans;
}
ll solve1(int x) {
if(x==-1) return 0;
len=0;
while(x) sta[++len]=x%10,x/=10;
// memset(dp[1],-1,sizeof(dp[1]));
return dfs1(len,1,1,0);
}
ll solve2(int x) {
if(x==-1) return 0;
len=0;
while(x) sta[++len]=x%16,x/=16;
// memset(dp[1],-1,sizeof(dp[1]));
return dfs2(len,1,1,0);
}
inline char id(int x) {
if(x<10) return x+'0';
return x-10+'a';
}
inline int id2(char x) {
if(isdigit(x)) return x^48;
return x-'a'+10;
}
inline int calc(char *a) {
int l=strlen(a);
int ans=0;
for(int i=0;i<l;i++) ans=ans*16+id2(a[i]);
return ans;
}
inline void print(int x) {
if(x>=16) print(x/16);
putchar(id(x%16));
return ;
}
void solve() {
scanf("%s",typ),scanf("%s",Typ);
if(typ[0]=='d') {
if(Typ[0]=='0') {//10th count
l=read(),r=read(),l--;
wr(solve1(r)-solve1(l),'\n');
} else {//10th kth
k=read();
if(k>8877690) {
puts("-");
return ;
}
int l=0,r=10000000000,mid;
while(l<r) {
mid=l+r>>1;
// wr(mid,' '),wr(solve1(mid),'\n');
if(solve1(mid)<k) l=mid+1;
else r=mid;
}
if(solve1(l)==k) wr(l,'\n');
else puts("-");
}
} else {
if(Typ[0]=='0') {//16th count
scanf("%s",num),l=calc(num)-1;
scanf("%s",num),r=calc(num);
// wr(solve2(r)-solve2(l),'\n');
print(solve2(r)-solve2(l)),putchar('\n');
} else {//16th kth
scanf("%s",num);
k=calc(num);
if(k>53319412081140) {
puts("-");
return ;
}
int l=0,r=one<<70,mid;
while(l<r) {
mid=l+r>>1;
if(solve2(mid)<k) l=mid+1;
else r=mid;
}
if(solve2(l)==k) print(l),putchar('\n');
else puts("-");
}
}
return ;
}
signed main() {
memset(dp,-1,sizeof(dp));
memset(dp2,-1,sizeof(dp2));
int Test=read();
while(Test--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 56ms
memory: 97408kb
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: 175ms
memory: 97268kb
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