結果

問題 No.1936 Rational Approximation
ユーザー eQeeQe
提出日時 2022-09-22 01:53:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 20,008 bytes
コンパイル時間 12,170 ms
コンパイル使用メモリ 485,096 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-01 17:42:06
合計ジャッジ時間 12,372 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<atcoder/all>
#include<boost/multiprecision/cpp_int.hpp>
#include<bits/stdc++.h>
#define bgn(a) begin(a)
#define rbg(a) rbegin(a)
#define fin(...) exit(pp(__VA_ARGS__))
#define done(...) rr vo(pp(__VA_ARGS__))
#define ep(...) emplace(__VA_ARGS__)
#define ef(...) emplace_front(__VA_ARGS__)
#define eb(...) emplace_back(__VA_ARGS__)
#define pf(...) pop_front(__VA_ARGS__)
#define pb(...) pop_back(__VA_ARGS__)
#define ei(...) else if(__VA_ARGS__)
#define iif if
#define el else
#define wh(...) while(__VA_ARGS__)
#define lb(...) lower_bound(__VA_ARGS__)
#define ub(...) upper_bound(__VA_ARGS__)
#define itmax(...) max_element(al(__VA_ARGS__))
#define itmin(...) min_element(al(__VA_ARGS__))
#define srt(...) sort(al(__VA_ARGS__))
#define rv(...) reverse(al(__VA_ARGS__))
#define rsr(a) srt(a);rv(a)
#define uq(a) srt(a);a.erase(unique(al(a)),end(a))
#define sw(a,b) swap(a,b)
#define sb(...) substr(__VA_ARGS__)
#define ov3(a,b,c,d,...) d
#define ov4(a,b,c,d,e,...) e
#define ov5(a,b,c,d,e,f,...) f
#define ov6(a,b,c,d,e,f,g,...) g
#define al1(v) bgn(v),end(v)
#define al2(v,b) bgn(v),bgn(v)+b
#define al3(v,a,b) bgn(v)+a,bgn(v)+b
#define al(...) ov3(__VA_ARGS__,al3,al2,al1)(__VA_ARGS__)
#define fo1(b) for(ll ii=0;ii<(ll)(b);ii++)
#define fo2(i,b) for(ll i=0;i<(ll)(b);i++)
#define fo3(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define fo4(i,a,b,c) for(ll i=(ll)(a);i<(ll)(b);i+=(ll)(c))
#define fo(...) ov4(__VA_ARGS__,fo4,fo3,fo2,fo1)(__VA_ARGS__)
#define of1(a) fo(a)
#define of2(i,a) for(ll i=(ll)(a)-1;i>=0;i--)
#define of3(i,a,b) for(ll i=(ll)(a)-1;i>=(ll)(b);i--)
#define of4(i,a,b,c) for(ll i=(ll)(a)-1;i>=(ll)(b);i-=(ll)(c))
#define of(...) ov4(__VA_ARGS__,of4,of3,of2,of1)(__VA_ARGS__)
#define fe1(a) fo(len(a))
#define fe2(a,v) for(au&&a:v)
#define fe3(a,b,v) for(au&&[a,b]:v)
#define fe4(a,b,c,v) for(au&&[a,b,c]:v)
#define fe5(a,b,c,d,v) for(au&&[a,b,c,d]:v)
#define fe(...) ov5(__VA_ARGS__,fe5,fe4,fe3,fe2,fe1)(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;li(__VA_ARGS__)
#define DD(...) dd __VA_ARGS__;li(__VA_ARGS__)
#define ST(...) str __VA_ARGS__;li(__VA_ARGS__)
#define UL2(n,a) u1 a(n);li(a)
#define UL3(n,a,b) u1 a(n),b(n);li(a,b)
#define UL4(n,a,b,c) u1 a(n),b(n),c(n);li(a,b,c)
#define UL5(n,a,b,c,d) u1 a(n),b(n),c(n),d(n);li(a,b,c,d)
#define UL6(n,a,b,c,d,e) u1 a(n),b(n),c(n),d(n),e(n);li(a,b,c,d,e)
#define UL(...) ov6(__VA_ARGS__,UL6,UL5,UL4,UL3,UL2)(__VA_ARGS__)
#define UV2(n,a) u1 a(n);vi(a)
#define UV3(n,a,b) u1 a(n),b(n);vi(a,b)
#define UV4(n,a,b,c) u1 a(n),b(n),c(n);vi(a,b,c)
#define UV5(n,a,b,c,d) u1 a(n),b(n),c(n),d(n);vi(a,b,c,d)
#define UV6(n,a,b,c,d,e) u1 a(n),b(n),c(n),d(n),e(n);vi(a,b,c,d,e)
#define UV(...) ov6(__VA_ARGS__,UV6,UV5,UV4,UV3,UV2)(__VA_ARGS__)
#define DL2(n,a) v1<dd>a(n);li(a)
#define DL3(n,a,b) v1<dd>a(n),b(n);li(a,b)
#define DL4(n,a,b,c) v1<dd>a(n),b(n),c(n);li(a,b,c)
#define DL5(n,a,b,c,d) v1<dd>a(n),b(n),c(n),d(n);li(a,b,c,d)
#define DL(...) ov5(__VA_ARGS__,DL5,DL4,DL3,DL2)(__VA_ARGS__)
#define DV2(n,a) v1<dd>a(n);vi(a)
#define DV3(n,a,b) v1<dd>a(n),b(n);vi(a,b)
#define DV4(n,a,b,c) v1<dd>a(n),b(n),c(n);vi(a,b,c)
#define DV5(n,a,b,c,d) v1<dd>a(n),b(n),c(n),d(n);vi(a,b,c,d)
#define DV(...) ov5(__VA_ARGS__,DV5,DV4,DV3,DV2)(__VA_ARGS__)
#define U23(h,w,a) u2 a;rs(a,h,w);li(a)
#define U24(h,w,a,b) u2 a,b;rs(a,h,w),rs(b,h,w);li(a,b)
#define U25(h,w,a,b,c) u2 a,b,c;rs(a,h,w),rs(b,h,w),rs(c,h,w);li(a,b,c)
#define U2(...) ov5(__VA_ARGS__,U25,U24,U23)(__VA_ARGS__)
#define S22(h,a) strs a(h);li(a)
#define S23(h,a,b) strs a(h),b(h);li(a,b)
#define S2(...) ov3(__VA_ARGS__,S23,S22)(__VA_ARGS__)
#define aa2(a,b) ((a)&&(b))
#define aa3(a,b,c) ((a)&&(b)&&(c))
#define aa4(a,b,c,d) ((a)&&(b)&&(c)&&(d))
#define aa5(a,b,c,d,e) ((a)&&(b)&&(c)&&(d)&&(e))
#define aa6(a,b,c,d,e,f) ((a)&&(b)&&(c)&&(d)&&(e)&&(f))
#define aa(...) ov6(__VA_ARGS__,aa6,aa5,aa4,aa3,aa2)(__VA_ARGS__)
#define oo2(a,b) ((a)||(b))
#define oo3(a,b,c) ((a)||(b)||(c))
#define oo4(a,b,c,d) ((a)||(b)||(c)||(d))
#define oo5(a,b,c,d,e) ((a)||(b)||(c)||(d)||(e))
#define oo6(a,b,c,d,e,f) ((a)||(b)||(c)||(d)||(e)||(f))
#define oo(...) ov6(__VA_ARGS__,oo6,oo5,oo4,oo3,oo2)(__VA_ARGS__)
#define I template
#define J class
#define rr return
#define ss struct
#define uu using
#define as assert
#define au auto
#define bk break
#define cs const
#define ct continue
#define endl "\n"
#define fd friend
#define op operator
#define sc static
#define th this
namespace my{
uu vo=void;
vo main();
vo solve();
}
int main(){my::main();}
namespace my{
uu namespace std;
uu namespace atcoder;
uu namespace boost::multiprecision;
uu mint=modint998244353;
uu cint=cpp_int;
uu ll=long long;
uu dd=long double;
uu ui=unsigned int;
uu ul=unsigned long long;
uu chr=char;
uu str=string;
uu bo=bool;
uu is=istream;
uu os=ostream;
str sp=" ",nc="",nl="\n";
au np=nullptr;
I<J A,J B>ss cp{
  A a;B b;
  cp():a(A()),b(B()){}
  cp(A a,B b):a(a),b(b){}
  cp op+()cs{rr*th;}
  cp op-()cs{rr cp(-a,-b);}
  cp&op++(){a++,b++;rr*th;}cp op++(int){cp r=*th;++*th;rr r;}
  cp&op--(){a--,b--;rr*th;}cp op--(int){cp r=*th;--*th;rr r;}
  cp&op+=(cs cp&c){a+=c.a;b+=c.b;rr*th;}cp op+(cs cp&c)cs{rr cp(*th)+=c;}
  cp&op-=(cs cp&c){a-=c.a;b-=c.b;rr*th;}cp op-(cs cp&c)cs{rr cp(*th)-=c;}
  cp&op*=(cs cp&c){a*=c.a;b*=c.b;rr*th;}cp op*(cs cp&c)cs{rr cp(*th)*=c;}
  I<J D>cp&op+=(cs D&d){a+=d;b+=d;rr*th;}I<J D>cp op+(cs D&d)cs{rr cp(*th)+=d;}
  I<J D>cp&op-=(cs D&d){a-=d;b-=d;rr*th;}I<J D>cp op-(cs D&d)cs{rr cp(*th)-=d;}
  I<J D>cp&op*=(cs D&d){a*=d;b*=d;rr*th;}I<J D>cp op*(cs D&d)cs{rr cp(*th)*=d;}
  bo op==(cs cp&c)cs{rr aa(a==c.a,b==c.b);}bo op!=(cs cp&c)cs{rr!(*th==c);}
  bo op<(cs cp&c)cs{rr a!=c.a?a<c.a:b<c.b;}bo op>=(cs cp&c)cs{rr!(*th<c);}
  bo op>(cs cp&c)cs{rr a!=c.a?a>c.a:b>c.b;}bo op<=(cs cp&c)cs{rr!(*th>c);}
  fd is&op>>(is&i,cp&c){rr i>>c.a>>c.b;}
  fd os&op<<(os&o,cs cp&c){rr o<<c.a<<sp<<c.b;}
};
I<J A,J B,J C>ss tr{
  A a;B b;C c;
  tr():a(A()),b(B()),c(C()){}
  tr(A a,B b,C c):a(a),b(b),c(c){}
  bo op==(cs tr&t)cs{rr aa(a==t.a,b==t.b,c==t.c);}
  bo op!=(cs tr&t)cs{rr!(*th==t);}
  bo op<(cs tr&t)cs{rr a!=t.a?a<t.a:b!=t.b?b<t.b:c<t.c;}
  bo op>(cs tr&t)cs{rr a!=t.a?a>t.a:b!=t.b?b>t.b:c>t.c;}
  fd is&op>>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;}
  fd os&op<<(os&o,cs tr&t){rr o<<t.a<<sp<<t.b<<sp<<t.c;}
};
I<J A,J B,J C,J D>ss qu{
  A a;B b;C c;D d;
  qu():a(A()),b(B()),c(C()),d(D()){}
  qu(A a,B b,C c,D d):a(a),b(b),c(c),d(d){}
  bo op==(cs qu&q)cs{rr aa(a==q.a,b==q.b,c==q.c,d==q.d);}
  bo op!=(cs qu&q)cs{rr!(*th==q);}
  bo op<(cs qu&q)cs{rr a!=q.a?a<q.a:b!=q.b?b<q.b:c!=q.c?c<q.c:d<q.d;}
  bo op>(cs qu&q)cs{rr a!=q.a?a>q.a:b!=q.b?b>q.b:c!=q.c?c>q.c:d>q.d;}
  fd is&op>>(is&i,qu&q){rr i>>q.a>>q.b>>q.c>>q.d;}
  fd os&op<<(os&o,cs qu&q){rr o<<q.a<<sp<<q.b<<sp<<q.c<<sp<<q.d;}
};
I<J T>uu v1=vector<T>;uu u1=v1<ll>;
I<J T>uu v2=v1<v1<T>>;uu u2=v1<u1>;
I<J T>uu v3=v1<v2<T>>;uu u3=v1<u2>;
I<J T>uu v4=v1<v3<T>>;uu u4=v1<u3>;
I<J T>uu mset=multiset<T>;
I<J T>uu fn=function<T>;
I<J T>uu pqmax=priority_queue<T>;
I<J T>uu pqmin=priority_queue<T,v1<T>,greater<T>>;
uu strs=v1<str>;
uu cl=cp<ll,ll>;
uu cd=cp<dd,dd>;
uu tl=tr<ll,ll,ll>;
uu ql=deque<ll>;
uu sl=set<ll>;
uu msl=mset<ll>;
uu ml=map<ll,ll>;
ss edg{ll t,w;edg(){}edg(ll t,ll w=1):t(t),w(w){}};
uu graph=v2<edg>;
ll inf=3e18;
dd e=1e-10,pi=acosl(-1);
u1 dx={-1,0,1,0,-1,1,1,-1},dy={0,-1,0,1,-1,-1,1,1};
str first(bo t=1){rr t?"first":"second";}
str First(bo t=1){rr t?"First":"Second";}
str yes(bo t=1){rr t?"yes":"no";}str no(){rr yes(0);}
str Yes(bo t=1){rr t?"Yes":"No";}str No(){rr Yes(0);}
str YES(bo t=1){rr t?"YES":"NO";}str NO(){rr YES(0);}
str possible(bo t=1){rr t?"possible":"impossible";}
str Possible(bo t=1){rr t?"Possible":"Impossible";}
str POSSIBLE(bo t=1){rr t?"POSSIBLE":"IMPOSSIBLE";}

