File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed
Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // generates next integer w with popcount(w) == popcount(v)
6+ uint32_t next_perm (uint32_t v) {
7+ uint32_t t = (v | ( v - 1 )) + 1 ;
8+ uint32_t w = t | ((((t & -t) / (v & -v)) >> 1 ) - 1 );
9+ return w;
10+ }
11+
12+ int main () {
13+ int n;
14+ cin >> n;
15+
16+ // loop throught all the permutations with 1, 2, 3... bits set
17+ for (int i = 1 ; i < n; i++) {
18+ for (int x = (1 <<i)-1 ; x < (1 <<n); x = next_perm (x)) {
19+ cout << bitset<8 >(x) << " \n " ;
20+ }
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ using ll = long long ;
6+
7+ [[ gnu::target(" abm,bmi,bmi2" ) ]]
8+ inline int ilog2 (ll x) {
9+ return x==0 ? 0 : 63 - __builtin_clzll (x);
10+ }
11+
12+ inline ll isqrt (ll x) {
13+ ll res = sqrtl (x);
14+ while (res*res < x) res++;
15+ return res;
16+ }
17+
18+ inline ll mul (ll a, ll b, ll m) {
19+ return __int128 (a)*b%m;
20+
21+ // ll res = 0;
22+ // while (b) {
23+ // if (b&1) b = (a+b)%m;
24+ // b = (b+b)%2;
25+ // b >>= 1;
26+ // }
27+ // return res;
28+ }
29+
30+ ll phi (ll x) {
31+ ll res = x;
32+
33+ while (x % 2 == 0 ) {
34+ res -= res/2 ;
35+ x /= 2 ;
36+ }
37+
38+ for (ll i = 3 ; i*i <= x; i += 2 ) {
39+ while (x % i == 0 ) {
40+ res -= res/i;
41+ x /= i;
42+ }
43+ }
44+
45+ return res;
46+ }
47+
48+ ll power (ll a, ll b, ll m) {
49+ ll res = 1 ;
50+ while (b) {
51+ if (b&1 ) res = mul (res, a, m);
52+ a = mul (a, a, m);
53+ b >>= 1 ;
54+ }
55+ return res;
56+ }
57+
You can’t perform that action at this time.
0 commit comments