結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー kenjiiiHkenjiiiH
提出日時 2016-05-14 01:49:16
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,577 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 77,580 KB
実行使用メモリ 10,012 KB
最終ジャッジ日時 2024-04-15 19:06:19
合計ジャッジ時間 3,459 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,012 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 WA -
testcase_23 AC 22 ms
5,376 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

vector<int> getPrimes(int n) {
  vector<bool> isPrime(n+1, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j <= n; j += i)
      isPrime[j] = false;
  }

  int m = 0;
  for (int i = 0; i <= n; i++)
    if (isPrime[i])
      ++m;

  int pos = 0;
  vector<int> ret(m);
  for (int i = 0; i <= n; i++)
    if (isPrime[i])
      ret[pos++] = i;

  return ret;
}

map<long long, int> factorize(long long n) {
  map<long long, int> ret;
  
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ++ret[i];
      n /= i;
    }
  }
  
  if (n != 1)
    ret[n] = 1;
  
  return ret;
}

int minFac(long long n) {
  map<long long, int> facs = factorize(n);
  long long ret = facs.begin()->first;
  if (ret == n)
    return 1;
  return (int)ret;
}

int main(int argc, char *argv[])
{
  long long l, h;
  cin >> l >> h;

  vector<int> pr = getPrimes(1e3);
  int n = pr.size();
  pair<int, long long> ret = make_pair(0, 0);
  for (int i = n-1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      long long x = pr[i] * pr[j];
      if (l <= x && x <= h) {
        ret = max(ret, make_pair(pr[i], x));
      }
    }
    if (ret.first)
      break;
  }

  if (!ret.first) {
    // this block is executed only if abs(h-l) is enough small.
    for (long long i = l; i <= h; i++) {
      ret = max(ret, make_pair(minFac(i), i));
    }
  }
  
  cout << ret.second << endl;
  
  return 0;
}
0