結果

問題 No.811 約数の個数の最大化
ユーザー dsm4up2cdsm4up2c
提出日時 2019-04-12 21:53:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 80,616 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 19:54:04
合計ジャッジ時間 2,821 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <numeric>
#include <cmath>

using namespace std;

typedef long long int ll;
typedef pair<int,int> P;

#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;
const ll MAXN = 1e9;

map<int, int> prime_factor(int n)
{
    map<int, int> res;

    for(int i = 2; i * i <= n; i++)
    {
        while(n % i == 0)
        {
            ++res[i];
            n /= i;
        }
    }

    if (n != 1)
    {
        res[n] = 1;
    }

    return res;
}

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

    map<int,int> factor_n = prime_factor(n);
    
    int ans = n,num = -1;
    int mi=1,cnt=0;
    int res = n;
    for(int i = 2; i*i <= n; i++){
        if(factor_n.find(i)!=factor_n.end()){
            while(res%i==0 && cnt<k){
                res /= i;
                cnt++;
                mi *= i;
            }
        }
    }

    for(int i = n-1; i >=mi; i--){
        map<int,int> factor_i = prime_factor(i);
        cnt = 0;
        res = 1;
        for(int j = 2; j*j <= n; j++){
            if(factor_n.find(j) != factor_n.end() && factor_i.find(j) != factor_i.end()){
                cnt += min(factor_n[j],factor_i[j]);
            }
            if(factor_i.find(j)!=factor_i.end()) res *= factor_i[j]+1;
        }

        if(cnt>=k && num<=res){
            ans = i;
            num = res;
        }
    }

    cout <<ans<<endl;
    return 0;
}
0