Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<int> a;
- int val(char c) {
- if (c >= '0' && c <= '9')
- return (int)c - '0';
- else
- return (int)c - 'A' + 10;
- }
- void init() {
- int T;
- cin >> T;
- while (T--) {
- int N, B, ans = -1;
- cin >> N;
- string s;
- for (int i = 1; i <= N; i++) {
- cin >> B >> s;
- if (B != -1) {
- int len = s.size();
- int power = 1;
- int num = 0;
- int i;
- for (i = len - 1; i >= 0; i--) {
- num += val(s[i]) * power;
- power = power * B;
- if (num > 1e12)
- break;
- }
- if(i==-1) ans = num;
- } else {
- int maxi = -1;
- for (int i = s.size() - 1; i >= 0; i--)
- maxi = max(val(s[i]), maxi);
- if (s.size() == 1)
- a.pb(val(s[s.size() - 1]));
- for (int j = maxi + 1; j <= 36; j++) {
- int len = s.size();
- int power = 1;
- int num = 0;
- int i;
- for (i = len - 1; i >= 0; i--) {
- num += val(s[i]) * power;
- power = power * j;
- if (num > 1e12)
- break;
- }
- if(i==-1) a.pb(num);
- }
- }
- }
- if (ans != -1)
- cout << ans << endl;
- else {
- int flag = 0, num;
- for (int i = 0; i < a.size(); i++) {
- int cnt = count(a.begin(), a.end(), a[i]);
- if (cnt == N) {
- num = a[i];
- flag = 1;
- break;
- }
- }
- if (flag == 1)
- cout << num << endl;
- else
- cout << -1 << endl;
- a.clear();
- }
- }
- }
Add Comment
Please, Sign In to add comment