Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef std::pair<int, int> ipair;
- #define int long long
- #define pb push_back
- #define ff first
- #define ss second
- #define fr(i, j, k) for (int i = j; i < k; i++)
- #define cnt_ones(x) __builtin_popcount(x)
- #define all(x) x.begin(), x.end()
- #define mp make_pair
- #define mod 1000000007
- #define IOS \
- std::ios::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- 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;
- if (pow(j,len-1)<1e12){
- 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();
- }
- }
- }
- int32_t main() {
- IOS;
- init();
- return 0;
- }
Add Comment
Please, Sign In to add comment