結果

問題 No.2066 Simple Math !
ユーザー milanis48663220milanis48663220
提出日時 2022-09-02 22:42:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 973 ms / 2,000 ms
コード長 4,636 bytes
コンパイル時間 1,398 ms
コンパイル使用メモリ 124,432 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 04:59:51
合計ジャッジ時間 20,196 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 41 ms
4,380 KB
testcase_02 AC 47 ms
4,380 KB
testcase_03 AC 18 ms
4,376 KB
testcase_04 AC 897 ms
4,380 KB
testcase_05 AC 896 ms
4,376 KB
testcase_06 AC 898 ms
4,376 KB
testcase_07 AC 895 ms
4,380 KB
testcase_08 AC 894 ms
4,376 KB
testcase_09 AC 946 ms
4,376 KB
testcase_10 AC 947 ms
4,380 KB
testcase_11 AC 942 ms
4,388 KB
testcase_12 AC 952 ms
4,376 KB
testcase_13 AC 941 ms
4,380 KB
testcase_14 AC 964 ms
4,376 KB
testcase_15 AC 967 ms
4,380 KB
testcase_16 AC 971 ms
4,380 KB
testcase_17 AC 973 ms
4,380 KB
testcase_18 AC 970 ms
4,380 KB
testcase_19 AC 273 ms
4,376 KB
testcase_20 AC 275 ms
4,376 KB
testcase_21 AC 273 ms
4,376 KB
testcase_22 AC 275 ms
4,380 KB
testcase_23 AC 279 ms
4,380 KB
testcase_24 AC 293 ms
4,376 KB
testcase_25 AC 292 ms
4,376 KB
testcase_26 AC 291 ms
4,380 KB
testcase_27 AC 291 ms
4,380 KB
testcase_28 AC 292 ms
4,380 KB
testcase_29 AC 162 ms
4,376 KB
testcase_30 AC 19 ms
4,380 KB
testcase_31 AC 40 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/math>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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; }

using namespace std;
typedef __int128 ll;


/**
 * https://kenkoooo.hatenablog.com/entry/2016/11/30/163533
 * より。Project Eularであまりにも使うので。
 */

__int128 parse(string &s) {
  __int128 ret = 0;
  for (int i = 0; i < s.length(); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  return ret;
}

istream & operator >> (istream &in,  __int128_t &m){
    string s; cin >> s;
    m = parse(s);
    return in;
}


ostream &operator<<(std::ostream &dest, __int128_t value) {
    ostream::sentry s(dest);
    if (s) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[128];
        char *d = end(buffer);
        do {
        --d;
        *d = "0123456789"[tmp % 10];
        tmp /= 10;
        } while (tmp != 0);
        if (value < 0) {
        --d;
        *d = '-';
        }
        int len = end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
        dest.setstate(ios_base::badbit);
        }
    }
    return dest;
}

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}


/**
 * ax + by = gcd(a, b)を満たす(x, y)をつめて、gcd(a, b)を返します。
 */ 
template <typename T>
T extGCD(T a, T b, T &x, T &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    T d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}


// calc ceil(a/b) 
template<typename T>
T ceil_div(T a, T b){
    return (a+b-1)/b;
}

// copied from acl and slightly modified.
namespace internal{

ll safe_mod(ll x, ll m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

 ll floor_sum_( ll n,
                                       ll m,
                                       ll a,
                                       ll b) {
     ll ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

         ll y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = ( ll)(y_max / m);
        b = ( ll)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}
}

ll floor_sum(ll n, ll m, ll a, ll b) {
    ll ans = 0;
    if (a < 0) {
        ll a2 = internal::safe_mod(a, m);
        ans -= n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        ll b2 = internal::safe_mod(b, m);
        ans -= n * ((b2 - b) / m);
        b = b2;
    }
    return ans + internal::floor_sum_(n, m, a, b);
}

void solve(){
    long p, q, k; cin >> p >> q >> k;
    if(p == q){
        cout << p*k << endl;
        return;
    }
    if(p > q) swap(p, q);
    long a, b;
    extGCD(p, q, a, b);
    if(a < 0){
        ll x = ceil_div(-a, q);
        a += x*q;
        b -= x*p;
    }
    if(a > q){
        ll x = a/q;
        a -= x*q;
        b += x*p;
    }
    b *= -1;
    ll g = gcd(p, q);
    p /= g;
    q /= g;
    function<ll(ll)> count = [&](ll x){
        if(x >= p*q){
            return count(p*q-1)+x-p*q+1;
        }
        ll upper = floor_sum(x+1, q, a, 0);
        ll lower = floor_sum(x+1, p, b, 0);
        ll on_line = x/p;
        return upper-lower+on_line;
    };
    ll l = 0, r = p*q+k;
    while(r-l > 1){
        ll x = (l+r)/2;
        if(count(x) >= k) r = x;
        else l = x;
    }
    cout << r*g << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; cin >> t;
    while(t--) solve();
}
0