結果
問題 | No.2066 Simple Math ! |
ユーザー |
![]() |
提出日時 | 2022-12-26 18:41:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 477 ms / 2,000 ms |
コード長 | 2,778 bytes |
コンパイル時間 | 2,100 ms |
コンパイル使用メモリ | 195,136 KB |
最終ジャッジ日時 | 2025-02-09 21:03:22 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 |
ソースコード
#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 Ttemplate<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__ << ")" << endltemplate<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 <= Mlong 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;}}