結果

問題 No.12 限定された素数
ユーザー kk
提出日時 2014-10-30 13:33:04
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,032 bytes
コンパイル時間 660 ms
コンパイル使用メモリ 75,328 KB
実行使用メモリ 23,488 KB
最終ジャッジ日時 2023-08-28 23:15:57
合計ジャッジ時間 6,995 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 191 ms
23,268 KB
testcase_01 AC 192 ms
23,104 KB
testcase_02 AC 191 ms
23,112 KB
testcase_03 AC 197 ms
23,268 KB
testcase_04 AC 195 ms
23,060 KB
testcase_05 AC 194 ms
23,152 KB
testcase_06 AC 195 ms
23,412 KB
testcase_07 AC 194 ms
23,272 KB
testcase_08 AC 193 ms
23,276 KB
testcase_09 AC 194 ms
23,156 KB
testcase_10 AC 195 ms
23,108 KB
testcase_11 AC 194 ms
23,056 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 196 ms
23,228 KB
testcase_18 AC 195 ms
23,216 KB
testcase_19 AC 195 ms
23,216 KB
testcase_20 AC 194 ms
23,148 KB
testcase_21 AC 201 ms
23,140 KB
testcase_22 AC 198 ms
23,052 KB
testcase_23 AC 197 ms
23,148 KB
testcase_24 AC 196 ms
23,468 KB
testcase_25 AC 200 ms
23,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <string>
#include <cstring>

using namespace std;

typedef long long ll;

bool not_prime[5000001];
int memo[10][348515];

int main() {
  int n;
  cin >> n;
  int a[n];
  bool ba[10];
  memset(ba, false, sizeof(ba));
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) ba[a[i]] = true;


  ll cnt = 0;
  for (int i = 2; i <= 5000000; i++) {
    for (int j = i+i; j <= 5000000; j+=i) {
      not_prime[j] = true;
    }
  }

  vector <int> prime;
  for (int i = 2; i <= 5000000; i++) if (!not_prime[i]) prime.push_back(i);

  memset(memo, 0, sizeof(memo));
  for (int i = 1; i <= prime.size(); i++) {
    for (int j = prime[i-1]; j != 0; j/=10) {
      memo[j%10][i]++;
    }
    for (int j = 0; j < 10; j++) memo[j][i+1] += memo[j][i];
  }

  int ans = -1;
  for (int l = 1, r = 1; r <= prime.size(); r++) {
    // bool one = true;
    // for (int i = 0; i < 10; i++) {
    //   if ((memo[i][r]-memo[i][r-1] == 0 && !ba[i]) ||
    // 	  ())
    // }

    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (memo[a[i]][r]-memo[a[i]][l-1] == 0) {
	ok = false;
	break;
      }
    }
    if (ok) {
      bool yes = true;
      for (; l <= r; l++) {
	yes = true;
	for (int j = 0; j < 10; j++) {
	  if ((memo[j][r]-memo[j][l-1] == 0 && ba[j]) ||
	      (memo[j][r]-memo[j][l-1] > 0 && !ba[j])) {
	    yes = false;
	    break;
	  }
	}
	if (yes) break;
      }
      if (yes) {
	//	cout << l << " " << r << endl;
	int tl = prime[l-1], tr = prime[r-1];
	if (l-1 == 0) {
	  tl = 1;
	}else {
	  tl = prime[l-1-1]+1;
	}
	if (r == prime.size()) {
	  tr = 5000000;
	}else {
	  tr = prime[r-1+1]-1;
	}
	ans = max(ans, tr-tl);
      }else {
	l = r;
      }
    }
  }
  cout << ans << endl;
}
0