ui rnd(){
  sc ui x=123456789,y=362436069,z=521288629,w=88675123;
  ui t=(x^(x<<11));x=y,y=z,z=w;
  rr(w=(w^(w>>19))^(t^(t>>8)));
}
dd rnd_prb(){rr (dd)rnd()/UINT_MAX;}
ll rnd_rng(ll r){rr rnd()%r;}
ll rnd_rng(ll l,ll r){rr rnd()%(r-l)+l;}

bo od(ll x){rr x&1;}
bo ev(ll x){rr !od(x);}
ll bit(ll a){rr 1LL<<a;}
ll ppc(ll a){rr __builtin_popcountll(a);}
ll sqr(ll x){if(x<=1)rr x;ll r=sqrtl(x)-1;wh(r+1<=x/(r+1))r++;rr r;}
ll lg2(ll n){ll r=0;wh(n){n/=2;r++;}rr r;}
u1 sub(ll S){u1 r;for(ll b=S;b>0;--b&=S)r.eb(b);srt(r);rr r;}
I<J T>ll len(cs T&a){rr a.size();}
I<J T>bo yu(cs T&a){rr len(a);}
I<J T>bo mu(cs T&a){rr !yu(a);}
I<J T>T sq(T a){rr a*a;}
ll at(ll S,ll i){rr S>>i&1;}
I<J T>T at(cs v1<T>&v,ll i){ll n=len(v);rr v[(i%n+n)%n];}
I<J T>ll sgn(cs T&a){rr(a>e)-(a<-e);}
I<J T,J U>ll sgn(cs T&a,cs U&b){rr sgn(a-b);}
I<J T,J U>T cei(T x,U y){as(y);rr(y<0?cei(-x,-y):(x>0?(x+y-1)/y:x/y));}
I<J T,J U>T flo(T x,U y){as(y);rr(y<0?flo(-x,-y):(x>0?x/y:x/y-!(x%y)));}
str tos(cs chr&a){rr a+nc;}
str tos(cs chr*a){rr a;}
str tos(cs str&a){rr a;}
I<J T>str tos(cs T&a){rr to_string(a);}
I<J T,J...A>str tos(cs T&a,cs A&...b){rr tos(a)+tos(b...);}
u1 tou(cs str&s,chr b='a'){u1 a;fe(c,s)a.eb(c-b);rr a;}
u2 tou(cs strs&s,chr b='a'){u2 a;fe(t,s)a.eb(tou(t,b));rr a;}
u1 tou(cs str&s,cs str&t){
  ll n=len(s);
  u1 a(n);
  fo(i,n)fo(j,len(t))if(s[i]==t[j]){a[i]=j;bk;}
  rr a;
}
u2 tou(cs strs&s,cs str&t){ll h=len(s);u2 a(h);fo(i,h)a[i]=tou(s[i],t);rr a;}

