提出 #35353475


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

const int mxn = 1000 + 5, mxp = 2 * 1000 * 1000 + 5;

using BS = bitset <mxp>;

bool isp[mxp];
BS notp;

int main()
{
	ios::sync_with_stdio(0);
	for (int i = 2; i < mxp; ++ i) isp[i] = 1;
	for (int i = 2; i * i < mxp; ++ i) if (isp[i])
		for (int j = i * i; j < mxp; j += i) isp[j] = 0;
	for (int i = 2; i < mxp; ++ i) if (!isp[i]) notp.set(i);
	int n;
	cin >> n;
	vector <int> v_even;
	for (int i = 2; i <= n * n; i += 2) v_even.push_back(i);
	while (true)
	{
		random_shuffle(v_even.begin(), v_even.end());
		int zz = -1;
		if (n % 2 == 1)
		{
			if (n == 3) zz = 7;
			else zz = n * n;
		}
		static BS full;
		for (int i = 1; i <= n * n; i += 2) if (i != zz) full.set(i);
		vector <pair <int, int> > chose;
		for (int i : v_even)
		{
			static BS temp;
			temp = full & (notp >> i);
			if (temp.any())
			{
				int x = rand() % temp.count();
				int j = temp._Find_first();
				while (x --) j = temp._Find_next(j);
				chose.push_back({i, j});
				full.reset(j);
				if ((int) chose.size() >= n / 2 * 2) break;
			}
		}
		if ((int) chose.size() >= n / 2 * 2)
		{
			if (n % 2 == 1)
			{
				int hav = -1;
				for (int i = 0; i < (int) chose.size(); ++ i)
					if (!isp[chose[i].first + zz])
					{
						hav = i;
						break;
					}
				if (!~hav) continue;
				static bool vis[mxp];
				for (int i = 1; i <= n * n; ++ i) vis[i] = 0;
				for (int i = 0; i < (int) chose.size(); ++ i) vis[chose[i].first] = vis[chose[i].second] = 1;
				int hav1 = -1;
				for (int i = 2; i < n * n; i += 2) if (!vis[i] && !isp[i + zz])
				{
					hav1 = i;
					break;
				}
				if (!~hav1) continue;
				swap(chose[hav], chose[n / 2 - 1]);
				vis[hav1] = 1;
				static int mat[mxn][mxn];
				mat[n / 2][n / 2] = zz;
				mat[n / 2][n / 2 + 1] = hav1;
				for (int i = 0; i < n / 2; ++ i)
				{
					mat[i][n / 2 - 1] = chose[i].second;
					mat[i][n / 2] = chose[i].first;
				}
				for (int i = n / 2 + 1; i < n; ++ i)
				{
					mat[i][n / 2] = chose[i - 1].second;
					mat[i][n / 2 + 1] = chose[i - 1].first;
				}
				vis[zz] = 1;
				int ptr = 1;
				for (int i = 0; i < n; ++ i)
					for (int j = 0; j < n / 2; ++ j) if (!mat[i][j])
					{
						while (vis[ptr]) ptr += 2;
						mat[i][j] = ptr;
						vis[ptr] = 1;
					}
				ptr = 2;
				for (int i = 0; i < n; ++ i)
					for (int j = n / 2 + 1; j < n; ++ j) if (!mat[i][j])
					{
						while (vis[ptr]) ptr += 2;
						mat[i][j] = ptr;
						vis[ptr] = 1;
					}
				for (int i = 0; i < n; ++ i)
					for (int j = 0; j < n; ++ j)
					{
						if (i + 1 < n) assert(!isp[mat[i][j] + mat[i + 1][j]]);
						if (j + 1 < n) assert(!isp[mat[i][j] + mat[i][j + 1]]);
					}
				for (int i = 0; i < n; ++ i)
					for (int j = 0; j < n; ++ j)
						cout << mat[i][j] << " \n"[j + 1 == n];
				return 0;
			} else
			{
				static bool vis[mxp];
				static int mat[mxn][mxn];
				for (int i = 0; i < (int) chose.size(); ++ i)
				{
//					cerr << "	" << chose[i].first << " " << chose[i].second << endl;
					vis[chose[i].first] = vis[chose[i].second] = 1;
					mat[n / 2 - 1][i] = chose[i].first;
					mat[n / 2][i] = chose[i].second;
				}
				int ptr = 2;
				for (int x = 0; x < n / 2 - 1; ++ x)
					for (int y = 0; y < n; ++ y)
					{
						while (vis[ptr]) ptr += 2;
						mat[x][y] = ptr;
						vis[ptr] = 1;
					}
				ptr = 1;
				for (int x = n / 2 + 1; x < n; ++ x)
					for (int y = 0; y < n; ++ y)
					{
						while (vis[ptr]) ptr += 2;
						mat[x][y] = ptr;
						vis[ptr] = 1;
					}
				for (int i = 0; i < n; ++ i)
					for (int j = 0; j < n; ++ j)
					{
						if (i + 1 < n) assert(!isp[mat[i][j] + mat[i + 1][j]]);
						if (j + 1 < n) assert(!isp[mat[i][j] + mat[i][j + 1]]);
					}
				for (int i = 0; i < n; ++ i)
					for (int j = 0; j < n; ++ j)
						cout << mat[i][j] << " \n"[j + 1 == n];
				return 0;
			}
		}
	}
	return 0;
}

