QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#585663 | #2519. Number with Bachelors | CSQ | TL | 0ms | 3504kb | C++23 | 4.1kb | 2024-09-23 21:42:16 | 2024-09-23 21:42:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
typedef unsigned long long int ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
ll P[20][20];
ll find(ll n){
if(n==0)return 1;
vector<int>v;
while(n){
v.push_back(n%10);
n/=10;
}
reverse(all(v));
ll ans = 1;
vector<int>seen(10,0);
int choice = 10;
for(int i=0;i<sz(v);i++){
for(int j=1;j<v[i];j++){
if(!seen[j])ans += P[choice-1][sz(v)-i-1];
}
if(!seen[0]){
if(i>0 && v[i]>0)ans += P[choice-1][sz(v)-i-1];
}
seen[v[i]]++;
choice--;
if(seen[v[i]]>1)break;
if(i==sz(v)-1)ans++;
}
for(int i=1;i<sz(v);i++){
ans += P[10][i] - P[9][i-1];
}
return ans;
}
ll find2(ll n){
if(n==0)return 1;
vector<int>v;
while(n){
v.push_back(n%16);
n/=16;
}
reverse(all(v));
ll ans = 1;
vector<int>seen(16,0);
int choice = 16;
for(int i=0;i<sz(v);i++){
for(int j=1;j<v[i];j++){
if(!seen[j])ans += P[choice-1][sz(v)-i-1];
}
if(!seen[0]){
if(i>0 && v[i]>0)ans += P[choice-1][sz(v)-i-1];
}
seen[v[i]]++;
choice--;
if(seen[v[i]]>1)break;
if(i==sz(v)-1)ans++;
}
for(int i=1;i<sz(v);i++){
ans += P[16][i] - P[15][i-1];
}
return ans;
}
ll condec(string s0){
ll a = 0;
for(ll i=sz(s0)-1,j=1;;i--){
if(s0[i]>='a' && s0[i]<='f')a += j * (s0[i]-'a'+10);
else a += j * (s0[i]-'0');
j*=16;
if(i==0)break;
}
return a;
}
string conhex(ll n){
if(n==0)return "0";
string s;
while(n){
int r = n%16;
if(r<=9)s.push_back(r + '0');
else s.push_back(r-10+'a');
n/=16;
}
reverse(all(s));
return s;
}
//10P1 - 9P0 = 9
//+2
//10P1 + 10P2
int main()
{
//cin.tie(0)->sync_with_stdio(0);
//cin.exceptions(cin.failbit);
P[0][0] = 1;
for(int i=0;i<=16;i++){
P[i][0] = 1;
ll f = 1;
for(int j=i;j>=1;j--){
f *= j;
P[i][i-j+1] = f;
}
}
int t;
cin>>t;
while(t--){
char c;
int mode;
cin>>c>>mode;
if(c == 'd'){
if(!mode){
ll a,b;
cin>>a>>b;
if(a==0)cout<<find(b)<<'\n';
else cout<<find(b)-find(a-1)<<'\n';
}else{
ll n;
cin>>n;
ll l = 0,r = 1e11;
while(r>=l){
ll mid = (l+r)/2;
//cout<<mid<<" "<<find(mid)<<'\n';
if(find(mid) >= n)r = mid-1;
else l = mid+1;
}
if(find(l) != n)cout<<"-"<<'\n';
else cout<<l<<'\n';
}
}else{
if(!mode){
string s0,s1;
cin>>s0>>s1;
ll a = condec(s0),b = condec(s1);
ll ans = 0;
if(a==0)ans = find2(b);
else ans = find2(b) - find2(a-1);
cout<<conhex(ans)<<'\n';
}else{
string s0;
cin>>s0;
ll n = condec(s0);
//cout<<s0<<" "<<n<<'\n';
ll l = 0,r = 1e11;
while(r>=l){
ll mid = (l+r)/2;
//cout<<mid<<" "<<find(mid)<<'\n';
if(find2(mid) >= n)r = mid-1;
else l = mid+1;
}
if(find2(l) != n)cout<<"-"<<'\n';
else cout<<conhex(l)<<'\n';
}
}
}
}
/*
6
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3504kb
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: -100
Time Limit Exceeded
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