u1 tou(cs v1<chr>&b,cs str&t){
  ll n=len(b);
  u1 a(n);
  fo(i,n)fo(j,len(t))if(b[i]==t[j]){a[i]=j;bk;}
  rr a;
}

u2 tou(cs graph&g){ll n=len(g);u2 a(n);fo(u,n)fe(v,w,g[u])a[u].eb(v);rr a;}
graph tog(cs u2&a){ll n=len(a);graph g(n);fo(u,n)fe(v,a[u])g[u].eb(v);rr g;}


I<J T>au op+(v1<T>v){rr v;}
I<J T>au op-(v1<T>v){fe(x,v)x=-x;rr v;}
I<J T>au&op++(v1<T>&v){fe(x,v)x++;rr v;}
I<J T>au&op--(v1<T>&v){fe(x,v)x--;rr v;}
I<J T>au op++(v1<T>&v,int){au r=v;++v;rr r;}
I<J T>au op--(v1<T>&v,int){au r=v;--v;rr r;}
I<J T>au&op+=(v1<T>&v,cs v1<T>&u){fo(i,len(v))v[i]+=u[i];rr v;}
I<J T>au&op-=(v1<T>&v,cs v1<T>&u){fo(i,len(v))v[i]-=u[i];rr v;}
I<J T>au&op*=(v1<T>&v,cs v1<T>&u){fo(i,len(v))v[i]*=u[i];rr v;}
I<J T>au op+(v1<T>v,cs v1<T>&u){rr v+=u;}
I<J T>au op-(v1<T>v,cs v1<T>&u){rr v-=u;}
I<J T>au op*(v1<T>v,cs v1<T>&u){rr v*=u;}
I<J T,J U>au&op+=(v1<T>&v,cs U&a){fe(x,v)x+=a;rr v;}
I<J T,J U>au&op-=(v1<T>&v,cs U&a){fe(x,v)x-=a;rr v;}
I<J T,J U>au&op*=(v1<T>&v,cs U&a){fe(x,v)x*=a;rr v;}
I<J T,J U>au&op/=(v1<T>&v,cs U&a){fe(x,v)x/=a;rr v;}
I<J T,J U>au&op%=(v1<T>&v,cs U&a){fe(x,v)x%=a;rr v;}
I<J T,J U>au op+(v1<T>v,cs U&a){rr v+=a;}
I<J T,J U>au op-(v1<T>v,cs U&a){rr v-=a;}
I<J T,J U>au op*(v1<T>v,cs U&a){rr v*=a;}
I<J T,J U>au op/(v1<T>v,cs U&a){rr v/=a;}
I<J T,J U>au op%(v1<T>v,cs U&a){rr v%=a;}
I<J T>au jo(v1<T>&a,cs v1<T>&b){copy(al(b),back_inserter(a));}
I<J T,J U>bo amax(T&a,cs U&b){rr a<b?a=b,1:0;}
I<J T,J U>bo amin(T&a,cs U&b){rr a>b?a=b,1:0;}
I<J T>T max(cs v1<T>&a){rr *itmax(a);}
I<J T>T min(cs v1<T>&a){rr *itmin(a);}
I<J T>au max(cs v2<T>&a){rr max(*itmax(a));}
I<J T>au min(cs v2<T>&a){rr min(*itmin(a));}
I<J...T>au max(T...a){rr max(initializer_list<common_type_t<T...>>{a...});}
I<J...T>au min(T...a){rr min(initializer_list<common_type_t<T...>>{a...});}
ll mex(cs sl&s){ll r=0;wh(s.count(r))r++;rr r;}
I<J...T>ll mex(cs T&...a){sl s;fe(x,{a...})s.insert(x);rr mex(s);}
I<J T>ll argmax(cs v1<T>&a){rr itmax(a)-bgn(a);}
I<J T>ll argmin(cs v1<T>&a){rr itmin(a)-bgn(a);}
I<J T,J U>au gcd(cs T&a,cs U&b){rr std::gcd(a,b);}
I<J T,J...A>au gcd(cs T&a,cs A&...b){rr gcd(a,gcd(b...));}
I<J T>T sum(cs v1<T>&a){rr accumulate(al(a),T());}
I<J T,J U>ll lbs(cs v1<T>&a,cs U&b){rr lb(al(a),b)-bgn(a);}
I<J T,J U>ll ubs(cs v1<T>&a,cs U&b){rr ub(al(a),b)-bgn(a);}
I<J T,J U>au minmax(cs T&a,cs U&b){rr cp(min(a,b),max(a,b));}
I<J T,J U>au minmax(cs cp<T,U>&p){rr minmax(p.a,p.b);}
I<J X,J Y>au mp(cs fn<Y(X)>&f,v1<X>x){v1<Y>y;fe(a,x)y.eb(f(a));rr y;}
I<J T,J U>au keys(cs map<T,U>&m){v1<T>r;fe(k,v,m)r.eb(k);rr r;}
I<J T,J U>au vals(cs map<T,U>&m){v1<U>r;fe(k,v,m)r.eb(v);rr r;}
u1 io(ll n,ll a=0){u1 i(n);iota(al(i),a);rr i;}
I<J T,J U>vo emplace_front(v1<T>&v,U x=U()){v.ep(bgn(v),x);}
I<J T>vo emplace_front(v1<T>&v,T x=T()){v.ep(bgn(v),x);}
I<J T>vo pop_front(v1<T>&v){v.erase(bgn(v));}
I<J T>au&bg(T&a){rr*bgn(a);}
I<J T>au&rb(T&a){rr*rbg(a);}
I<J T>T bg(pqmax<T>&q){rr q.top();}
I<J T>T bg(pqmin<T>&q){rr q.top();}
I<J T>T pop(v1<T>&v){T r=rb(v);v.pb();rr r;}
I<J T>T pop_front(deque<T>&q){T r=bg(q);q.pf();rr r;}
I<J T>T pop_back(deque<T>&q){T r=rb(q);q.pb();rr r;}
I<J T>T pop(deque<T>&q){rr pop_front(q);}
I<J T>T pop(pqmax<T>&q){T r=bg(q);q.pop();rr r;}
I<J T>T pop(pqmin<T>&q){T r=bg(q);q.pop();rr r;}


