1747B - BAN BAN - CodeForces Solution


constructive algorithms

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n=int(input())
    if n==1:
        print(1)
        print(1,2)
    else:
        a, b = 1, 3 * n
        n=(n+1)//2

        print(n)
        for i in range(n):
            print(a,b)
            a+=3
            b-=3

'''
BAN BAN BAN BAN
BNNN
'''



'''
123456789101112
BANBANBANBAN
'''

C++ Code:



#include <bits/stdc++.h>

using namespace std;

/* clang-format off */

//! TYPES
#define ll long long
// pair
#define pii pair<int, int>
#define pll pair<long long, long long>
//vector
typedef vector<int>::iterator vit;
#define vi vector<int>

#define vll vector<long long>
typedef vector<char> vc;
typedef vector<string> vs;
// map
#define mii map<int, int>
//set
typedef set<int>::iterator sit;
#define si set<int>
#define sc set<char>


/* FUNCTIONS */
#define f(i, s, e) for(long long int i=s;i<e;i++)
#define cf(i, s, e) for(long long int i=s;i<=e;i++)
#define rf(i, e, s) for(long long int i=e-1;i>=s;i--)
#define pb push_back
#define eb emplace_back
#define ff first
#define s second
#define all(n) (n).begin(), (n).end()


//! PRINTS
#define sp <<" "<<
#define ss <<
#define e << endl
#define endl '\n'

//print vector
template<typename T>
void print_v(vector<T> &v) {
    cout << "{";
    for (auto x: v) cout << x << ",";
    cout << "\b}";
};

//print set
template<typename T>
void print_s(set<T> &v) {
    cout << "{";
    for (auto x: v) cout << x << ",";
    cout << "\b}";
};

//print array
template<typename T>
void print(const T array[], size_t SIZE) { for (size_t i = 0; i < SIZE; i++) std::cout << array[i] << " "; }

#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define read(type) readInt<type>()

ll min(ll a, int b) {
    if (a < b) return a;
    return b;
}

ll min(int a, ll b) {
    if (a < b) return a;
    return b;
}

ll max(ll a, int b) {
    if (a > b) return a;
    return b;
}

ll max(int a, ll b) {
    if (a > b) return a;
    return b;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll lcmm(ll a, ll b) { return a / gcd(a, b) * b; }

string to_upper(string a) {
    for (int i = 0; i < (int) a.size(); ++i) if (a[i] >= 'a' && a[i] <= 'z') a[i] -= 'a' - 'A';
    return a;
}

string to_lower(string a) {
    for (int i = 0; i < (int) a.size(); ++i) if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 'a' - 'A';
    return a;
}

bool prime(ll a) {
    if (a == 1) return 0;
    for (int i = 2; i <= round(sqrt(a)); ++i) if (a % i == 0) return 0;
    return 1;
}

void yes() { cout << "YES\n"; }

void no() { cout << "NO\n"; }

/*  All Required define Pre-Processors and typedef Constants */
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

// ! USACO FILE
void u_file(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

/* clang-format on */

void gen(ll max, vll &arr) {
    ll start = 1;
    while (start <= 1e9) {
        arr.pb(start * start);
        start++;
    }
}

/* Main()  function */
int main() {
    // u_file("");
    IOS;

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        if (n == 1) {
            cout << n e;
            cout << 1 sp 2 e;
            continue;
        }
        if (n % 2 == 0) {
            ll end = n * 3;
            ll start = 1;
            n /= 2;
            cout << n e;
            f(i, 0, n) {
                cout << start sp end e;
                start += 3;
                end -= 3;
            }
        } else {
            ll end = n * 3;
            ll start = 1;
            n /= 2;
            n++;
            cout << n e;
            f(i, 0, n) {
                cout << start sp end e;
                start += 3;
                end -= 3;
            }
        }


    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal