結果

問題 No.1936 Rational Approximation
ユーザー eQe
提出日時 2025-09-09 14:50:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,361 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 275,848 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-09-09 14:50:20
合計ジャッジ時間 3,961 ms
ジャッジサーバーID
(参考情報)
judge / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#if __has_include(<atcoder/all>)
#endif
using namespace std;
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);}
#define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{
namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ld=long double;
using ll=long long;
constexpr ld ee=1e-12;
void lin(auto&...a){(cin>>...>>a);}
constexpr auto abs(auto x){return x<0?-x:x;}
auto sign(auto a){using T=decltype(a);return(a>=(T)ee)-(a<=(T)-ee);}
template<class A,class B=A>struct pair{
  A a;B b;
  pair()=default;
  pair(A aa,B bb):a(aa),b(bb){}
  auto operator<=>(const pair&)const=default;
};
}
namespace my{
template<class T>pair<T,T>impl_ax_by_g(T a,T b){
  if(b==0)return{1,0};
  auto[s,t]=impl_ax_by_g(b,a%b);
  return{t,s-a/b*t};
}
template<class T>pair<T,T>ax_by_g(T a,T b){
  auto[s,t]=impl_ax_by_g(abs(a),abs(b));
  return{s*sign(a),t*sign(b)};
}
}
namespace my{entry
void main(){
  LL(a,b);
  auto[s0,t0]=ax_by_g(a,-b);
  auto[s1,t1]=ax_by_g(-a,b);

  while(s0<0)s0+=b,t0+=a; // 1\le s0<b を満たすように
  while(s1<0)s1+=b,t1+=a; // 1\le s1<b を満たすように
  pp(s0+s1+t0+t1);
}}
0