ss z{z(){cin.tie(0);ios::sync_with_stdio(0);cout<<fixed<<setprecision(10);}}z;
is&op>>(is&i,mint&x){ll t;i>>t;x=t;rr i;}
os&op<<(os&o,cs mint&x){rr o<<x.val();}
I<J T,J U>is&op>>(is&i,pair<T,U>&p){rr i>>p.first>>p.second;}
I<J T,J U>os&op<<(os&o,cs pair<T,U>&p){rr o<<p.first<<sp<<p.second;}
I<J T>is&op>>(is&i,v1<T>&v){fe(x,v)i>>x;rr i;}
I<J T>os&op<<(os&o,cs v1<T>&v){fe(x,v)o<<x<<(&x==&rb(v)?nc:sp);rr o;}
I<J T>os&op<<(os&o,cs v2<T>&v){fe(x,v)o<<x<<(&x==&rb(v)?nc:nl);rr o;}
I<J T,J U>os&op<<(os&o,cs map<T,U>&m){fe(p,m)o<<p<<(&p==&rb(m)?nc:nl);rr o;}
I<J T>os&op<<(os&o,cs set<T>&s){fe(x,s)o<<x<<sp;rr o;}
I<J T>os&op<<(os&o,cs mset<T>&s){fe(x,s)o<<x<<sp;rr o;}
I<J T>os&op<<(os&o,deque<T>q){wh(yu(q))o<<pop(q)<<sp;rr o;}
I<J T>os&op<<(os&o,pqmax<T>q){wh(yu(q))o<<pop(q)<<sp;rr o;}
I<J T>os&op<<(os&o,pqmin<T>q){wh(yu(q))o<<pop(q)<<sp;rr o;}
ll pp(){cout<<endl;rr 0;}
I<J T,J...A>ll pp(cs T&a,cs A&...b){cout<<a;((cout<<sp<<b),...);rr pp();}
I<J...T>ll li(T&...a){(cin>>...>>a);rr 0;}
I<J...T>vo vi(ll i,T&...a){(cin>>...>>a[i]);}
I<J T,J...A>vo vi(v1<T>&a,A&...b){fo(i,len(a))vi(i,a,b...);}
I<J T>vo rs(v1<T>&v,ll n){v.resize(n);}
I<J T,J...A>vo rs(v1<T>&V,ll n,cs A&...a){rs(V,n);fe(v,V)rs(v,a...);}
I<J T,J U>vo fl(T&x,cs U&a){x=a;}
I<J T,J U>vo fl(v1<T>&V,cs U&a){fe(v,V)fl(v,a);}
vo gi(u2&g,ll m,bo d=1){fo(m){LL(a,b);a--,b--;g[a].eb(b);if(d)g[b].eb(a);}}
vo gi(graph&g,ll m,bo w=1,bo d=1){
  fo(m){LL(a,b);a--,b--;ll c=1;if(w)li(c);g[a].eb(b,c);if(d)g[b].eb(a,c);}
}
vo ti(u2&g,bo d=1){gi(g,len(g)-1,d);}
vo ti(graph&g,bo w=1,bo d=1){gi(g,len(g)-1,w,d);}


