結果
問題 | No.12 限定された素数 |
ユーザー |
![]() |
提出日時 | 2015-03-05 01:59:19 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 60 ms / 5,000 ms |
コード長 | 963 bytes |
コンパイル時間 | 574 ms |
コンパイル使用メモリ | 65,400 KB |
実行使用メモリ | 23,040 KB |
最終ジャッジ日時 | 2024-11-24 07:59:40 |
合計ジャッジ時間 | 3,087 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <iostream> #include <algorithm> #include <sstream> #include <set> #include <vector> using namespace std; int to_bit(int n) { int b = 0; while (n > 0) { b |= (1 << (n % 10)); n /= 10; } return b; } int main(int argc, char *argv[]) { string s; getline(cin, s); int N = atoi(s.c_str()); getline(cin, s); stringstream ss(s); int bits = 0; for (int i = 0; i < N; ++i) { int d; ss >> d; bits |= (1 << d); } int ans = -1; int current_bits = 0; int low = 1; int primes = 0; static int p[5000001] = {}; for (int i = 2; i <= 5000000; ++i) { if (!p[i]) { int b = to_bit(i); if ((b & bits) != b) { if (bits == current_bits) { ans = max(ans, (i - 1 - low)); } current_bits = 0; low = i + 1; } else { current_bits |= b; } for (int j = i * 2; j <= 5000000; j += i) { p[j] = 1; } } } if (bits == current_bits) { ans = max(ans, (5000000 - low)); } cout << ans << endl; return 0; }