結果

問題 No.811 約数の個数の最大化
ユーザー mamekinmamekin
提出日時 2019-04-15 22:43:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,600 bytes
コンパイル時間 1,197 ms
コンパイル使用メモリ 118,492 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 06:52:53
合計ジャッジ時間 2,000 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

int gcd(int a, int b){
    while(b != 0){
        int tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}

void integerFactorization(int n, vector<pair<int, int> >& factor)
{
    factor.clear();
    int a = 2;
    while(a * a <= n){
        int b = 0;
        while(n % a == 0){
            ++ b;
            n /= a;
        }
        if(b > 0)
            factor.push_back(make_pair(a, b));
        ++ a;
    }
    if(n > 1)
        factor.push_back(make_pair(n, 1));
}

int solve(int x, int n, int k)
{
    int y = gcd(x, n);
    vector<pair<int, int> > f;
    integerFactorization(y, f);

    for(const auto& p : f)
        k -= p.second;
    if(k > 0)
        return -1;
    
    integerFactorization(x, f);
    int ans = 1;
    for(const auto& p : f)
        ans *= p.second + 1;
    return ans;
}

int main()
{
    int n, k;
    cin >> n >> k;

    int maxCnt = -1;
    int ans = -1;
    for(int x=1; x<n; ++x){
        int cnt = solve(x, n, k);
        if(maxCnt < cnt){
            maxCnt = cnt;
            ans = x;
        }
    }
    cout << ans << endl;

    return 0;
}
0