I<J F>ss rcl:private F{
  explicit rcl(F&&f):F(forward<F>(f)){}
  I<J...T>decltype(au)op()(T&&...a)cs{rr F::op()(*th,forward<T>(a)...);}
};
vo pz(cs u1&v,u1&h){fe(x,h)x=lbs(v,x);}
I<J...T>vo pz(cs u1&v,u1&h,T&...t){pz(v,h);pz(v,t...);}
I<J...T>u1 zp(T&...a){u1 v;fe(e,{a...})jo(v,e);uq(v);pz(v,a...);rr v;}
I<J T>vo sv(cs u1&o,v1<T>&a){au c=a;fo(i,len(a))a[i]=c[o[i]];}
I<J T,J...A>vo sv(cs u1&o,v1<T>&a,A&...b){sv(o,a);sv(o,b...);}
I<J...T>au vs(cs fn<bo(ll,ll)>&f,ll n,T&...a){
  u1 o=io(n),p(n);
  sort(al(o),f);
  fo(i,n)p[o[i]]=i;
  sv(o,a...);
  rr cp(o,p);
}


ll bsl(cs fn<bo(ll)>&j,ll o,ll n){wh(abs(o-n)>1)(j((o+n)/2)?o:n)=(o+n)/2;rr o;}
dd bsd(cs fn<bo(dd)>&j,dd o,dd n){wh(abs(o-n)>e)(j((o+n)/2)?o:n)=(o+n)/2;rr o;}
I<J T>au zt(v1<T>a){fo(i,1,len(a))a[i]+=a[i-1];rr a;}
I<J T>au mb(v1<T>a){of(i,len(a),1)a[i]-=a[i-1];rr a;}
I<J T>au zt(v2<T>a){fe(v,a)v=zt(v);fo(i,1,len(a))a[i]+=a[i-1];rr a;}
I<J T>au mb(v2<T>a){fe(v,a)v=mb(v);of(i,len(a),1)a[i]-=a[i-1];rr a;}
au kth(cs u1&a,ll k){u1 s=zt(a);rr cp(lbs(s,k),s);}
au kth2(cs u1&a,ll&k){au[i,s]=kth(a,k);k-=s[i-1];rr i;}
I<J T>T esq(T x,ll n,T e){wh(n){if(n&1)e*=x;x*=x;n/=2;}rr e;}
vo wafl(u2&d){ll n=len(d);fo(k,n)fo(i,n)fo(j,n)amin(d[i][j],d[i][k]+d[k][j]);}
u1 divs(ll n){ml m;fo(i,1,n/i+1)if(n%i==0)m[i]++,m[n/i]++;rr keys(m);}
ml prfa(ll n){ml m;fo(i,2,n/i+1)wh(n%i==0)m[i]++,n/=i;if(n>1)m[n]++;rr m;}
ll inv(ll a,ll m){
  ll b=m,u=1,v=0;
  wh(b)u-=a/b*v,sw(u,v),a-=a/b*b,sw(a,b);
  rr(u%m+m)%m;
}
ll pow(ll x,ll n,ll m=inf){
  if(n<0)n=-n,x=inv(x,m);
  ll r=1;wh(n){if(n&1)r*=x,r%=m;x*=x,x%=m,n/=2;}rr r;
}
ll phi(ll n){//Euler's totient function
  ll r=n;
  fo(i,2,n/i+1)if(n%i==0){
    r-=r/i;
    wh(n%i==0)n/=i;
  }
  if(n>1)r-=r/n;
  rr r;
}

