D - Takahashi Unevolved Editorial
by
Mitsubachi
C++ を使い、公式解説の方針で解く場合にオーバフローを回避する別の方法を紹介します。
カコモンジムとAtcoderジムのどちらに行くとよいかの判定部分で、整数 $Z$ に対し、
そこで
多倍長整数として、 boost/multiprecision/cpp_int.hpp をincludeすることで使用できる cpp_int を使います。
これは通常の int や long long 型などと同様に扱うことのできる、(メモリが許す限り)上限値がない多倍長整数です。
演算が $O(1)$ ではないですが、この問題では気にすることなくACすることができます。
#include<bits/stdc++.h>
using namespace std;
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
int main(){
cpp_int x,y,a,b;
cin>>x>>y>>a>>b;
cpp_int ans=0;
while(true){
if(x*a>x+b||x*a>=y){
break;
}
x*=a;
ans++;
}
ans+=(y-1-x)/b;
cout<<ans<<endl;
}
posted:
last update:
