QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#56958 | #2519. Number with Bachelors | Sa3tElSefr | TL | 85ms | 6716kb | C++20 | 3.8kb | 2022-10-22 06:11:42 | 2022-10-22 06:11:43 |
Judging History
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[(1 << 16) + 2][2][2];
int mark[(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[mask][taken][g];
int& M = mark[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;
}
string mx[20];
ll getAns(string t, char c) {
id++;
curDig = 10;
if(c == 'h') curDig = 16;
if(t.size() > curDig) {
t = mx[curDig];
}
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);
mx[10] = "9876543210";
mx[16] = "fedcba9876543210";
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
ll ans = getAns(r, c) - getAns(l, c) + tmam(l);
if(c == 'h') cout << getBase(ans, 16);
else cout << ans;
cout << '\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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 85ms
memory: 6716kb
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
Time Limit Exceeded
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...