結果

問題 No.811 約数の個数の最大化
ユーザー Yoshinari TakaokaYoshinari Takaoka
提出日時 2019-04-13 03:51:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,964 bytes
コンパイル時間 1,221 ms
コンパイル使用メモリ 146,480 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 09:29:25
合計ジャッジ時間 2,034 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 18 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 4 ms
4,352 KB
testcase_08 AC 9 ms
4,348 KB
testcase_09 AC 11 ms
4,348 KB
testcase_10 AC 7 ms
4,348 KB
testcase_11 AC 12 ms
4,348 KB
testcase_12 AC 5 ms
4,352 KB
testcase_13 AC 42 ms
4,348 KB
testcase_14 AC 17 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int gcd_primefactor(int one, int other) {
    int64_t tmp;
    while (one != 0) {
        tmp = one;
        one = other % one;
        other = tmp;
    }

    vector<int> arr;
    for (int i = 2; i * i <= other; i++) {
        while (other % i == 0) {
            arr.push_back(i);
            other /= i;
        }
    }
    if (other > 1) {
        arr.push_back(other);
    }

    return arr.size();
}

int main(int argc, char *argv[]) {
    int N, K;
    cin >> N >> K;

    //
    //  元々の浅知恵
    //
    //  1. 素因数分解(個数をLとする)
    //  2. K 個以上L個未満の素因数の組み合わせを列挙
    //    - そもそもこの時点で大変...
    //  3. 上記の素因数の倍数を N以下で列挙
    //  4. 3. の約数の数のうち、最大のものを出力
    //

    //
    //  想定解
    //
    //  2. 1 .. N -1 までの自然数を全探索
    //    2-1. 各自然数に付き、共通の素因数の数を判定
    //         これは、N との最大公約数の素因数の数で判定できる
    //    2-2. 上記が K 以上の数なら、約数を列挙
    //         約数の数が最大のものを答えにする
    //

    int max_count = 0;
    int ans = 0;
    for (int j = 1; j < N; j++) {
        int n = gcd_primefactor(j, N);
        int count = 0;
        if (n >= K) {
            for (int k = 1; k * k <= j; k++) {
                if (j % k == 0) {
                    count++;
                    if (k != 1 && k * k != j) {
                        //  約数をひとつ求めたら、もう片方の
                        //  operandも忘れずに数え上げること
                        count++;
                    }
                }
            }
            if (count > max_count) {
                max_count = count;
                ans = j;
            }
        }
    }
    cout << ans << endl;
	return 0;
}
0