QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#722022#2519. Number with Bachelorsucup-team5062#AC ✓53ms3604kbC++203.9kb2024-11-07 17:28:552024-11-07 17:28:55

Judging History

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

  • [2024-11-07 17:28:55]
  • 评测
  • 测评结果:AC
  • 用时:53ms
  • 内存:3604kb
  • [2024-11-07 17:28:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

unsigned long long Count10[19];
unsigned long long Count16[19];

void Initialize() {
	for (int i = 1; i <= 10; i++) {
		unsigned long long sum = 1;
		for (int j = 1; j <= i; j++) {
			unsigned long long x = (j == i ? 9 : 10 - (i - j));
			sum *= x;
		}
		Count10[i] = Count10[i - 1] + sum;
	}
	for (int i = 1; i <= 16; i++) {
		unsigned long long sum = 1;
		for (int j = 1; j <= i; j++) {
			unsigned long long x = (j == i ? 15 : 16 - (i - j));
			sum *= x;
		}
		Count16[i] = Count16[i - 1] + sum;
	}
	return;
}

char Convert(int x) {
	if (x >= 10) return ('a' + (x - 10));
	return ('0' + x);
}

unsigned long long Convert2(char chr) {
	if (chr >= '0' && chr <= '9') return (chr - '0');
	return 10 + (chr - 'a');
}

unsigned long long Integer(string str, unsigned long long d) {
	unsigned long long s = 0;
	for (int i = 0; i < str.size(); i++) {
		s *= d;
		s += Convert2(str[i]);
	}
	return s;
}

string String(unsigned long long intg, unsigned long long d) {
	if (intg == 0) return "0";
	string str = "";
	while (intg > 0) {
		str += Convert(intg % d);
		intg /= d;
	}
	reverse(str.begin(), str.end());
	return str;
}


// ====================================================================== Answer Query 1
string Query1(unsigned long long order, int d) {
	if (order == 1) return "0";
	order--;

	// Simulation
	for (int i = 1; i <= d; i++) {
		if (d == 10 && order > Count10[i]) continue;
		if (d == 16 && order > Count16[i]) continue;
		if (d == 10) order -= (Count10[i - 1] + 1);
		if (d == 16) order -= (Count16[i - 1] + 1);

		// Get Answer
		vector<long long> vec(i, 0);
		vector<bool> used(d, false);
		for (int j = 0; j < i; j++) {
			unsigned long long x = (j == i - 1 ? d - 1 : d + 1 - (i - j));
			vec[j] = (order % x);
			order /= x;
		}

		// Get String
		string str = "";
		for (int j = i - 1; j >= 0; j--) {
			int cnt = 0;
			for (int k = 0; k < d; k++) {
				if (j == i - 1 && k == 0) continue;
				if (used[k] == true) continue;
				if (cnt == vec[j]) {
					str += Convert(k);
					used[k] = true;
					break;
				}
				cnt++;
			}
		}
		return str;
	}
	return "-";
}


// ====================================================================== Answer Query 0
bool Check(string str) {
	sort(str.begin(), str.end());
	for (int i = 0; i < (int)str.size() - 1; i++) {
		if (str[i] == str[i + 1]) return false;
	}
	return true;
}

unsigned long long Query0(string str, int d) {
	if (str == "0") return 0;

	// Solve Init
	unsigned long long Answer = 0;
	if (d == 10) Answer += Count10[str.size() - 1] + 1;
	if (d == 16) Answer += Count16[str.size() - 1] + 1;

	// Solve Main
	vector<bool> used(d, false);
	for (int i = 0; i < str.size(); i++) {
		int x = Convert2(str[i]);
		int cnt = 0;
		for (int j = 0; j < x; j++) {
			if (j == 0 && i == 0) continue;
			if (used[j] == false) cnt++;
		}

		// Get Numways
		unsigned long long ushiro = 1;
		for (int j = i + 1; j < (int)str.size(); j++) ushiro *= (unsigned long long)(d - j);

		// First Case
		if (used[x] == true) {
			Answer += 1ULL * cnt * ushiro;
			break;
		}
		else {
			Answer += 1ULL * cnt * ushiro;
			used[x] = true;
		}
	}
	return Answer;
}


// ====================================================================== Main
int main() {
	Initialize();
	int T; cin >> T;
	for (int t = 1; t <= T; t++) {
		char kata; int typ; cin >> kata >> typ;
		if (typ == 0) {
			string S, T; cin >> S >> T;
			unsigned long long v1 = Query0(S, (kata == 'd' ? 10 : 16));
			unsigned long long v2 = Query0(T, (kata == 'd' ? 10 : 16));
			unsigned long long ans = v2 - v1;
			if (Check(T) == true) ans += 1;
			cout << String(ans, (kata == 'd' ? 10 : 16)) << endl;
		}
		if (typ == 1) {
			string S; cin >> S;
			unsigned long long Order = Integer(S, (kata == 'd' ? 10 : 16));
			cout << Query1(Order, (kata == 'd' ? 10 : 16)) << endl;
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 53ms
memory: 3540kb

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