QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#569891#9107. Zayin and CountSGColin#AC ✓27ms3764kbC++171.7kb2024-09-17 11:53:442024-09-17 11:53:45

Judging History

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

  • [2024-09-17 11:53:45]
  • 评测
  • 测评结果:AC
  • 用时:27ms
  • 内存:3764kb
  • [2024-09-17 11:53:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

inline ll rd() {
	ll x = 0;
	bool f = 0;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) f |= (c == '-');
	for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
	return f ? -x : x;
}

#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

const int N = 100007;

char s[N];

ull fpow(ull x, int t) {
	ull res = 1;
	for (; t; t >>= 1, x = x * x)
		if (t & 1) res = res * x;
	return res;
}

void work() {
	vector<int> A, B;
	rep(i, 0, 9) {int w = rd(); if (w) A.eb(i);}
	rep(i, 0, 9) {int w = rd(); if (w) B.eb(i);}
	scanf("%s", s + 1);
	int n = strlen(s + 1);
	auto getv = [&](vector<int> &s, int w) {
		return lower_bound(all(s), w) - s.begin() + 1;
	};
	ull tot = 0;
	if (A[0] != 0) {
		int w = getv(A, s[1] - '0');
		rep(i, 1, n - 1) tot += fpow(A.size(), i);
	}
	rep(i, 1, n) {
		int w = getv(A, s[i] - '0');
		tot += (w - 1) * fpow(A.size(), n - i);
	}
	++tot;

	vector<int> ans;

	if (B[0] == 0) {
		--tot;
		if (tot == 0) ans.eb(B[0]);
		while (tot) {
			ans.eb(B[tot % B.size()]);
			tot /= B.size();
		}
		reverse(all(ans));
	} else {
		int pos = 1;
		while (true) {
			ull cntl = fpow(B.size(), pos);
			if (tot > cntl) tot -= cntl;
			else break;
			++pos;
		}
		per(i, pos, 1) {
			ull cntl = fpow(B.size(), i - 1);
			int cur = (tot - 1) / cntl;
			ans.eb(B[(tot - 1) / cntl]);
			tot -= cntl * cur;
		}
	}
	for (auto x : ans) printf("%d", x); puts("");
}

int main() {
	per(t, rd(), 1) work();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 27ms
memory: 3764kb

input:

10000
1 0 0 0 1 1 0 0 0 1
0 0 1 0 1 1 1 1 0 0
950595954440050004054505054050
1 0 0 0 1 1 1 1 0 0
1 1 1 0 1 0 0 0 1 1
45467007076660767550460064
1 1 1 1 0 0 0 1 0 0
1 1 0 1 1 0 1 0 0 1
23373171320213300170200722
0 0 0 0 1 1 1 0 1 0
0 0 1 0 0 1 0 1 1 1
558565664666565565558468668484
1 1 0 0 1 0 1 0 1 ...

output:

52755244567262766742575722
41990991999414091249949
101364364636933104003903
57558888789255872922852552
757222758857875785288225787822
761161760076076167101117776167
56666586555668686566656586856566686658
15611661611611111511116116661611616155
505885888775005550558080707878
3912911219633669993999199
...

result:

ok 10000 lines