結果

問題 No.811 約数の個数の最大化
ユーザー simansiman
提出日時 2020-09-15 12:44:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,341 bytes
コンパイル時間 878 ms
コンパイル使用メモリ 97,956 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 01:40:05
合計ジャッジ時間 1,633 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;
const int MAX_N = 100010;

int spf[MAX_N];

int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}

void init_table(int N) {
  memset(spf, 0, sizeof(spf));
  spf[1] = 1;

  for (int i = 2; i <= N; ++i) {
    if (spf[i] != 0) continue;

    for (int j = i; j <= N; j+= i) {
      spf[j] = i;
    }
  }
}

int factor_count(int n) {
  int cnt = 0;
  while (n > 1) {
    int f = spf[n];
    n /= f;
    cnt++;
  }

  return cnt;
}

int divisor_count(int n) {
  map<int, int> counter;

  while (n > 1) {
    int f = spf[n];
    counter[f]++;
    n /= f;
  }

  int cnt = 1;

  for (auto it : counter) {
    cnt *= it.second + 1;
  }

  return cnt;
}

int main() {
  int N, K;
  cin >> N >> K;

  init_table(N);
  int max_dc = 0;
  int ans = 0;

  for (int i = 1; i < N; ++i) {
    int g = gcd(i, N);
    int fc = factor_count(g);
    // fprintf(stderr, "i: %d, g: %d, fc: %d\n", i, g, fc);
    if (fc < K) continue;
    int dc = divisor_count(i);

    if (max_dc < dc) {
      max_dc = dc;
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}
0