結果
問題 | No.2066 Simple Math ! |
ユーザー |
|
提出日時 | 2022-09-02 22:05:45 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 490 ms / 2,000 ms |
コード長 | 3,103 bytes |
コンパイル時間 | 1,959 ms |
コンパイル使用メモリ | 197,336 KB |
最終ジャッジ日時 | 2025-02-07 01:17:36 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 |
ソースコード
#include <bits/stdc++.h>#include <random>using namespace std;//#include <atcoder/all>//using namespace atcoder;using ll = long long;using vll = vector<ll>;using vvll = vector<vll>;using vvvll = vector<vvll>;using vvvvll = vector<vvvll>;using vvvvvll = vector<vvvvll>;using vvvvvvll = vector<vvvvvll>;using vb = vector<bool>;using vvb = vector<vb>;using vvvb = vector<vvb>;using vvvvb = vector<vvvb>;using vd = vector<double>;using vvd = vector<vd>;using vvvd = vector<vvd>;using vvvvd = vector<vvvd>;using vvvvvd = vector<vvvvd>;#define all(A) A.begin(),A.end()#define ALL(A) A.begin(),A.end()#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;template<class T>bool chmax(T& p, T q, bool C = 1) {if (C == 0 && p == q) {return 1;}if (p < q) {p = q;return 1;}else {return 0;}}template<class T>bool chmin(T& p, T q, bool C = 1) {if (C == 0 && p == q) {return 1;}if (p > q) {p = q;return 1;}else {return 0;}}ll gcd(ll(a), ll(b)) {if (b == 0)return a;ll c = a;while (a % b != 0) {c = a % b;a = b;b = c;}return b;}vector<ll> fact, factinv, inv;ll mod = 1e9 + 7;void prenCkModp(ll n) {fact.resize(n + 5);factinv.resize(n + 5);inv.resize(n + 5);fact.at(0) = fact.at(1) = 1;factinv.at(0) = factinv.at(1) = 1;inv.at(1) = 1;for (ll i = 2; i < n + 5; i++) {fact.at(i) = (fact.at(i - 1) * i) % mod;inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;}}ll nCk(ll n, ll k) {if (n < k) return 0;return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;}ll floor_sum(ll n, ll m, ll a, ll b) {ll ans = 0;if (a >= m) {ans += (n - 1) * n * (a / m) / 2;a %= m;}if (b >= m) {ans += n * (b / m);b %= m;}ll y_max = (a * n + b) / m, x_max = (y_max * m - b);if (y_max == 0) return ans;ans += (n - (x_max + a - 1) / a) * y_max;ans += floor_sum(y_max, a, m, (a - x_max % a) % a);return ans;}int main() {ll T;cin >> T;rep(t, T) {ll P, Q, K;cin >> P >> Q >> K;ll G = gcd(P, Q);P /= G;Q /= G;if (P > Q)swap(P, Q);if (K > (P * Q - P - Q) - (P - 1) * (Q - 1) / 2) {ll an = P * Q - P - Q + 1;an += K - ((P * Q - P - Q) - (P - 1) * (Q - 1) / 2) - 1;cout << an * G << endl;continue;}ll L = 0, R = P * Q - P - Q + 1;while (R - L > 1) {ll mid = (R + L) / 2;ll M = mid / P;ll res = floor_sum(M + 1, Q,Q-P, mid);res += M+1;res -= M * (M + 1) / 2;res--;if (res >= K)R = mid;else L = mid;}cout <<R*G << endl;}}