結果

問題 No.2066 Simple Math !
ユーザー chineristACchineristAC
提出日時 2022-09-02 22:36:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 527 ms / 2,000 ms
コード長 2,756 bytes
コンパイル時間 3,293 ms
コンパイル使用メモリ 186,200 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 04:52:50
合計ジャッジ時間 14,435 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 21 ms
4,384 KB
testcase_02 AC 32 ms
4,388 KB
testcase_03 AC 18 ms
4,376 KB
testcase_04 AC 493 ms
4,384 KB
testcase_05 AC 498 ms
4,380 KB
testcase_06 AC 480 ms
4,380 KB
testcase_07 AC 484 ms
4,380 KB
testcase_08 AC 484 ms
4,380 KB
testcase_09 AC 512 ms
4,376 KB
testcase_10 AC 512 ms
4,380 KB
testcase_11 AC 519 ms
4,380 KB
testcase_12 AC 516 ms
4,380 KB
testcase_13 AC 525 ms
4,380 KB
testcase_14 AC 521 ms
4,376 KB
testcase_15 AC 518 ms
4,376 KB
testcase_16 AC 527 ms
4,380 KB
testcase_17 AC 523 ms
4,376 KB
testcase_18 AC 519 ms
4,380 KB
testcase_19 AC 156 ms
4,380 KB
testcase_20 AC 160 ms
4,380 KB
testcase_21 AC 151 ms
4,380 KB
testcase_22 AC 156 ms
4,380 KB
testcase_23 AC 160 ms
4,380 KB
testcase_24 AC 177 ms
4,376 KB
testcase_25 AC 174 ms
4,380 KB
testcase_26 AC 178 ms
4,376 KB
testcase_27 AC 180 ms
4,380 KB
testcase_28 AC 178 ms
4,380 KB
testcase_29 AC 41 ms
4,380 KB
testcase_30 AC 19 ms
4,380 KB
testcase_31 AC 20 ms
4,376 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 = 0;

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

  PQ_less = cnt(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