はじめに
AtCoderで競技プログラミングをする中で最大公約数・最小公倍数を出力したい
場合があります。
C++17にgcdとlcm関数があるのですが、AtcoderはC++14までしか対応していないので
使うことができません・・・
そこで最強ライブラリBoostを導入して、この問題を解決しようという記事です。
使用環境:
Windows10
Visual Studio2017
boost 1_68_0
Boostのダウンロード
以下のURLからBoostの最新バージョンをダウンロードします。
https://www.boost.org/users/download/#live
Windowsなら一番下をダウンロードすれば問題ないと思います。
Cドライブ直下に解凍!
ビルド
コマンドプロンプトの画面
C:\WINDOWS\system32>cd C:\boost_1_68_0
C:\boost_1_68_0>bootstrap.bat
bootstrap.batでビルドします
ビルドが終わったら
×86
b2.exe toolset=msvc-14.1 link=static runtime-link=static,shared --build-dir=build/x86 address-model=32 -j5 install --includedir=C:\boost_1_68_0\include --libdir=C:\boost_1_68_0\stage\lib\x86
×64
b2.exe toolset=msvc-14.1 link=static runtime-link=static,shared --build-dir=build/x64 address-model=64 -j5 install --includedir=C:\boost_1_68_0\include --libdir=C:\boost_1_68_0\stage\lib\x64
と打ち込みます。
かなり長い時間かかります。
Visual Studioの設定
画面上のプロジェクト➞~のプロパティを選択
C/C++ ➞ 全般 ➞ 追加のインクルードディレクトリに下記を追加
C:\boost_1_68_0\include\boost-1_68
リンカー ➞ 全般 ➞ 追加のライブラリディレクトリに下記を追加
×86
C:\boost_1_68_0\stage\lib\x86
×64
C:\boost_1_68_0\stage\lib\x64
これで準備完了です!
gcdを使ってみよう!
# include <iostream>
# include <numeric>
# include <boost/math/common_factor_rt.hpp>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
cout << gcd(n, m) << endl;
}
最大公約数が出てきたら成功です!
やったー!