LoginSignup
0
1

More than 3 years have passed since last update.

競プロのライブラリ整理~約数の列挙~

Posted at

こちらの記事でPythonのコードとして書かれていたものを流用したものおよびそれをC++に書き直したものになります。
また、C++のコードを使用する際は、テンプレートと一緒に使うようにお願いします(一緒に使わないと動きません。)。

enumerate_divisors.py
def make_divisors(n):
    divisors=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            divisors.append(i)
            if i!=n//i:
                divisors.append(n//i)
    #約数の小さい順にソートしたい場合
    #divisors.sort()
    #約数の大きい順にソートしたい場合
    #divisors.sort(reverse=True)
    return divisors
enumerate_divisors.cc
vector<ll> divisors;//約数を格納する配列

void make_divisors(ll n){
    FOR(i,1,sqrt(n)){
        if(n%i==0){
            divisors.PB(i);
            if(i!=n/i){
                divisors.PB(n/i);
            }
        }
    }
    //約数の小さい順にソートしたい場合
    //sort(ALL(divisors));
    //約数の大きい順にソートしたい場合
    //sort(ALL(divisors),greater<ll>());
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1