QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#255722#2519. Number with Bachelorstpc_icpc_n#WA 366ms3528kbC++203.2kb2023-11-18 16:51:272023-11-18 16:51:28

Judging History

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

  • [2023-11-18 16:51:28]
  • 评测
  • 测评结果:WA
  • 用时:366ms
  • 内存:3528kb
  • [2023-11-18 16:51:27]
  • 提交

answer

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <ranges>
#include <sstream>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

using i64 = long long;
using u64 = std::uint64_t;

std::vector<u64> fact;

u64 npk(int n, int k) {
    return fact[n] / fact[n - k];
}
u64 counting(std::vector<int> b, int D) {
    u64 ans = 0;
    if(b.size() <= D) {
        std::vector<int> vis(D);
        for(int i = 0; i < b.size(); i++) {
            u64 n = b[i];
            u64 c = 0;
            for(int j = (i == 0 ? 1 : 0); j < n; j++) {
                if(!vis[j]) c++;
            }
            ans += c * npk(D - i - 1, b.size() - i - 1);
            if(i > 0) {
                ans += (D - 1) * npk(D - 1, b.size() - 1 - i);
            }
            if(vis[n]) break;
            vis[n] = 1;
            if(i + 1 == b.size()) ans++;
        }
    }
    else {
        for(int i = 1; i <= D; i++) {
            ans += (D - 1) * npk(D - 1, i - 1);
        }
    }
    ans++;
    return ans;
}

std::vector<int> conv(const std::string& a) {
    if(a == "0") {
        return {};
    }
    std::vector<int> x(a.size());
    for(int i = 0; i < a.size(); i++) {
        if('a' <= a[i] && a[i] <= 'f') {
            x[i] = (10 + a[i] - 'a');
        }
        else {
            x[i] = a[i] - '0';
        }
    }
    return x;
}

std::vector<int> vms(std::vector<int> x, int D) {
    for(int i = x.size(); i --> 0;) {
        if(x[i] > 0) {
            x[i]--;
            break;
        }
        x[i] = D - 1;
    }
    if(x.front() == 0) {
        x.erase(x.begin());
    }
    return x;
}

void solve() {
    char c;
    std::cin >> c;
    int mode = 0;
    int D = c == 'h' ? 16 : 10;
    std::cin >> mode;
    if(mode == 0) {
        std::string a, b;
        std::cin >> a >> b;
        auto x = conv(a);
        auto y = conv(b);
        u64 p = (a == "0" ? 0 : counting(vms(x, D), D));
        u64 q = counting(y, D);
        if(c == 'h') std::cout << std::hex;
        std::cout << q - p << std::dec << std::endl;
    }
    else {
        
        std::string a;
        std::cin >> a;
        auto x = conv(a);
        u64 obj = 0;
        for(u64 c: x) {
            obj = obj * D + c;
        }

        if(obj == 1) {
            std::cout << 0 << std::endl;
            return;
        }
        u64 ok = ~(u64(0));
        u64 ng = 0;
        while((ok - ng) > 1) {
            u64 mid = ng + (ok - ng) / 2;
            std::stringstream ss;
            if(c == 'h') ss << std::hex;
            ss << mid;
            u64 p = counting(conv(ss.str()), D);
            if(p >= obj) {
                ok = mid;
            }
            else {
                ng = mid;
            }
        }
        if(ok == ~0) {
            std::cout << "-" << std::endl;
        }
        else {
            if(c == 'h') std::cout << std::hex;
            std::cout << ok << std::dec << std::endl;
        }
    }
}

int main() {
    fact.resize(20);
    fact[0] = 1;
    for(int i = 1; i < 20; i++) {
        fact[i] = fact[i - 1] * i;
    }
    int T;
    std::cin >> T;
    while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3528kb

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
Wrong Answer
time: 366ms
memory: 3516kb

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
6700
7710
953
3
7eff9cb8680
26054
3
1356
-
946700
4681
40
387
-
-
-
492000
0
1
29
-
4605700
1
1
-
15b4
175f
6c01968e400
124b7
6279
9
6270
-
f808cd4000
5ec45a47e00
146ce
2a64
-
0
-
7
d
6
2041
-
19ce43d0b80
0
5
9158
16780000
1389
116
0
-
3bc31189480
415
67538
7
-
-
1e6
0
0
48b6
9
-
2b0
1844674...

result:

wrong answer 3rd lines differ - expected: '6587', found: '6700'