ll mrg(u1&a){
  ll n=len(a);
  if(n<=1)rr 0;
  u1 b(al(a,n/2)),c(al(a,n/2,n));
  ll cnt=mrg(b)+mrg(c);
  for(ll i=0,j=0,k=0;i<n;){
    if(aa(j<len(b),oo(k==len(c),b[j]<=c[k])))a[i++]=b[j++];
    el cnt+=n/2-j,a[i++]=c[k++];
  }
  rr cnt;
}
ll inv(u1 a){rr mrg(a);}

ll inv(u1 a,u1 b){
  u1 x=a,y=b;srt(x),srt(y);as(x==y);
  ll n=len(a);
  u2 g(len(zp(a,b)));
  of(i,n)g[b[i]].eb(i);
  fo(i,n)a[i]=pop(g[a[i]]);
  rr inv(a);
}


I<J T>ll jobsch(v1<cp<T,T>>s){
  vs([&](ll i,ll j){rr s[i].b<s[j].b;},s);
  ll r=0;T cu=0;
  fe(a,b,s)if(cu<=a)cu=b,r++;
  rr r;
}


mint sbqc(u1 a){
  ll n=len(a);ef(a);
  v1<mint>dp(n+1),su(n+1);dp[0]=su[0]=1;
  u1 p(len(zp(a)));
  fo(i,1,n+1){
    ll&k=p[a[i]];
    dp[i]=su[i-1]-(k?su[k-1]:0);
    su[i]=su[i-1]+dp[i];
    k=i;
  }
  rr su[n]-1;
}


