# 10月26日 CF#678 (Div. 2)强化训练 B_Prime Square

Codeforces Round #678 (Div. 2)

B_Prime Square

题目链接在此!

B. Prime Square
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square.
A square of size 𝑛×𝑛 is called prime if the following three conditions are held simultaneously:
all numbers on the square are non-negative integers not exceeding 105;
there are no prime numbers in the square;
sums of integers in each row and each column are prime numbers.
Sasha has an integer 𝑛. He asks you to find any prime square of size 𝑛×𝑛. Sasha is absolutely sure such squares exist, so just help him!
Input
The first line contains a single integer 𝑡 (1≤𝑡≤10) — the number of test cases.
Each of the next 𝑡 lines contains a single integer 𝑛 (2≤𝑛≤100) — the required size of a square.
Output
For each test case print 𝑛 lines, each containing 𝑛 integers — the prime square you built. If there are multiple answers, print any.
Example
inputCopy
2
4
2
outputCopy
4 6 8 1
4 9 9 9
4 10 10 65
1 4 4 4
1 1
1 1

大概题意:

意思是给你个n,你要输出一个n阶方阵,要求内部元素没有素数,但是各行各列加起来都为素数。

思路分析:

一开始想了半天的素数凑来凑去的方法。
发现没有推广意义。
最后想到内部元素没有素数意思是内部元素可以有1,那就简单了。

if (n是素数) 直接元素全部输出1
else 主对角线元素改变一下,其他全部为1。

代码如下:

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <set>
#include <cstdio>

typedef long long ll;

using namespace std;

bool isprime(int m) 
//自己别把1是素数还是0是素数搞混了啊呜呜呜
{
    bool flag = 1;
    for (int i = 2; i * i <= m; i++)
        if (m % i == 0) {
            flag = 0;
            break;
        }
    return flag;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        if (isprime(n)) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++)
                    cout << 1 << " ";
                cout << endl;
            }
        }
        else {
            int temp;
            for(temp =1;;temp++){
                if(isprime(n-1+temp)&&!isprime(temp))
                //要求很明显,见题意。
                //也就是行列和(这里都一样了)为素数。
                //但是元素本身不能是素数。
                {
                    break;
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++){
                if(i==j&&j!=(n-1)) cout<<temp<<" ";
               else if(i==j&&j==(n-1))  cout<<temp;
               else cout<<1<<" ";
                }
                cout<<endl;
            }
        }
    }
}

反思:

智商检测题名不虚传。
还是自己太笨了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值