QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#573811#2519. Number with BachelorslifanAC ✓169ms24112kbC++142.3kb2024-09-18 20:02:292024-09-18 20:02:30

Judging History

你现在查看的是最新测评结果

  • [2024-09-18 20:02:30]
  • 评测
  • 测评结果:AC
  • 用时:169ms
  • 内存:24112kb
  • [2024-09-18 20:02:29]
  • 提交

answer

// 
#include <bits/stdc++.h>
#define int long long
typedef unsigned int ui;
using namespace std;
constexpr int Mod = 1e9 + 7;

int B, a[60], cnt, ct, rk[2521], dp[2][20][1 << 16];
int SBQOJ;

int dfs(int p, bool lim, int s) {
    if (!p) return 1;
    if (!lim && ~dp[B == 10][p][s]) return dp[B == 10][p][s];
    int rs(0), up(lim ? a[p] : B - 1);
    for (int i = 0; i <= up; i++) if (!(s >> i & 1)) rs += dfs(p - 1, lim && i == up, !s && !i ? 0 : (s | 1 << i));
    if (!lim) dp[B == 10][p][s] = rs;
    return rs;
}
ui Get(ui x) {
    cnt = 0;
    while (x) a[++cnt] = x % B, x /= B;
    return dfs(cnt, 1, 0);
}
void read(ui &x) {
    if (B == 10) cin >> x;
    else {
        x = 0;
        string s;
        cin >> s;
        for (char c : s) {
            if (c >= '0' && c <= '9') x = x * B + (c ^ 48);
            else x = x * B + c - 'a' + 10;
        }
    }
}
void write(ui x) {
    if (!x) return cout << "0\n", void();
    if (B == 10) cout << x << '\n';
    else {
        vector<int> pc;
        while (x) pc.push_back(x % B), x /= B;
        for (auto it = pc.rbegin(); it != pc.rend(); it++) {
            if (*it >= 10) cout << (char)(*it - 10 + 'a');
            else cout << *it;
        }
        cout << '\n';
    }
}
void solve() {
    ++SBQOJ;
    char c;
    int ty;
    cin >> c >> ty;
    // if (SBQOJ == 21) cout << SBQOJ << ' ' << "asfd " << c << ' ' << ty << '\n';
    if (c == 'd') B = 10;
    else B = 16;
    if (ty == 0) {
        ui l, r;
        read(l), read(r);
        // cerr << Get(r) << '\n';
        write(Get(r) - (l == 0 ? 0 : Get(l - 1)));
    } else {
        ui x;
        read(x);
        if (x < 10) return cout << x - 1 << '\n', void();
        ui l = 0, r = -1;
        if (Get(r) < x) return cout << '-' << '\n', void();
        while (l < r) {
            int mid(l + r >> 1);
            if (Get(mid) >= x) r = mid;
            else l = mid + 1;
        }
        write(l);
    }
    // if (SBQOJ == 21) cout << SBQOJ << ' ' << "asfd " << c << ' ' << ty << '\n';
}

signed main() {
#ifdef LARRIX
    freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    memset(dp, -1, sizeof dp);
    int T; cin >> T;
    while (T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 39ms
memory: 24080kb

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: 169ms
memory: 24112kb

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