u2 bfs2(cs u2&g,ll sx=0,ll sy=0){
  ll h=len(g),w=len(g[0]);
  u2 d;rs(d,h,w);fl(d,inf);d[sx][sy]=0;
  deque<cl>q;q.eb(sx,sy);
  wh(yu(q)){
    au[x,y]=pop(q);
    fo(k,4){
      ll nx=x+dx[k],ny=y+dy[k];
      if(oo(nx<0,ny<0,nx>=h,ny>=w))ct;
      if(g[nx][ny])ct;
      if(d[nx][ny]==inf)d[nx][ny]=d[x][y]+1,q.eb(nx,ny);
    }
  }
  rr d;
}
u1 bfs(cs u2&g,ll s=0){
  ll n=len(g);
  u1 d(n,inf);d[s]=0;
  ql q;q.eb(s);
  wh(yu(q)){
    ll u=pop(q);
    fe(v,g[u])if(d[v]==inf)d[v]=d[u]+1,q.eb(v);
  }
  rr d;
}
u1 djks(cs graph&g,ll s=0){
  ll n=len(g);
  u1 d(n,inf);d[s]=0;
  pqmin<cl>q;q.ep(0,s);
  wh(yu(q)){
    au[du,u]=pop(q);
    if(d[u]<du)ct;
    fe(v,w,g[u])if(amin(d[v],du+w))q.ep(d[v],v);
  }
  rr d;
}


