QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#719308 | #2519. Number with Bachelors | yuto1115 | AC ✓ | 17ms | 3880kb | C++20 | 3.3kb | 2024-11-07 00:10:50 | 2024-11-07 00:10:51 |
Judging History
answer
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <bits/stdc++.h>
const int N = 25;
int n, base[2] = {10, 16};
__int128 dp[N][N], leqlen[N][N];
int fromchar(char c) {
if (c <= '9' && c >= '0') return c - '0';
else return c - 'a' + 10;
}
char tochar(int c) {
if (c >= 10) return 'a' + c - 10;
return '0' + c;
}
void outhex(__int128 x) {
if (!x) {
printf("0\n");
return;
}
std::vector<int> repr;
while (x) {
repr.push_back(x % 16);
x /= 16;
}
for (int i = (int)repr.size() - 1; i >= 0; --i) {
int c = repr[i];
if (c < 10) printf("%d", c);
else printf("%c", 'a' + c - 10);
}
printf("\n");
}
__int128 readhex() {
char s[N];
scanf("%s", s);
__int128 r = 0;
for (int i = 0; s[i]; ++i) {
r *= 16;
r += fromchar(s[i]);
}
return r;
}
__int128 f(const char *s, int b, bool last) {
if (s[0] == '0') return last;
int len = strlen(s), avail = base[b];
bool u[N] = {0};
__int128 ans = leqlen[len - 1][avail];
for (int i = 0; s[i]; ++i) {
int c = fromchar(s[i]);
if (avail - i - 1 < 0) return ans;
for (int j = 0 + (!i); j < c; ++j) if (!u[j]) ans += dp[len - i - 1][avail - i - 1];
if (u[c]) return ans;
u[c] = true;
}
return ans + (__int128)last;
}
std::string solve(__int128 ind, bool b) {
std::string ans;
if (ind == 1) {
ans.push_back('0');
return ans;
}
int len, avail = base[b];
--ind;
for (len = 0; len < N && leqlen[len][avail] <= ind; ++len);
if (len == N) {
ans.push_back('-');
return ans;
}
ind -= leqlen[len - 1][avail];
bool u[N] = {0};
for (int i = 0; i < len; ++i) {
int j = 0;
if (!i) ++j;
--avail;
for (; dp[len - i - 1][avail] <= ind; ++j) {
if (!u[j]) ind -= dp[len - i - 1][avail];
}
while (u[j]) ++j;
ans.push_back(tochar(j));
u[j] = 1;
}
return ans;
}
#include <random>
#include <cassert>
/*
void solve1(__int128 ind, bool b) {
__int128 l = 0, r = (__int128)0 - 1;
printf("rmost %llu\n", r);
while (l < r) {
__int128 m = l + (r - l) / 2;
if (f(std::to_string(m).c_str(), 0,
}
}
*/
__int128 to_int(std::string s, bool b) {
__int128 r = 0;
for (int i = 0; s[i]; ++i) {
r *= base[b];
r += fromchar(s[i]);
}
return r;
}
int main() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (!i) dp[i][j] = leqlen[i][j] = 1;
else {
if (j) dp[i][j] = j * dp[i - 1][j - 1];
leqlen[i][j] = leqlen[i - 1][j] + (j ? dp[i - 1][j - 1] * (j - 1) : 0);
}
}
}
scanf("%d", &n);
/*
std::mt19937_64 rng(228);
while (n--) {
__int128 x = rng() % 1000 + 2;
std::string str = solve(x, 0);
__int128 val = to_int(str, 0);
if (str == "-") continue;
if (f(std::to_string(val - 1).c_str(), 0, 1) != x - 1) {
printf("x is %lld\n", x);
printf("got str %s\n", str.c_str());
printf("f returned %llu\n", f(std::to_string(val - 1).c_str(), 0, 1));
}
}
return 0;
*/
while (n--) {
int type;
char b;
scanf(" %c%d", &b, &type);
b = b == 'd' ? 0 : 1;
if (type == 0) {
char l[N], r[N];
scanf("%s%s", l, r);
unsigned long long ans = f(r, b, 1) - f(l, b, 0);
if (b == 1) outhex(ans);
else printf("%llu\n", ans);
} else {
__int128 ind;
unsigned long long ind2;
if (b == 1) ind = readhex();
else{
scanf("%llu", &ind2);
ind = ind2;
}
printf("%s\n", solve(ind, b).c_str());
//solve1(ind, b);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3792kb
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: 17ms
memory: 3880kb
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