1436B - Prime Square - CodeForces Solution


constructive algorithms math *900

Please click on ads to support us..

Python Code:

import math
def isprime(t):
    for i in range(2,int(math.sqrt(t))+1):
        if t%i==0:
            return False
    return True
def hi(t):
    r=1
    while(True):
        if not(isprime(r)):
            if(isprime(((t-1)*(t))+r)):
                return r
        r+=1

for _ in range(int(input())):
    n=int(input())
    if n==1:print(1)
    elif isprime(n):
        for i in range(n):
            for j in range(n):
                print(1,end=' ')
            print()
    else:
                                x=hi(n)
        for i in range(n):
            for j in range(n):
                if i==j:print(x,end=' ')
                else:print(n,end=' ')
            print()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a, b, c, i, j, k, m, n, o, l, r, f = 0, cnt = 0, sum = 0, total = 0, mn = INT_MAX, mx = INT_MIN, sz, len, temp, pos, tc, x, y, z, arr[100000], brr[120];
    vector<long long int> vrr;
    string s;

    cin>>tc;
    while(tc--)
    {
        cin>>n;
        f = 0;
        map<int, int> mp;
        for( i = 2; i < 200 ; i++ )
        {
            f = 1;
            for( j = 2 ; j < i ; j++ )
            {
                if( i%j == 0 ) f = 0;
            }
            if(f) mp[i] = 1;
        }

        if( mp[n] )
        {
            for( i = 1; i<= n; i++ )
            {
                for( j = 1; j <= n; j++ )
                {
                  cout<< 1 << " ";

                }
                cout<< endl;
            }
        }
        else
        {
            for( i = n+1; 1 ; i++ )
            {
                if( mp[i] )
                {
                    x =i - (n-1);
                    if( mp[x] == 0 ) break;
                }
            }

            for( i = 1; i<= n; i++ )
            {
                for( j = 1; j <= n; j++ )
                {
                    if( i == j ) cout<< x << " ";
                    else cout<< 1 << " ";
                }
                cout<< endl;
            }
        }
    }



}


Comments

Submit
0 Comments
More Questions

49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap