結果

問題 No.811 約数の個数の最大化
ユーザー toyamatoyama
提出日時 2019-04-12 22:03:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 820 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 51,812 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 20:27:14
合計ジャッジ時間 3,212 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 15 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 8 ms
4,352 KB
testcase_09 AC 12 ms
4,348 KB
testcase_10 AC 6 ms
4,352 KB
testcase_11 AC 12 ms
4,352 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 46 ms
4,348 KB
testcase_14 AC 14 ms
4,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:31:10: warning: ‘ans’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  cout << ans << endl;
          ^~~

ソースコード

diff #

#include <iostream>
using namespace std;

int p_gcd(int m);
int gcd(int m, int n);
int root(int n);

int main()
{
	int n;
	int k;
	int ans;
	int m = 0;

	cin >> n >> k;

	for (int i = 2; i < n; i++) {

		if (p_gcd(gcd(i, n)) >= k) {

			int r = root(i);

			if (r > m) {
				m = r;
				ans = i;
			}

		}
	}

	cout << ans << endl;

	return 0;
}

int p_gcd(int m)
{
	int cnt = 0;

	int i = 2;
	while (m > 1) {
		if (m % i == 0) {
			cnt++;
			m /= i;
		} else {
			i++;
		}
	}

	return cnt;
}

int gcd(int m, int n)
{
	if (m < n) {
		int tmp = m;
		m = n;
		n = tmp;
	}

	int r;
	while (n) {
		r = m % n;
		m = n;
		n = r;
	}

	return m;
}

int root(int n)
{
	int cnt = 0;

	for (int i = 1; i * i <= n; i++) {
		if (n % i == 0) {

			if (i * i == n ) {
				cnt += 1;
			} else {
				cnt += 2;
			}
		}
	}

	return cnt;
}
0