結果

問題 No.2066 Simple Math !
ユーザー chineristACchineristAC
提出日時 2022-09-02 22:28:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,567 bytes
コンパイル時間 2,914 ms
コンパイル使用メモリ 188,336 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 22:03:59
合計ジャッジ時間 7,158 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 20 ms
6,944 KB
testcase_02 AC 31 ms
6,944 KB
testcase_03 AC 18 ms
6,944 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 18 ms
6,940 KB
testcase_31 AC 17 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <atcoder/all>

using namespace std;
using namespace atcoder;



#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;




template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){
  os << "[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ' ';
    }
  }
  os<<"]";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const set<T>& A){
  os << "{";
  for (auto e:A){
    os << e;
    auto it = A.find(e);
    it++;
    if (it!=A.end()){
      os << " ";
    }
  }
  os << "}";
  return os;
}

template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& A){
  os << "(" << A.first <<", " << A.second << ")";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const deque<T>& A){
  os << "deque({";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ' ';
    }
  }
  os<<"})";
  return os;
}



ll solve(){
  ll P,Q,K;
  cin>>P>>Q>>K;

  ll g = gcd(P,Q);

  ll p = P/g, q = Q/g;
  auto [r0,m0] = crt({1,0},{p,q});
  ll x0 = (1-r0)/p, y0 = r0/q;

  if (0 <= x0){
    return K * g;
  }

  x0 *= -1;

  ll PQ_less = floor_sum(p*q-1,p,y0,y0) - floor_sum(p*q-1,q,x0,x0-1);

  auto cnt = [&](ll m){
    if (m < p*q){
      return floor_sum(m,p,y0,y0) - floor_sum(m,q,x0,x0-1);
    }
    else{
      return PQ_less + m-p*q+1;
    }
  };

  ll ok = 1e18;
  ll ng = 0;
  while (ok-ng>1){
    ll mid = (ok+ng)/2;
    if (cnt(mid) >= K){
      ok = mid;
    }
    else{
      ng = mid;
    }
  }

  return ok * g;





}  

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);


  int T;
  cin>>T;
  while (T--){
    cout << solve() << endl;
  }

  
}
0