1469D - Ceil Divisions - CodeForces Solution


brute force constructive algorithms math number theory *1700

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  endl            '\n'
#define  pb              push_back
#define  rep(i,a,n)      for(ll i =a;i<n;i++)
#define  min3(a,b,c)     min(a,min(b,c))
#define  min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define  max3(a,b,c)     max(a,max(b,c))
#define  max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define  mod             1000000007
#define  fastio()        ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define  ss              second
#define  ff              first
#define  gcd             __gcd
#define  all(x)          x.begin(),x.end()
#define  wttt            while(t--)
#define  printall(v)     for(auto value:v) cout<<value<<" ";cout<<endl;
#define  mp              make_pair
#define  PI              3.141592653589793238462643383279502884197
#define  ub              upper_bound
#define  lb              lower_bound
#define  read(v,n)       rep(i,0,n)cin>>v[i];
#define  count_set_bits  __builtin_popcountll



#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif


typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<ll,ll>pll;
typedef map<ll,ll> mll;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll,ll>> vpll;
typedef vector<ld> vld;
typedef vector<char> vch;
typedef vector<vector<char>> vvch;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > pbds;

ld eps = 1e-9;

/*
For using ordered_set : 

1. *s.find_by_order(k) --> Returns the element present at index k.

2.  s.order_of_key(k) -->  Returns the number of elements smaller than k.
*/





void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(ld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
// void _print(pbds v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T, class V> void _print(multimap <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}


















void solve()

{
    ll n;cin>>n;
    vll v(n);
    rep(i,0,n)
    {
        v[i] = i + 1;
    }
    
    vpll ans;
    for(ll i = n-2; i >= 2; i--)
    {
        ll val = v[n-1]/v[i];
        if(v[n-1] % v[i] != 0)
        {
            val++;
        }
        if(v[i] <= val)
        {
            ans.pb({n-1, i});
            v[n-1] = val;
        }
        ans.pb({i, n-1});
        v[i] = 1;
    }
    
    while(v[n-1] != 1)
    {
        ll val = v[n-1]/v[1];
        if(v[n-1] % v[1] != 0)
        {
            val++;
        }
        v[n-1] = val;
        ans.pb({n-1, 1});
    }
    
    cout<<ans.size()<<endl;
    for(auto val:ans)
    {
        cout<<val.ff + 1<<" "<<val.ss + 1<<endl;
    }
}










int main()
{
      #ifndef ONLINE_JUDGE
      freopen("Error.txt", "w", stderr);
      #endif

      fastio();
  
      ll t=1;
      //pre();

      cin>>t;
  
  
  
      for(ll ttt = 1;ttt<=t;ttt++)
      {
        
    
           // cout<<"Case #"<<ttt<<": ";
    
    
        solve();
    
    
      }    
  
  
  
      return 0;
}


Comments

Submit
0 Comments
More Questions

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
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval