1408F - Two Different - CodeForces Solution


constructive algorithms divide and conquer *2300

Please click on ads to support us..

Python Code:

def pairss(n):
	res = [];i = 1
	while i < n:
		for k in range(0,n,i*2):
			for j in range(i):res.append((j+k,j+i+k))
		i *= 2
	return res
n = int(input());z = 1
while z * 2 < n:z *= 2
q = pairss(z);res = []
for a, b in q:res.append((a+1, b+1))
for a, b in q:res.append((n-a, n-b))
print(len(res))
for a, b in res:print(a,b)

C++ Code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef pair<int,int>pii;
const int maxn=1e5+10;
vector<pii>ans;
int n,ind,K;
inline void match(int l,int r)
{
    if(l==r)return;int mid=(l+r)>>1;
    match(l,mid),match(mid+1,r);
	for(int i=l;i<=mid;i++) ans.pb({i,i-l+mid+1});
}
inline void solve()
{
    cin>>n,ind=log2(n),K=(1<<ind);
    match(1,K),match(n-K+1,n);
    cout<<ans.size()<<'\n';
    for(auto [l,r]:ans) cout<<l<<" "<<r<<'\n';
}
signed main(){ios::sync_with_stdio(false);cin.tie(NULL);return solve(),0;}


Comments

Submit
0 Comments
More Questions

805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks
1389B - Array Walk
71B - Progress Bar
701A - Cards
545A - Toy Cars
1538E - Funny Substrings
234A - Lefthanders and Righthanders