1838C - No Prime Differences - CodeForces Solution


constructive algorithms math number theory

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define int long long
#define endl "\n"
#define ptr(a, n)               \
    for (int i = 0; i < n; i++) \
    {                           \
        cout << a[i] << ' ';    \
    }
#define fort(z, x, t) for (ll z = x; z < t; z++)
#define rev(z, x, t) for (ll z = t; z > x; z--)
#define arin(a, n)             \
    ll a[n];                   \
    for (ll i = 0; i < n; i++) \
    {                          \
        cin >> a[i];           \
    }
#define pb push_back
#define vein(v, n)             \
    vector<ll> v;              \
    for (ll i = 0; i < n; i++) \
    {                          \
        ll x;                  \
        cin >> x;              \
        v.pb(x);               \
    }
#define seint(s, n)            \
    set<int> s;                \
    for (ll i = 0; i < n; i++) \
    {                          \
        ll x;                  \
        cin >> x;              \
        s.insert(x);           \
    }
#define vint(v) vector<ll> v;
#define vstring(vs, n)         \
    vector<string> vs;         \
    for (ll i = 0; i < n; i++) \
    {                          \
        string s;              \
        cin >> s;              \
        vs.pb(s);              \
    }
#define num(n) \
    ll n;      \
    cin >> n;
#define out(n) cout << n << endl;
#define sortv(v) sort(v.begin(), v.end());
#define mpll map<ll, ll> mpi;
#define mpchar map<char, ll> mpc;
#define cin(a, n, b) \
    f(i, n)          \
    {                \
        cin >> a[i]; \
        b[i] = a[i]; \
    }
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 1e5 + 10;
int mod = 1e9 + 7;

void solve()
{
    int n,m;
    cin>>n>>m;
    int grid[n+1][m+1];
    int count=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            grid[i][j]=count;
            count++;
        }
    }
    int k=1;
    int f1=1;
    int l=((n+1)/2)+1;
    int f2=0;
    if(n==4){
        l=3;
        k=1;
        f2=1;
        f1=0;
    }
    while(n--){
        if(f1){
            for(int i=1;i<=m;i++){
                cout<<grid[k][i]<<" ";
            }
            cout<<endl;
            k++;
            f1=0;
            f2=1;
        }
        else{
             for(int i=1;i<=m;i++){
                cout<<grid[l][i]<<" ";
            }
            cout<<endl;
            l++;
            f2=0;
            f1=1;
            
        }
    }
    cout<<endl;
}
signed main()
{
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;
    cin >> t;
    fort(i, 0, t)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST