結果
問題 | No.2066 Simple Math ! |
ユーザー | drken1215 |
提出日時 | 2022-12-26 18:41:30 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 474 ms / 2,000 ms |
コード長 | 2,778 bytes |
コンパイル時間 | 2,152 ms |
コンパイル使用メモリ | 202,892 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-05-01 03:50:50 |
合計ジャッジ時間 | 14,401 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 122 ms
6,812 KB |
testcase_02 | AC | 165 ms
6,940 KB |
testcase_03 | AC | 57 ms
6,944 KB |
testcase_04 | AC | 468 ms
6,944 KB |
testcase_05 | AC | 470 ms
6,944 KB |
testcase_06 | AC | 467 ms
6,940 KB |
testcase_07 | AC | 470 ms
6,944 KB |
testcase_08 | AC | 469 ms
6,940 KB |
testcase_09 | AC | 467 ms
6,940 KB |
testcase_10 | AC | 466 ms
6,940 KB |
testcase_11 | AC | 474 ms
6,940 KB |
testcase_12 | AC | 468 ms
6,940 KB |
testcase_13 | AC | 474 ms
6,944 KB |
testcase_14 | AC | 416 ms
6,940 KB |
testcase_15 | AC | 418 ms
6,944 KB |
testcase_16 | AC | 418 ms
6,944 KB |
testcase_17 | AC | 426 ms
6,944 KB |
testcase_18 | AC | 417 ms
6,940 KB |
testcase_19 | AC | 270 ms
6,944 KB |
testcase_20 | AC | 274 ms
6,940 KB |
testcase_21 | AC | 268 ms
6,944 KB |
testcase_22 | AC | 270 ms
6,940 KB |
testcase_23 | AC | 270 ms
6,944 KB |
testcase_24 | AC | 422 ms
6,944 KB |
testcase_25 | AC | 421 ms
6,944 KB |
testcase_26 | AC | 420 ms
6,940 KB |
testcase_27 | AC | 421 ms
6,940 KB |
testcase_28 | AC | 422 ms
6,940 KB |
testcase_29 | AC | 104 ms
6,940 KB |
testcase_30 | AC | 60 ms
6,944 KB |
testcase_31 | AC | 78 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; // sum_{i=0}^{n-1} floor((a * i + b) / m) // O(log(n + m + a + b)) // __int128 can be used for T template<class T> T floor_sum(T n, T a, T b, T m) { if (n == 0) return 0; T res = 0; if (a >= m) { res += n * (n - 1) * (a / m) / 2; a %= m; } if (b >= m) { res += n * (b / m); b %= m; } if (a == 0) return res; T ymax = (a * n + b) / m, xmax = ymax * m - b; if (ymax == 0) return res; res += (n - (xmax + a - 1) / a) * ymax; res += floor_sum(ymax, m, (a - xmax % a) % a, a); return res; } using pint = pair<int, int>; using pll = pair<long long, long long>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define REP(i, n) for (long long i = 0; i < (long long)(n); ++i) #define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, deque<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template<class T> ostream& operator << (ostream &s, set<T> P) { for(auto it : P) { s << "<" << it << "> "; } return s; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; } // calc #n that can be expressed n = Px + Qy (P, Q is coprime) // 0 <= n <= M long long calc_num(__int128 P, __int128 Q, __int128 M) { __int128 mp = M / P; __int128 N = min(mp + 1, Q); __int128 a = P, b = M + Q - a * (N - 1); return floor_sum(N, a, b, Q) - 1; } int main() { /* COUT(calc_num(3, 4, 3)); COUT(calc_num(3, 4, 4)); COUT(calc_num(3, 4, 5)); */ int CASE; cin >> CASE; while (CASE--) { long long P, Q, K; cin >> P >> Q >> K; long long G = gcd(P, Q); P /= G, Q /= G; long long low = -1, high = 1LL<<50; while (high - low > 1) { long long M = (low + high) / 2; if (calc_num(P, Q, M) >= K) high = M; else low = M; } cout << high * G << endl; } }