結果

問題 No.12 限定された素数
ユーザー snrnsidysnrnsidy
提出日時 2021-04-22 02:46:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,342 ms / 5,000 ms
コード長 1,915 bytes
コンパイル時間 1,795 ms
コンパイル使用メモリ 123,028 KB
実行使用メモリ 203,792 KB
最終ジャッジ日時 2023-09-17 09:51:28
合計ジャッジ時間 26,481 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 792 ms
203,520 KB
testcase_01 AC 822 ms
203,532 KB
testcase_02 AC 820 ms
203,532 KB
testcase_03 AC 1,223 ms
203,532 KB
testcase_04 AC 787 ms
203,560 KB
testcase_05 AC 1,004 ms
203,756 KB
testcase_06 AC 938 ms
203,576 KB
testcase_07 AC 819 ms
203,536 KB
testcase_08 AC 838 ms
203,588 KB
testcase_09 AC 799 ms
203,516 KB
testcase_10 AC 842 ms
203,592 KB
testcase_11 AC 791 ms
203,532 KB
testcase_12 AC 1,178 ms
203,516 KB
testcase_13 AC 1,068 ms
203,584 KB
testcase_14 AC 1,093 ms
203,672 KB
testcase_15 AC 1,073 ms
203,532 KB
testcase_16 AC 1,342 ms
203,528 KB
testcase_17 AC 816 ms
203,572 KB
testcase_18 AC 766 ms
203,532 KB
testcase_19 AC 758 ms
203,556 KB
testcase_20 AC 816 ms
203,736 KB
testcase_21 AC 931 ms
203,792 KB
testcase_22 AC 769 ms
203,552 KB
testcase_23 AC 777 ms
203,584 KB
testcase_24 AC 807 ms
203,532 KB
testcase_25 AC 775 ms
203,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <unordered_set>
#include <fstream>

using namespace std;

bool check[10];
int arr[5000001][10];
void eratos()
{
	bool isprime[5000001];
	memset(isprime, true, sizeof(isprime));

	for (int i = 2; i <= 5000000; i++)
	{
		if (isprime[i])
		{
			int temp = i;
			while (temp > 0)
			{
				arr[i][temp % 10]++;
				temp /= 10;
			}
			for (int j = 2 * i; j <= 5000000; j += i)
			{
				isprime[j] = false;
			}
		}
	}
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, t;

	memset(check, false, sizeof(check));
	memset(arr, 0, sizeof(arr));

	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> t;
		check[t] = true;
	}

	eratos();

	int res = -1;

	for (int i = 1; i <= 5000000; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			arr[i][j] += arr[i - 1][j];
		}
	}

	for (int i = 1; i <= 5000000; i++)
	{
		int lo = i;
		int hi = 5000000;
		int val = -1;
		while (lo <= hi)
		{
			int mid = (lo + hi) / 2;
			bool ok = true;
			for (int j = 0; j < 10; j++)
			{
				if (check[j] == false && arr[mid][j] - arr[i - 1][j] > 0)
				{
					ok = false;
					break;
				}
			}
			if (ok)
			{
				val = max(val, mid);
				lo = mid + 1;
			}
			else
			{
				hi = mid - 1;
			}
		}
		if (val != -1)
		{
			bool ok = true;
			for (int j = 0; j < 10; j++)
			{
				if (check[j])
				{
					if (arr[val][j] - arr[i - 1][j] == 0)
					{
						ok = false;
						break;
					}
				}
			}
			if (ok)
			{
				//cout << i << ' ' << val << '\n';
				res = max(res, val - i);
			}
		}
	}

	cout << res << '\n';

	return 0;
}
0