1530B - Putting Plates - CodeForces Solution


constructive algorithms implementation *800

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<algorithm>
#include<unordered_map>
#include <vector>
#include<stack>
#include <math.h>
#include<bits/stdc++.h>
using namespace std;

//push_back
#define vec vector
#define read(n) ll n cin>>n
typedef long long ll;

const ll N=1000000007;
 
bool cmp(pair<ll,ll> a,pair<ll,ll> b){
	if(a.first != b.first) return a.first<b.first;
	return a.second > b.second;
}
bool cmp2(pair<ll,ll> a,pair<ll,ll> b){
	return a.second < b.second;
}
bool cmp3(ll a,ll b){
	return a>b;
}
bool cmp4(pair<ll,ll> a,pair<ll,ll> b){
	return a.first + a.second < b.first + b.second;
}
ll power(ll a,ll b){
	if(b==0) return 1;
	if(b==1) return a%N;
	ll res = power(a,b/2);
	if(b%2==0){
		ll ans = (res%N) * (res%N);
		return ans%N;
	}
	ll ans = (res%N)*(res%N);
	ans%=N;
	ans = (ans*(a%N))%N;
	return ans%N;
}

//Fib(2k) = Fib(k)*(2*Fib(k+1) - Fib(k))
//Fib(2k+1) = Fib(k+1)**2 + Fib(k)**2
//pair{Fib(2k) , Fib(2*k+1)}
pair<long,long> Fib(long n,vector<long> v){
    if(n==0) return {0,1};
    if(n==1) return {1,1};
    if(v[n] != 0) return {v[n],v[n-1]+v[n]};
    pair<long,long> a = Fib(n>>1,v);
    long x = a.first*(2*a.second - a.first);
    long y = a.first*a.first + a.second*a.second;
    if(n%2) return {y,y+x};
    else return {x,y};
}

vector<ll> sieve(ll n){
	vector<ll> v(n+1,1);
	v[0]=0;
	v[1]=0;
	for(ll i=2;i<=n;i++){
		if(v[i] == 1){
			for(ll j=2*i;j<=n;j+=i){
				v[j]=0;
			}
		}
	}
	return v;
}

//Number of llegers from 1 to i that are co prime to i.
vector<ll> euler_totient_1_to_n(ll n){
	vector<ll> v(n+1);
	for(ll i=0;i<=n;i++){
		v[i] = i;
	}
	for(ll i=2;i<=n;i++){
		if(v[i]==i){
			v[i] = i-1;
			for(ll j=i*2;j<=n;j+=i){
				v[j] -= v[j]/i;
			}
		}
	}
	return v;
}

ll phi(ll n){			//phi(n) = n*(1 - 1/p1)*(1 - 1/p2)....*(1 - 1/pk), p1,p2.. are prime factors of n
	ll cnt=n;
	for(ll i=2;i*i<=n;i++){
		if(n%i == 0){
			cnt -= cnt/i;
			while(n%i==0){
				n/=i;
			}
		}
	}
	if(n>=2) cnt -= cnt/n;
	return cnt;
}
bool prime(ll n){
	if(n==1) return false;
	for(ll i=2;i*i<=n;i++){
		if(n%i == 0) return false;
	}
	return true;
}

ll check(ll i,ll j,ll n,ll m){
	return i>=0 && i<n && j>=0 && j<m;
}

ll lcm(ll a,ll b){
	return min(a,b)*(max(a,b)/__gcd(a,b));
}

void bfs_al(vector<ll> v[], int start_node, int visited[]){
	queue<ll> q;
	q.push(start_node);
	while(!q.empty()){
		int node = q.front();
		q.pop();
		for(auto i : v[node]){
			if(visited[i] == 0) q.push(i);
		}
	}
}


ll modInverse(ll A,ll M){
	ll m0 = M;
	ll y=0,x=1;
	if(M==1){
		return 0;
	}

	while(A>1){
		ll q = A/M;
		ll t=M;

		M=A%M,A=t;
		t=y;

		y=x-q*y;
		x=t;
	}
	if(x<0) return x+=m0;

	return x;
}

ll find(vector<pair<ll,ll>> &v,ll x){
	ll l=0,h=v.size()-1;
	while(h>=l){
		ll mid = (l+h)/2;
		ll first = v[mid].first;
		if(first <= x) l = mid+1;
		else{
			if(mid==0 || x >= v[mid-1].first) return mid;
			else h = mid-1;
		}
	}
	return -1;
}

ll bs(vector<ll>& nums, ll target) {
    ll n = nums.size();
    ll l=0,h=n-1;
    ll a=nums[0];
    while(h>=l){
        ll mid = l + (h-l)/2;
        if(nums[mid]==target) return mid;
        else{
            if(nums[mid]>=a){
                if(nums[mid] > target){
                    if(target < a) l = mid+1;
                    else h = mid-1;
                }
                else l = mid+1;
            }
            else{
                if(nums[mid] < target){
                    if(target < a) l = mid+1;
                    else h = mid-1;
                }
                else h = mid-1;
            }
        }
    }
    return -1;
}


void print(char a[],ll l,ll h){
	for(ll i=l;i<h;i++){
		cout<<a[i];
	}
	cout<<" ";
}

void prints(set<ll> s){
	for(auto i : s){
		cout<<i<<" ";
	}
	cout<<"\n";
}

bool palindrome(string a){
	for(int i=0;2*i<a.size();i++){
		if(a[i] != a[a.size() - i - 1]) return false;
	}
	return true;
}


void dfs(vector<int> v[], int node,int visited[]){
	for(auto i : v[node]){
		if(visited[i] == 0){
			visited[i] = 1;
			dfs(v, i, visited);
		}
	}
}

void bfs(vector<int> v[],int start_node, int visited[]){
	queue<int> q;
	q.push(start_node);
	visited[start_node] = 1;
	while(!q.empty()){
		int front = q.front();
		q.pop();
		for(auto i : v[front]){
			if(visited[front] == 0){
				q.push(i);
			}
		}
	}
}


bool checkin(int i, int j, int n, int m){
	return (i>=1 && i<=n) && (j>=1 && j<=m);
}

void solve(){
	int n,m;
	cin>>n>>m;

	int arr[n][m] = {};
	for(int i=0;i<m;i+=2){
		arr[0][i] = 1;
	}
	for(int i=2; i<n;i+=2){
		arr[i][m-1] = 1;
	}
	for(int i=m-3; i>=0;i-=2){
		arr[n-1][i] = 1;
	}
	for(int i=n-3;  i>=0; i-=2){
		if(i != 1)
			arr[i][0] = 1;
	}

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout<<arr[i][j];
		}
		cout<<"\n";
	}
	cout<<"\n";
}


int main(){
	ios_base::sync_with_stdio(false);	//Prevents synchronisation of c++ stream from c stream.
	cin.tie(NULL);		//Method that flush std::cout before std::cin.
	cout.tie(NULL);		//Method that flush std::cin before std::cout.
	
	ll tc=1;
	cin>>tc;
	while(tc--){
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring