結果
| 問題 |
No.811 約数の個数の最大化
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2019-04-13 01:45:49 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 18 ms / 2,000 ms |
| コード長 | 2,225 bytes |
| コンパイル時間 | 839 ms |
| コンパイル使用メモリ | 91,040 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-15 06:34:25 |
| 合計ジャッジ時間 | 1,463 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:100:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
100 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-
*
* 811.cc: No.811 約数の個数の最大化 - yukicoder
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_P = 1000;
/* typedef */
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
/* global variables */
bool primes[MAX_P + 1];
/* subroutines */
int gen_primes(int maxp, vi &pnums) {
memset(primes, true, sizeof(primes));
primes[0] = primes[1] = false;
int p;
for (p = 2; p * p <= maxp; p++)
if (primes[p]) {
pnums.push_back(p);
for (int q = p * p; q <= maxp; q += p) primes[q] = false;
}
for (; p <= maxp; p++)
if (primes[p]) pnums.push_back(p);
return (int)pnums.size();
}
void prime_decomp(int n, vi &pnums, vpii& pds) {
pds.clear();
for (vi::iterator vit = pnums.begin();
vit != pnums.end() && *vit * *vit <= n; vit++) {
int &pi = *vit, fi = 0;
while (n % pi == 0) n /= pi, fi++;
if (fi > 0) pds.push_back(pii(pi, fi));
}
if (n > 1) pds.push_back(pii(n, 1));
}
int count_pds(vpii &pds) {
int c = 0;
for (vpii::iterator vit = pds.begin(); vit != pds.end(); vit++)
c += vit->second;
return c;
}
int count_dvs(vpii &pds) {
int c = 1;
for (vpii::iterator vit = pds.begin(); vit != pds.end(); vit++)
c *= vit->second + 1;
return c;
}
int gcd(int m, int n) { // m >= n > 0
while (n > 0) {
int r = m % n;
m = n;
n = r;
}
return m;
}
/* main */
int main() {
vi pnums;
int pn = gen_primes(MAX_P, pnums);
//printf("pn=%d\n", pn);
int n, k;
scanf("%d%d", &n, &k);
vpii npds, mpds;
prime_decomp(n, pnums, npds);
int maxm = 0, maxdvc = 0;
for (int m = 2; m < n; m++) {
int g = gcd(n, m);
prime_decomp(g, pnums, mpds);
if (count_pds(mpds) >= k) {
prime_decomp(m, pnums, mpds);
int dvc = count_dvs(mpds);
if (maxdvc < dvc) maxdvc = dvc, maxm = m;
}
}
printf("%d\n", maxm);
return 0;
}
tnakao0123