恰逢 HH国国庆,国王邀请nn 位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这 nn 位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
structbigN { int d[10000] = {0}; int len = 0; booloperator < (bigN const &temp) const { if(this->len != temp.len) returnthis->len < temp.len; else for(int i = this->len - 1; i >= 0; i--) if(this->d[i] != temp.d[i]) returnthis->d[i] < temp.d[i]; } };
bigN mult(bigN a, int b); bigN divid(bigN a, int b); voidprintN(bigN a);
intmain(int argc, constchar *argv[]) { int N; cin >> N; minister M[N + 1]; M[0].left = M[0].right = M[0].total = 1; for(int i = 0; i <= N; i++) { cin >> M[i].left >> M[i].right; M[i].total = M[i].left * M[i].right; } sort(M + 1, M + N + 1);
bigN MAX, tot; tot.len = 1; tot.d[0] = 1; for(int i = 1; i <= N; i++) { tot = mult(tot, M[i - 1].left); bigN temp = divid(tot, M[i].right); if(MAX < temp) MAX = temp; }
printN(MAX);
return0; }
bigN mult(bigN a, int b) { bigN res; int carry = 0; for(int i = 0; i < a.len; i++) { int temp = a.d[i] * b + carry; res.d[res.len++] = temp % 10; carry = temp / 10; } while(carry) { res.d[res.len++] = carry % 10; carry /= 10; }
return res; }
bigN divid(bigN a, int b) { bigN res; res.len = a.len; int r = 0; for(int i = a.len - 1; i >= 0; i--) { r = r * 10 + a.d[i]; if(r < b) res.d[i] = 0; else { res.d[i] = r / b; r = r % b; } } while(res.len > 1 && res.d[res.len - 1] == 0) res.len--;
return res; }
voidprintN(bigN a) { for(int i = a.len - 1; i >= 0; i--) cout << a.d[i]; cout << endl; }