提出情報

提出日時
問題 C - Avoid Prime Sum
ユーザ bmb87978
言語 C++ (GCC 9.2.1)
得点 500
コード長 3997 Byte
結果 AC
実行時間 1089 ms
メモリ 17400 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 24
セット名 テストケース
Sample 01_sample_01.txt
All 01_sample_01.txt, 02_small_01.txt, 02_small_02.txt, 02_small_03.txt, 02_small_04.txt, 02_small_05.txt, 02_small_06.txt, 02_small_07.txt, 02_small_08.txt, 03_rand_01.txt, 03_rand_02.txt, 03_rand_03.txt, 03_rand_04.txt, 03_rand_05.txt, 03_rand_06.txt, 03_rand_07.txt, 03_rand_08.txt, 03_rand_09.txt, 03_rand_10.txt, 04_max_01.txt, 04_max_02.txt, 04_max_03.txt, 04_max_04.txt, 04_max_05.txt
ケース名 結果 実行時間 メモリ
01_sample_01.txt AC 23 ms 6348 KiB
02_small_01.txt AC 20 ms 6444 KiB
02_small_02.txt AC 20 ms 6548 KiB
02_small_03.txt AC 23 ms 6392 KiB
02_small_04.txt AC 25 ms 6560 KiB
02_small_05.txt AC 28 ms 6368 KiB
02_small_06.txt AC 20 ms 6512 KiB
02_small_07.txt AC 20 ms 6504 KiB
02_small_08.txt AC 21 ms 6408 KiB
03_rand_01.txt AC 514 ms 11712 KiB
03_rand_02.txt AC 66 ms 7340 KiB
03_rand_03.txt AC 94 ms 7672 KiB
03_rand_04.txt AC 170 ms 8364 KiB
03_rand_05.txt AC 772 ms 14716 KiB
03_rand_06.txt AC 152 ms 8268 KiB
03_rand_07.txt AC 667 ms 13300 KiB
03_rand_08.txt AC 128 ms 8140 KiB
03_rand_09.txt AC 394 ms 10332 KiB
03_rand_10.txt AC 103 ms 7792 KiB
04_max_01.txt AC 1073 ms 17252 KiB
04_max_02.txt AC 1089 ms 17300 KiB
04_max_03.txt AC 1058 ms 17356 KiB
04_max_04.txt AC 1073 ms 17316 KiB
04_max_05.txt AC 1082 ms 17400 KiB