forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2205 - Gray Code.cpp
46 lines (43 loc) · 910 Bytes
/
2205 - Gray Code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Problem Name: Gray Code
Problem Link: https://cses.fi/problemset/task/2205
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin>>n;
int a[1<<n + 1] = {0};
for (int i = 0; i < n; i++)
cout<<0;
cout<<endl;
a[0] = 1;
int x = 0;
for (int i = 1; i < 1<<n; i++) {
for (int j = 0; j < n; j++) {
int y = x^(1<<j);
if (!a[y]) {
x = y;
a[y] = 1;
string s;
while (y) {
if (y&1) s += '1';
else s += '0';
y >>= 1;
}
reverse(s.begin(), s.end());
for (int i = 0; i < n - s.size(); i++)
cout<<0;
cout<<s<<endl;
break;
}
}
}
}