結果

問題 No.811 約数の個数の最大化
ユーザー rpy3cpprpy3cpp
提出日時 2019-04-14 00:44:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,217 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 167,404 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 14:43:13
合計ジャッジ時間 2,388 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> factorize(int N){
    vector<int> fs;
    for (int f = 2; f*f <= N; ++f){
        while (N % f == 0){
            fs.push_back(f);
            N /= f;
        }
    }
    if (N > 1) fs.push_back(N);
    return fs;
}

bool has_K_common_factors(int n, int K, const vector<int> &fs){
    for (auto f : fs){
        if (n % f == 0){
            n /= f;
            --K;
        }
    }
    return K <= 0;
}

int count_divisors(int n){
    int n_div = 1;
    for (int f = 2; f*f <= n; ++f){
        int c = 1;
        while (n % f == 0){
            ++c;
            n /= f;
        }
        n_div *= c;
    }
    if (n > 1) n_div *= 2;
    return n_div;
}

int solve(int N, int K){
    int max_n_div = 1;
    int ans = 1;
    auto fs = factorize(N);
    for (int i = 2; i < N; ++i){
        if (has_K_common_factors(i, K, fs)){
            int n_div = count_divisors(i);
            if (n_div > max_n_div){
                ans = i;
                max_n_div = n_div;
            }
        }
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(0);
    int N, K;
    cin >> N >> K;
    cout << solve(N, K) << endl;
    return 0;
}
0