Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) begin(x), end(x)
- using namespace std;
- using ll = long long;
- void InitIO(std::string name = "", std::string nameout = "") {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- if (name.size() && nameout.size()) {
- assert(freopen((name + ".txt").c_str(), "r", stdin));
- assert(freopen((nameout + ".txt").c_str(), "w", stdout));
- } else if (name.size()) {
- assert(freopen((name + ".in").c_str(), "r", stdin));
- assert(freopen((name + ".out").c_str(), "w", stdout));
- }
- }
- ll Stoll(string s) {
- if (s.size() == 0) return 0;
- return stoll(s);
- }
- ll mypow10(int x) {
- ll ans = 1;
- while (x--) {
- ans *= 10;
- }
- return ans;
- }
- string Solve(int u, vector<pair<ll, string>>& arr) {
- const int n = 1e4;
- vector<vector<char>> cmp(u + 1);
- string tmp;
- for (int i = 0; i < n; i++) {
- tmp += arr[i].second;
- }
- sort(all(tmp));
- tmp.erase(unique(all(tmp)), tmp.end());
- vector<int> mp(256);
- vector<char> rmp;
- for (int i = 0; i < int(tmp.size()); i++) {
- mp[tmp[i]] = i;
- rmp.push_back(tmp[i]);
- }
- vector<vector<double>> p(10, vector<double>(10));
- for (int i = 0; i < n; i++) {
- int k = arr[i].second.size();
- string s = to_string(arr[i].first);
- if (arr[i].first == -1) {
- char ch = arr[i].second[0];
- double prob = (mypow10(u) - mypow10(int(arr[i].second.size()))) * 1.0 / mypow10(u);
- for (int j = 1; j < 10; j++) {
- p[mp[ch]][j] += prob / 9;
- }
- vector<double> kek(10);
- for (int first = 1; first < 10; first++) {
- double lst = 0.5 / first;
- double sum = first - 1 + lst;
- for (int j = 1; j < first; j++) {
- kek[j] += 1.0 / sum;
- }
- kek[first] += lst / sum;
- }
- for (int j = 1; j < 10; j++) kek[j] /= 9;
- for (int j = 1; j < 10; j++) {
- p[mp[ch]][j] += kek[j];
- }
- } else {
- if (k < int(s.size())) {
- char ch = arr[i].second[0];
- for (int j = 1; j < 10; j++) {
- p[mp[ch]][j] += 1.0 / 9;
- }
- } else {
- char ch = arr[i].second[0];
- int first = s[0] - '0';
- double lst = 1.0 * (Stoll(s.substr(1, int(s.size()) - 1)) + 1) / arr[i].first;
- double sum = first - 1 + lst;
- vector<double> kek(first + 1);
- for (int j = 1; j < first; j++) {
- kek[j] = 1.0 / sum;
- }
- kek[first] = lst / sum;
- for (int j = 1; j <= first; j++) {
- p[mp[ch]][j] += kek[j];
- }
- }
- }
- }
- int zero = 0;
- for (int i = 0; i < 10; i++) {
- if (accumulate(all(p[i]), 0.0) == 0) {
- zero = i;
- break;
- }
- }
- string ans = "";
- ans.push_back(tmp[zero]);
- vector<int> ind;
- for (int i = 0; i < 10; i++) {
- if (i == zero) continue;
- ind.push_back(i);
- }
- double cur = 0;
- string suff;
- do {
- double val = 0;
- for (int i = 0; i < int(ind.size()); i++) {
- val += p[ind[i]][i + 1];
- }
- if (val > cur) {
- cur = val;
- suff = "";
- for (int i = 0; i < int(ind.size()); i++) {
- suff.push_back(tmp[ind[i]]);
- }
- }
- } while (next_permutation(all(ind)));
- return ans + suff;
- }
- void Solve() {
- int t;
- cin >> t;
- for (int test = 1; test <= t; test++) {
- int u;
- cin >> u;
- vector<pair<ll, string>> arr;
- for (int i = 0; i < 1e4; i++) {
- ll a;
- string b;
- cin >> a >> b;
- arr.emplace_back(a, b);
- }
- string res = Solve(u, arr);
- cout << "Case #" << test << ": " << res << '\n';
- }
- }
- int main() {
- InitIO();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement