結果

問題 No.12 限定された素数
ユーザー snrnsidysnrnsidy
提出日時 2021-04-22 02:42:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,013 bytes
コンパイル時間 1,283 ms
コンパイル使用メモリ 123,400 KB
実行使用メモリ 10,124 KB
最終ジャッジ日時 2023-09-17 09:50:12
合計ジャッジ時間 15,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘bool eratos()’ 内:
main.cpp:44:1: 警告: 非 void を戻す関数内に return 文がありません [-Wreturn-type]
   44 | }
      | ^

ソースコード

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;

vector <int> prime;
bool eratos()
{
	bool isprime[5000001];
	memset(isprime, true, sizeof(isprime));

	for (int i = 2; i <= 5000000; i++)
	{
		if (isprime[i])
		{
			prime.push_back(i);
			for (int j = 2 * i; j <= 5000000; j += i)
			{
				isprime[j] = false;
			}
		}
	}
}

bool check[10];
int arr[5000001][10];

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

	eratos();

	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;
	}

	int res = -1;

	int m = prime.size();

	for (int i = 0; i < m; i++)
	{
		int x = prime[i];
		while (x > 0)
		{
			arr[prime[i]][x%10]++;
			x /= 10;
		}
	}

	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