首页 > 其他 > 详细

CodeForces练习(4)

时间:2020-10-29 09:34:27      阅读:42      评论:0      收藏:0      [点我收藏+]

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 n×n 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 n. He asks you to find any prime square of size n×n. Sasha is absolutely sure such squares exist, so just help him!

Input
The first line contains a single integer t (1≤t≤10) — the number of test cases.

Each of the next t lines contains a single integer n (2≤n≤100) — the required size of a square.

Output
For each test case print n lines, each containing n integers — the prime square you built. If there are multiple answers, print any.

解法思路:

题目大意就是一个n*n的方阵,每个元素都必须是非素数,但是每行每列加起来必须是素数,输出一种即可

考虑两种情况,第一种n为素数,注意到方阵的元素是可以重复的,那这个情况就很简单,直接打印一个每个都是1的方阵即可。
第二种情况,如果n不是素数,那就有一点点麻烦,我们必须要找到一个非素数,还要满足 n-1+此非素数=一个素数 。经过验证可以发现我们必然可以找到这个数的。大概是因为素数的分部比较稀疏,所以非素数很密集?我数学很差我不知道。。。。。。别骂我。。。。。。然后把这个数放在方阵的对角线输出,其他地方为1即可。

考虑完后就很简单,写一个判断素数的函数,然后照着思路写就完事儿了(这里我还犯了个蠢,直接可以输出1或者那个特定的数字的,傻逼的我还写了个数组,依次填进去再输出。。。。。。太丑了我去。

另附代码链接:https://paste.ubuntu.com/p/z6N3d2H2NQ/

CodeForces练习(4)

原文:https://www.cnblogs.com/yuanqie1024/p/13894479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!