結果

問題 No.2066 Simple Math !
ユーザー RubikunRubikun
提出日時 2022-09-02 22:09:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 2,695 bytes
コンパイル時間 2,056 ms
コンパイル使用メモリ 199,316 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 04:13:04
合計ジャッジ時間 7,374 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,376 KB
testcase_02 AC 13 ms
4,376 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 188 ms
4,376 KB
testcase_05 AC 187 ms
4,380 KB
testcase_06 AC 188 ms
4,376 KB
testcase_07 AC 187 ms
4,376 KB
testcase_08 AC 186 ms
4,376 KB
testcase_09 AC 193 ms
4,376 KB
testcase_10 AC 193 ms
4,380 KB
testcase_11 AC 193 ms
4,376 KB
testcase_12 AC 193 ms
4,380 KB
testcase_13 AC 193 ms
4,380 KB
testcase_14 AC 194 ms
4,380 KB
testcase_15 AC 194 ms
4,376 KB
testcase_16 AC 195 ms
4,380 KB
testcase_17 AC 195 ms
4,376 KB
testcase_18 AC 195 ms
4,380 KB
testcase_19 AC 56 ms
4,380 KB
testcase_20 AC 56 ms
4,380 KB
testcase_21 AC 56 ms
4,380 KB
testcase_22 AC 56 ms
4,380 KB
testcase_23 AC 58 ms
4,380 KB
testcase_24 AC 97 ms
4,380 KB
testcase_25 AC 96 ms
4,376 KB
testcase_26 AC 96 ms
4,376 KB
testcase_27 AC 96 ms
4,376 KB
testcase_28 AC 96 ms
4,380 KB
testcase_29 AC 51 ms
4,380 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

//短い版

// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9
// (based on AtCoder STL)

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

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long 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;
        }
        
        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

long long floor_sum(long long n, long long m, long long a, long long b) {
    assert(0 <= n && n < (1LL << 32));
    assert(1 <= m && m < (1LL << 32));
    unsigned long long ans = 0;
    if (a < 0) {
        unsigned long long a2 = safe_mod(a, m);
        ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        unsigned long long b2 = safe_mod(b, m);
        ans -= 1ULL * n * ((b2 - b) / m);
        b = b2;
    }
    return ans + floor_sum_unsigned(n, m, a, b);
}

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    auto solve=[&](ll P,ll Q,ll K){
        if(P==1){
            return K;
        }
        ll ma=P*Q-(P-1)*(Q-1)/2;
        if(ma<=K){
            return P*Q+(K-ma);
        }
        ll left=0,right=P*Q;
        while(right-left>1){
            ll mid=(left+right)/2;
            ll cn=mid/Q;
            ll sum=floor_sum(cn+1,P,-Q,mid)+cn+1;
            if(sum>K) right=mid;
            else left=mid;
        }
        return right;
    };
    
    int QQ;cin>>QQ;
    while(QQ--){
        ll P,Q,K;cin>>P>>Q>>K;
        ll g=gcd(P,Q);
        P/=g;Q/=g;
        if(P>Q) swap(P,Q);
        cout<<solve(P,Q,K)*g<<"\n";
    }
}
0