题目
构造一个n*n(n<=1e3)的矩阵,恰用到1到n*n这n*n个数,
使得相邻两项之和不是质数
思路来源
官方题解/zhoukangyang代码/SSerxhs代码/Heltion代码
题解
自己虽然搞过去了,但是很复杂,多学学别人怎么写的
首先想到是奇数分一堆偶数分一堆,特判n=3后,分奇偶直接搞也可做
简洁的做法是特判n=3,4,5,然后n>=6用奇数和偶数中3的倍数做两堆数的分隔
此时奇数里3的倍数,偶数里3的倍数已经能独占一行了
代码1
中规中矩,n=3,4,5构造,n>=6按规则排一下序,很简洁
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define ull unsigned long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
using namespace std;
const int N = 1e6 + 7, mod = 998244353, inv3 = (mod + 1) / 3;
int n, m, w[N], ord[N];
int main () {
ios :: sync_with_stdio(false);
cin.tie (0); cout.tie (0);
cin >> n;
if(n == 3) {
cout << "5 4 8\n";
cout << "3 6 2\n";
cout << "1 9 7\n";
return 0;
}
if(n == 4) {
cout << "15 11 16 12\n";
cout << "13 3 6 9\n";
cout << "14 7 8 1\n";
cout << "4 2 10 5\n";
return 0;
}
if(n == 5) {
cout << "1 3 5 7 9\n";
cout << "11 13 15 17 19\n";
cout << "23 25 21 4 14\n";
cout << "10 8 12 24 22\n";
cout << "18 20 2 16 6\n";
return 0;
}
L(i, 1, n * n) {
if(i % 2 == 0) w[i] = 1e9 - (i % 3 == 0);
else w[i] = (i % 3 == 0);
ord[i] = i;
}
sort(ord + 1, ord + n * n + 1, [&] (int x, int y) {
return w[x] < w[y];
});
L(i, 1, n * n) cout << ord[i] << " \n"[i % n == 0];
return 0;
}
/*
6 2 5
5 2 3 1 4 6
*/
代码2
n=3,4,5直接暴力随机置换至有解
//这回只花了114514min就打完了。
//真好。记得多手造几组。ACM拍什么拍。
#include "bits/stdc++.h"
using namespace std;
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
template<typename typC> bool cmin(typC &x,const typC &y) { if (x>y) { x=y; return 1; } return 0; }
template<typename typC> bool cmax(typC &x,const typC &y) { if (x<y) { x=y; return 1; } return 0; }
#if !defined(ONLINE_JUDGE)&&defined(LOCAL)
#include "my_header\debug.h"
#else
#define dbg(...) ;
#define dbgn(...) ;
#endif
typedef unsigned int ui;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
// void inc(ui &x,const ui &y) { if ((x+=y)>=p) x-=p; }
// void dec(ui &x,const ui &y) { if ((x+=p-y)>=p) x-=p; }
namespace pr
{
typedef long long ll;
typedef __int128 lll;
typedef pair<ll,int> pa;
ll ksm(ll x,ll y,const ll p)
{
ll r=1;
while (y)
{
if (y&1) r=(lll)r*x%p;
x=(lll)x*x%p; y>>=1;
}
return r;
}
namespace miller
{
const int p[7]={2,3,5,7,11,61,24251};
ll s,t;
bool test(ll n,int p)
{
if (p>=n) return 1;
ll r=ksm(p,t,n),w;
for (int j=0; j<s&&r!=1; j++)
{
w=(lll)r*r%n;
if (w==1&&r!=n-1) return 0;
r=w;
}
return r==1;
}
bool prime(ll n)
{
if (n<2||n==46'856'248'255'981ll) return 0;
for (int i=0; i<7; ++i) if (n%p[i]==0) return n==p[i];
s=__builtin_ctz(n-1); t=n-1>>s;
for (int i=0; i<7; ++i) if (!test(n,p[i])) return 0;
return 1;
}
}
using miller::prime;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
namespace rho
{
void nxt(ll &x,ll &y,ll &p) { x=((lll)x*x+y)%p; }
ll find(ll n,ll C)
{
ll l,r,d,p=1;
l=rnd()%(n-2)+2,r=l;
nxt(r,C,n);
int cnt=0;
while (l^r)
{
p=(lll)p*llabs(l-r)%n;
if (!p) return gcd(n,llabs(l-r));
++cnt;
if (cnt==127)
{
cnt=0;
d=gcd(llabs(l-r),n);
if (d>1) return d;
}
nxt(l,C,n); nxt(r,C,n); nxt(r,C,n);
}
return gcd(n,p);
}
vector<pa> w;
vector<ll> d;
void dfs(ll n,int cnt)
{
if (n==1) return;
if (prime(n)) return w.emplace_back(n,cnt),void();
ll p=n,C=rnd()%(n-1)+1;
while (p==1||p==n) p=find(n,C++);
int r=1; n/=p;
while (n%p==0) n/=p,++r;
dfs(p,r*cnt); dfs(n,cnt);
}
vector<pa> getw(ll n)
{
w=vector<pa>(0); dfs(n,1);
if (n==1) return w;
sort(w.begin(),w.end());
int i,j;
for (i=1,j=0; i<w.size(); i++) if (w[i].first==w[j].first) w[j].second+=w[i].second; else w[++j]=w[i];
w.resize(j+1);
return w;
}
void dfss(int x,ll n)
{
if (x==w.size()) return d.push_back(n),void();
dfss(x+1,n);
for (int i=1; i<=w[x].second; i++) dfss(x+1,n*=w[x].first);
}
vector<ll> getd(ll n)
{
getw(n); d=vector<ll>(0); dfss(0,1);
sort(d.begin(),d.end());
return d;
}
}
using rho::getw,rho::getd;
using miller::prime;
}
using pr::getw,pr::getd;
const int N=1e6+5;
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cout<<setiosflags(ios::fixed)<<setprecision(15);
int n,i,j,k;
cin>>n;
int m=n*n;
vector a(n,vector(n,0));
if (n>=6)
{
for (i=j=0,k=1; k<=m; k+=2) if (k%3)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
for (k=1; k<=m; k+=2) if (k%3==0)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
for (k=2; k<=m; k+=2) if (k%3==0)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
for (k=2; k<=m; k+=2) if (k%3)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
}
else
{
for (i=j=0,k=1; k<=m; k+=2) if (k%3)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
for (k=1; k<=m; k+=2) if (k%3==0)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
for (k=2; k<=m; k+=2) if (k%3==0)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
for (k=2; k<=m; k+=2) if (k%3)
{
a[i][j]=k;
if (++j==n) ++i,j=0;
}
mt19937 rnd(345);
while (1)
{
bool flg=0;
for (i=0; i<n; i++) for (j=0; j<n; j++) if (j+1<n&&pr::prime(a[i][j]+a[i][j+1])||i+1<n&&pr::prime(a[i][j]+a[i+1][j]))
{
swap(a[i][j],a[rnd()%n][rnd()%n]);
flg=1;
}
if (!flg) break;
}
}
for (auto &v:a) cout<<v<<'\n';
/*for (i=0; i<n; i++) for (j=0; j<n; j++)
{
dbg(i,j);
if (j+1<n) assert(!pr::prime(a[i][j]+a[i][j+1]));
if (i+1<n) assert(!pr::prime(a[i][j]+a[i+1][j]));
}*/
}
代码3
暴力:n为奇数,把中心一填1 7 8 2一填,然后解决一行分界线;n为偶数,解决一行分界线
奇 1 奇
奇 7 8
偶 2 偶
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int p(int n) {
for (int i = 2; i * i <= n; i += 1)
if (n % i == 0)
return 1;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
int N;
cin >> N;
vector A(N, vector<int>(N));
array<set<int>, 2> S;
for (int i = 1; i <= N * N; i += 1)
S[i % 2].insert(i);
vector<pair<int, int>> vp;
if (N % 2 == 1) {
A[N / 2][N / 2] = 7;
A[N / 2][N / 2 + 1] = 8;
A[N / 2 + 1][N / 2] = 2;
A[N / 2 - 1][N / 2 + 1] = 1;
S[1].erase(1);
S[1].erase(7);
S[0].erase(2);
S[0].erase(8);
for (int i = 0; i < N / 2; i += 1)
vp.emplace_back(N / 2, i);
for (int i = N / 2 + 2; i < N; i += 1)
vp.emplace_back(N / 2 - 1, i);
}
else for (int i = 0; i < N; i += 1)
vp.emplace_back(N / 2 - 1, i);
int n = N * N * 2;
for (auto [x, y] : vp) {
int si = -1, sj = -1;
for (int i : S[0])
if (si == -1) {
for (int j : S[1])
if (p(i + j)) {
si = i;
sj = j;
break;
}
}
else break;
S[0].erase(si);
S[1].erase(sj);
A[x][y] = sj;
A[x + 1][y] = si;
}
for (auto& Ai : A) {
for (int& Aij : Ai) {
if (Aij == 0) {
int k = not S[1].empty();
Aij = *S[k].begin();
S[k].erase(Aij);
}
cout << Aij << " ";
}
cout << "\n";
}
}
429




被折叠的 条评论
为什么被折叠?



