結果

問題 No.2066 Simple Math !
ユーザー fumofumofunifumofumofuni
提出日時 2022-09-03 00:11:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 2,814 bytes
コンパイル時間 1,920 ms
コンパイル使用メモリ 200,244 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 06:30:38
合計ジャッジ時間 7,762 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 21 ms
4,376 KB
testcase_02 AC 27 ms
4,380 KB
testcase_03 AC 26 ms
4,376 KB
testcase_04 AC 207 ms
4,384 KB
testcase_05 AC 200 ms
4,380 KB
testcase_06 AC 204 ms
4,380 KB
testcase_07 AC 209 ms
4,376 KB
testcase_08 AC 207 ms
4,380 KB
testcase_09 AC 208 ms
4,380 KB
testcase_10 AC 209 ms
4,380 KB
testcase_11 AC 213 ms
4,376 KB
testcase_12 AC 207 ms
4,380 KB
testcase_13 AC 214 ms
4,376 KB
testcase_14 AC 187 ms
4,380 KB
testcase_15 AC 190 ms
4,380 KB
testcase_16 AC 193 ms
4,376 KB
testcase_17 AC 194 ms
4,384 KB
testcase_18 AC 200 ms
4,380 KB
testcase_19 AC 76 ms
4,380 KB
testcase_20 AC 76 ms
4,380 KB
testcase_21 AC 78 ms
4,384 KB
testcase_22 AC 77 ms
4,380 KB
testcase_23 AC 76 ms
4,380 KB
testcase_24 AC 119 ms
4,376 KB
testcase_25 AC 117 ms
4,380 KB
testcase_26 AC 112 ms
4,380 KB
testcase_27 AC 112 ms
4,380 KB
testcase_28 AC 111 ms
4,376 KB
testcase_29 AC 53 ms
4,380 KB
testcase_30 AC 26 ms
4,384 KB
testcase_31 AC 24 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")


#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,1,0,-1,1,-1,1,-1,0};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

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;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        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);
}

void solve(){
  ll p,q,k;cin >> p >> q >> k;
  ll g=gcd(p,q);p/=g;q/=g;
  if(p>q)swap(p,q);
  ll f=(p-1)*(q-1);
  //cout << f << endl;
  ll s=floor_sum(f/p+1,q,-p,f)+f/p;
  //cout << s << endl;
  if(k>=s){
    cout << (f+k-s)*g << endl;return ;
  }
  ll ok=f,ng=0;
  while(ok-ng>1){
    ll mid=(ok+ng)>>1;
    s=floor_sum(mid/p+1,q,-p,mid)+mid/p;
    if(s>=k)ok=mid;
    else ng=mid;
  }
  cout << ok*g << endl;return ;
}
int main(){
  ll t;cin >> t;
  while(t--)solve();
}
0