Codeforce Problem 1436B Square//constructive algorithms, math

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.

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*N的矩阵
矩阵的每一个数都不是素数
且每行每列之和都是素数
输出任意一个答案即可

思路

刚开始对N*N的矩阵的元素全赋值1
然后修改主对角线上的值
符合题意即可输出

Code

#include"iostream"
#include"cstdio"
#include"stdlib.h"
#include"cstring"
#include"cstdlib"
#include"vector"
#include"map"
#include"algorithm"
#include <iomanip>
#include <time.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include<bits/stdc++.h>
//const double PI=acos(-1.0);
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define inf 0x3f3f3f3f
#define pr pair<int,int>
#define ios ios::sync_with_stdio(false);
typedef pair<char,int> PAIR;
struct CmpByValue
{
    bool operator()(const PAIR& lhs, const PAIR& rhs)
    {
        return lhs.second > rhs.second;
    }
};
typedef long long ll;
//priority_qe<int,vector<int>,greater<int>> pp;
bool JudgePrime(ll digit)
{
   // cout<<"002:"<<endl;
    if(digit==2 || digit==3)
        return true;
    else if(digit<=1 || digit%2==0)
        return false;
    else if(digit>3)
    {
        for(int i=3; i*i<=digit; i+=2)
            if(digit%i==0)
                return false;
        return true;
    }
}
const int mod = 1000000007;
const int M = 1e2 +100 ;
const int limt = 1<<20;
const int INF = 1e9;
 
int mp[M][M];
double sum;
bool flag;
ll t,s,n,m,ans,cnt;
void inti()
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            mp[i][j]=1;
}
void fun()
{
    for(int i=1; i<=n; i++)
            mp[i][i]++;
}
int main()
{
    ios;
    cin>>t;
    ll k=1;
    while(t--)
    {
        flag=0;
        cin>>n;
        inti();
        while(!flag)
        {
            sum=0;
            sum+=mp[1][1]+n-1;
            if(JudgePrime(sum)&&!JudgePrime(mp[1][1]))
                flag=1;
            else fun();
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(j!=n)
                    cout<<mp[i][j]<<" ";
                else
                    cout<<mp[i][j]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值