I<J T>ss tww{
  v1<T>fa,rf,bs;
  v2<T>m;
  v2<bo>u;
  tww(ll n){
    n++;
    rs(fa,n);rs(rf,n);rs(bs,n);
    fa[0]=rf[0]=bs[0]=1;
    fo(i,1,n)fa[i]=fa[i-1]*i,rf[i]=rf[i-1]/i;
    fo(i,1,n)bs[i]=bs[i-1]+rf[i]*pow(-1,i&1);
    if(n<=5000)rs(m,n,n),rs(u,n,n);
  }
  T c(ll n,ll k){rr n==k?1:oo(n<k,n<0,k<0)?0:fa[n]*rf[k]*rf[n-k];}
  T p(ll n,ll k){rr c(n,k)*fa[k];}
  T h(ll n,ll r){rr c(n+r-1,r);}//n種類の選択肢から重複を許してr個選ぶ.
  T s(ll n,ll k){T r=0;fo(i,k+1)r+=c(k,i)*T(i).pow(n)*pow(-1,k-i);rr r*rf[k];}
  T b(ll n,ll k){T r=0;fo(i,k+1)r+=T(i).pow(n)*rf[i]*bs[k-i];rr r;}
  T par(ll n,ll k){//自然数nをk個の0以上の整数に分割する
    if(aa(n==0,k==0))rr 1;
    if(k==0)rr 0;
    if(u[n][k])rr m[n][k];
    if(n-k>=0)rr u[n][k]=1,m[n][k]=p(n,k-1)+p(n-k,k);
    rr u[n][k]=1,m[n][k]=p(n,k-1);
  }
};


ss ufin{
  ll n;
  u1 d;
  ufin():n(0){}
  ufin(ll n):n(n),d(n,-1){}
  ll ldr(ll a){as(aa(0<=a,a<n));if(d[a]<0)rr a;rr d[a]=ldr(d[a]);}
  bo same(ll a,ll b){as(aa(0<=a,a<n,0<=b,b<n));rr ldr(a)==ldr(b);}
  ll size(ll a){as(aa(0<=a,a<n));rr-d[ldr(a)];}
  ll mrg(ll a,ll b){
    as(aa(0<=a,a<n,0<=b,b<n));
    ll x=ldr(a),y=ldr(b);
    if(x==y)rr x;
    if(-d[x]<-d[y])sw(x,y);
    d[x]+=d[y];
    d[y]=x;
    rr x;
  }
  u2 groups(){
    u1 l(n);u2 g(n);
    fo(i,n)l[i]=ldr(i);fo(i,n)g[l[i]].eb(i);fo(i,n)srt(g[i]);srt(g);
    g.erase(remove_if(al(g),[&](cs u1&v){rr mu(v);}),end(g));
    rr g;
  }
};


I<J T,cs fn<T(T,T)>&f,cs fn<T()>&e>ss sgt{
  ll n,A,log;
  v1<T>d;
  sgt(ll n):sgt(v1<T>(n,e())){}
  sgt(cs v1<T>&v):n(len(v)){
    log=lg2(n*2-1)-1;
    A=bit(log);
    d=v1<T>(A*2,e());
    fo(i,n)d[i+A]=v[i];
    of(i,A,1)upd(i);
  }
  ll size()cs{rr n;}
  vo upd(ll k){d[k]=f(d[k*2],d[k*2+1]);}
  vo set(ll i,T x){as(aa(0<=i,i<n));i+=A;d[i]=x;fo(j,1,log+1)upd(i>>j);}
  vo add(ll i,T x){as(aa(0<=i,i<n));set(i,get(i)+x);}
  T get(ll i)cs{as(aa(0<=i,i<n));rr d[i+A];}
  T aprd(){rr d[1];}
  T prd(ll l,ll r){
    as(aa(0<=l,l<=r,r<=n));
    T sml=e(),smr=e();
    l+=A,r+=A;
    wh(l<r){if(od(l))sml=f(sml,d[l++]);if(od(r))smr=f(d[--r],smr);l/=2,r/=2;}
    rr f(sml,smr);
  }
  fd os&op<<(os&o,cs sgt&s){fo(i,len(s))o<<s.get(i)<<sp;rr o;}
};


cl bez(ll a,ll b){
  if(b==0)rr{1,0};
  au[s,t]=bez(b,a%b);
  rr{t,s-a/b*t};
}

cl bez(ll a,ll b,ll c){//ax+by=cを満たす(x,y)
  ll g=gcd(a,b);
  if(c<0)a=-a,b=-b,c=-c;
  if(c%g)rr{inf,inf};
  au[x,y]=bez(abs(a),abs(b));
  if(a<0)x=-x;
  if(b<0)y=-y;
  rr cl(x,y)*(c/g);
}


vo main(){
  ll T=1;
  //li(T);
  fo(T)solve();
}


vo solve(){
  LL(P,Q);
  
  ll ans=0;
  {//L
    au[x0,y0]=bez(-Q,P,1);
    ll n=flo(Q-1-y0,Q);
    ans+=x0+P*n;
    ans+=y0+Q*n;
  }
  {//R
    au[x0,y0]=bez(Q,-P,1);
    ll n=flo(Q-1-y0,Q);
    ans+=x0+P*n;
    ans+=y0+Q*n;
  }
  pp(ans);
}}
0