QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#574411 | #2519. Number with Bachelors | lifan | AC ✓ | 1186ms | 23328kb | C++14 | 2.7kb | 2024-09-18 22:00:42 | 2024-09-18 22:00:46 |
Judging History
answer
//
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
using namespace std;
int base; bool type;
void read(ull& x) { x = 0;
string s; cin >> s;
for (char c : s) {
if (c >= 'a')
x = x*base+10+(c-'a');
else
x = x*base+(c-'0');
}
}
string write(ull x) {
// cout << "!" << x << "!" << '\n';
string s;
while (s.empty() || x) {
if (x%base>9)
s += char('a'+(x%base)-10);
else
s += char('0'+x%base);
x /= base;
}
reverse(s.begin(), s.end());
return s;
}
int a[25], len;
ull dp[2][25][1<<16]; bool vis[2][25][1<<16];
ull dfs(int pos, int sta, bool lim, bool qdl) {
// 10 ~ 1f
// 16 ~ 31
if (!pos) return 1;
ull& di = dp[type][pos][sta];
bool& vi = vis[type][pos][sta];
if (!lim && !qdl && vi) return di;
int up = (lim?a[pos]:(base-1)); ull res = 0;
for (int i = 0; i <= up; i++) {
if ((sta&(1<<i)) || (qdl&&(i==0)))
res += dfs(pos-1, (qdl&&(i==0))?sta:(sta^(1<<i)), lim&&(i==up), qdl&&(i==0));
}
if (!lim && !qdl) vi=1, di=res; return res;
}
ull solve(ull x, bool f) {
// cout << "solving:" << x << ' ' << f << '\n';
if (x==0) return f; x -= (!f);
len=0; while (x) {
a[++len] = x%base;
x/=base;
}
// cout << dfs(len, (1<<base)-1, 1, 1) << '\n';
return dfs(len, (1<<base)-1, 1, 1);
}
int main() {
// g++ code.cpp -o code.exe -std=c++14 -O2 -fno-ms-extensions -DLOCAL; ./code
time_t st = clock();
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("ans.out", "w", stdout);
#else
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio();
cin.tie(0), cout.tie(0);
#endif
int T, op; char dt;
ull l, r, k;
cin >> T; while (T--) {
cin >> dt >> op;
if (dt == 'd') base = 10, type = 0;
if (dt == 'h') base = 16, type = 1;
if (op == 0) {
read(l); read(r);
cout << write(solve(r,1)-solve(l,0)) << '\n';
}
if (op == 1) {
read(k);
ull l = 0, r, mid;
if (base==10) r = 987654321;
if (base==16) r = 1147797409030816545;
while (l < r) {
mid = l + r >> 1;
if (solve(mid, 1) >= k) r = mid;
else l = mid + 1;
}
if (solve(r, 1) < k) cout << '-' << '\n';
else cout << write(r) << '\n';
}
}
cerr << "Exec Time:" << (double)(clock()-st)/CLOCKS_PER_SEC << "s" << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 39ms
memory: 16976kb
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: 1186ms
memory: 23328kb
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