QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#56955#2519. Number with BachelorsSa3tElSefrWA 619ms39404kbC++203.6kb2022-10-22 06:01:552022-10-22 06:01:57

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-22 06:01:57]
  • 评测
  • 测评结果:WA
  • 用时:619ms
  • 内存:39404kb
  • [2022-10-22 06:01:55]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>

#define ll long long
#define ld  double
using namespace std;

const int N = 10000 + 5, mod = 998244353;

/// 9876543210

ll dp[17][(1 << 16) + 2][2][2];
int mark[17][(1 << 16) + 2][2][2], id, curDig, n;


string s;

int f(char c) {
    if(c >= '0' && c <= '9') return c - '0';
    return c - 'a' + 10;
}



ll solve(int idx, int mask, bool taken, bool g) {
    if(idx == n) return 1;
    ll &ret = dp[idx][mask][taken][g];
    int& M = mark[idx][mask][taken][g];
    if(M == id) return ret;
    M = id;

    ll ans = 0;
    for(int bit = 0; bit < curDig; bit++) {
        if((g == 0) && bit > f(s[idx])) break;
        if(mask >> bit & 1 ^ 1) {
           int newMask = mask ^ (1 << bit);
           if(bit == 0 && taken == 0) newMask = mask;
           ans += solve(idx + 1, newMask, taken, g | (bit < f(s[idx])));
        }
    }
    return ret = ans;
}


char toChar(int c) {
    if(c >= 0 && c <= 9) return (char)(c + '0');
    return (char)(c - 10 + 'a');
}


string getBase(unsigned ll n, int base) {
    string ret = "";
    while(n > 0) {
        ret.push_back(toChar(n % base));
        n /= base;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}


ll getAns(string t, char c) {
    id++;
    curDig = 10;
    if(c == 'h') curDig = 16;
    s = t;
    n = s.size();
    return solve(0, 0, 0, 0);
}

bool tmam(string s) {
    map<char, bool> seen;
    for(auto i: s) {
        if(seen[i]) return 0;
        seen[i] = 1;
    }
    return 1;
}


unsigned ll toBase10(string s, int base) {
    unsigned ll ret = 0;
    for(auto i: s) {
        ret = ret * base + f(i);
    }
    return ret;
}



void solveBase10(ll n) {
    if(n > 58941091) {
        cout << "-\n";
        return;
    }

    ll low = 0, high = 9876543210, ans;
    while(high >= low) {
        ll mid = low + high >> 1;
        string t = to_string(mid);
        if(getAns(t, 'd') >= n) {
            ans = mid;
            high = mid - 1;
        }
        else low = mid + 1;
    }
    cout << ans << '\n';
}

void solveBase16(unsigned ll n) {
    if(n > 1290434218669921) {
        cout << "-\n";
        return;
    }
    unsigned ll low = 0, high = toBase10("fdecba9876543210", 16), ans;
    while(high >= low) {
        unsigned ll mid = low + high >> 1;
        string t = getBase(mid, 16);
        if(getAns(t, 'h') >= n) {
            ans = mid;
            high = mid - 1;
        }
        else low = mid + 1;
    }
    cout << getBase(ans, 16) << '\n';

}

void solve(string s, char c) {
    int base = 10;
    if(c == 'h') base = 16;
    if((int) s.size() > base) {
        cout << "-\n";
        return;
    }
    ll cur = toBase10(s, base);
    if(base == 10) solveBase10(cur);
    else solveBase16(cur);

}



// fedcba9876543210

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);


    int tt;
    cin >> tt;
    while(tt--) {
        char c;
        string l, r;
        int op;
        cin >> c >> op;
        if(op == 0) {
            cin >> l >> r;
            /// check at L
            cout << getAns(r, c) - getAns(l, c) + tmam(l) << '\n';
        }
        else {
            string idx;
            cin >> idx;
            solve(idx, c);
        }
    }


    return 0;
}

/*

2
d 0 0 9876543210
h 0 0 fedcba9876543210

6
d 0 10 20
h 0 10 1f
d 1 10
h 1 f
d 1 1000000000
h 1 ffffffffffffffff10

 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 619ms
memory: 39404kb

input:

6
d 0 10 20
h 0 10 1f
d 1 10
h 1 f
d 1 1000000000
h 1 ffffffffffffffff

output:

10
15
9
e
-
-

result:

wrong answer 2nd lines differ - expected: 'f', found: '15'