結果
問題 | No.2066 Simple Math ! |
ユーザー | drken1215 |
提出日時 | 2022-12-26 18:37:44 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,773 bytes |
コンパイル時間 | 2,040 ms |
コンパイル使用メモリ | 202,784 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-05-01 03:47:10 |
合計ジャッジ時間 | 14,677 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 460 ms
6,944 KB |
testcase_05 | AC | 458 ms
6,948 KB |
testcase_06 | AC | 465 ms
6,940 KB |
testcase_07 | AC | 455 ms
6,940 KB |
testcase_08 | AC | 458 ms
6,944 KB |
testcase_09 | AC | 456 ms
6,944 KB |
testcase_10 | AC | 462 ms
6,940 KB |
testcase_11 | AC | 456 ms
6,940 KB |
testcase_12 | AC | 454 ms
6,940 KB |
testcase_13 | AC | 462 ms
6,940 KB |
testcase_14 | AC | 409 ms
6,940 KB |
testcase_15 | AC | 410 ms
6,940 KB |
testcase_16 | AC | 412 ms
6,940 KB |
testcase_17 | AC | 413 ms
6,944 KB |
testcase_18 | AC | 404 ms
6,940 KB |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 412 ms
6,944 KB |
testcase_25 | AC | 411 ms
6,940 KB |
testcase_26 | AC | 412 ms
6,940 KB |
testcase_27 | AC | 414 ms
6,940 KB |
testcase_28 | AC | 415 ms
6,940 KB |
testcase_29 | AC | 101 ms
6,940 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
ソースコード
#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 - mp * a; 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; } }