思路
设$x$为$n-1$,每行和为$y$,$z=y-x$,且$y$是素数,$z$是合数,则下面的矩阵满足条件:
C++ 代码
#include <iostream>
using namespace std;
const int N = 10000;
int prime[N];
bool st[N];
int cnt;
void get_prime() {
cnt = 0;
st[1] = true;
for(int i = 2; i < N; i++) {
if(!st[i]) prime[cnt++] = i;
for(int j = 0; i * prime[j] < N;j++) {
st[prime[j] * i] = true;
if(i % prime[j] == 0) break;
}
}
return;
}
int main () {
int t;
cin >> t;
get_prime();
while (t --) {
int n;
cin >> n;
int x = n - 1;
int y = n;
while (!st[y - x] || st[y]) {
y++;
}
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
if (i == j) cout << y - x << " ";
else cout << 1 << " ";
}
cout << endl;
}
}
return 0;
}
同构造方法
矩阵是本地latex编译的嘛