1783B - Matrix of Differences - CodeForces Solution


constructive algorithms math

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
   n=int(input())
   q=n
   n=n*n
   l=list(range(1,(n//2)+1))
   s=list(range((n//2)+1,n+1))
   s=s[::-1]
   f=[]
   for i in range(n//2):
     f.append(l[i])
     f.append(s[i])
   if n%2!=0:
     f.append(s[-1])
   c=0
   for k in range(0,n,q):
     c+=1
     if c%2==0:
       print(' '.join(map(str,(f[k:k+q]))))
     else:
       print(' '.join(map(str,(f[k:k+q])[::-1])))
     
     

C++ Code:

#include <bits/stdc++.h>
#define ll long long int
#define lld long double
#define ull unsigned long long int
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
#include <iostream> 
using namespace __gnu_pbds; 
using namespace std; 
ll mod=998244353;
// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag, 
            tree_order_statistics_node_update> 
    ordered_set;
// Declaring ordered_multiset
typedef tree<int, null_type, 
             less_equal<int>, rb_tree_tag, 
             tree_order_statistics_node_update> 
    ordered_mset; 
ll NcR(ll n, ll r) {ll p = 1, k = 1; if (n - r < r)r = n - r; if (r != 0) {while (r) {p *= n; k *= r; ll m = __gcd(p, k); p /= m; k /= m; n--; r--;}} else p = 1; return p;}
ll modaddition(ll a, ll b, ll mod ) {return (a % mod + b % mod) % mod;}
using  namespace std;
void printv(vector<ll>&v)
{
    for(auto a:v)
        cout<<a<<" ";
    cout<<endl;
}
void inputv(vector<ll>&v)
{
    for(ll i=0;i<v.size();i++)
    {
        cin>>v[i];
    }
}
string decToBinary(ll n)
{
    // Size of an integer is assumed to be 32 bits
    string s="";
    for (int i = 31; i >= 0; i--) {
        int k = n >> i;
        if (k & 1)
            s+='1';
        else
            s+='0';
    }
    return s;
}
inline ll ceil(ll a , ll b){ if(a%b==0) return a/b ; else return a/b+1 ; } 
void fun(ll tt)
{
    ll n;
    cin>>n;
    ll l,r;
    l=1;
    r=n*n;
    vector<vector<ll>>v(n,vector<ll>(n,0));
    ll i=0,j=0;
    while(i<n)
    {
        v[i][j]=l;
        l++;
        i++;
        j++;
    }
    ll il1=2,jl1=0,il2=0,jl2=2,ir1=1,jr1=0,ir2=0,jr2=1;
    bool flip=false;
    while(true)
    {
        // cout<<l<<" "<<r<<endl;
        if(l>r)
        {
            // cout<<"Break\n";
            break;
        }
        
        if(!flip)
        {
            ll i=ir1,j=jr1;
            while(i<n && j<n)
            {
                v[i][j]=r;
                r--;
                i++;
                j++;
            }
            i=ir2,j=jr2;
            while(i<n && j<n)
            {
                v[i][j]=r;
                r--;
                i++;
                j++;
            }
            ir1+=2;
            jr2+=2;
        }
        else
        {
            ll i=il1,j=jl1;
            while(i<n && j<n)
            {
                v[i][j]=l;
                l++;
                i++;
                j++;
            }
            i=il2,j=jl2;
            while(i<n && j<n)
            {
                v[i][j]=l;
                l++;
                i++;
                j++;
            }
            il1+=2;
            jl2+=2;
        }
        flip=!flip;
    }
    for(auto x:v)
        printv(x);
    
}  

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    // t=1;
    for (ll i = 1; i <= t; i++)
    {
        fun(t);
    }
}


Comments

Submit
0 Comments
More Questions

1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple