forked from mostafa-saad/ArabicCompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
41 C++ Programming 4 Competitions - Bits.cpp
690 lines (319 loc) · 10 KB
/
41 C++ Programming 4 Competitions - Bits.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
*
*
******************************************** Licence *******************************************
* *
* This File is part of Algorithms Arabic Video Series *
* Made By Eng Mostafa Saad, Teaching Assistant in FCI - Cairo University *
* *
* Available at My YouTube Channel: http://www.youtube.com/user/nobody123497?feature=mhee *
* *
* Feel free to make use of it at anytime, for any reason, in anyway, without any obligations. *
* *
* *
* In case of finding a mistake, kindly notify me at: mostafa.saad.fci@gmail.com *
* *
* Mostafa Saad Ibrahim © 2013 *
* *
************************************************************************************************
*
*
*/
/*
Content:
Bits Usage
Printing Number
Count Num Bits1
Basic operations
Power of X-1
Count Num Bits2
Print All Subsets of Length
Gray Code
Common Mistakes
Print All Subsets of a GIVEN mask
Bits is Time Efficient
Bits is Memory Efficient
Bits in STL
Problems: SRM 539 Div1-1, SRM 543 Div1-1, SRM 555 Div1-1
Links
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=bitManipulation
http://en.wikipedia.org/wiki/Gray_code
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
*/
// PLEASE Never to copy my code Blocks. I will banned in TopCoder if you used my code blocks!
// Sometimes, it is low-level optimization, so better time order
// Sometimes, it drop memory by factor of 8! so better memory order
// Sometimes, it make code shorter
/*
A B !A A&B A|B A^B
0 0 1 0 0 0
0 1 1 0 1 1
1 0 0 0 1 1
1 1 0 1 1 0
X = 1152 = 10010000000
Y = 1428 = 10110010100
X & Y = 10010000000
X | Y = 10110010100
X ^ Y = 00100010100
A ^ B ^ C ^ D = E For any bit in E with 1, must be ODD number of 1s
X ^ 0 = X X ^ X = 0
(A ^ B ^ C ^ D ^ E) ^ (A ^ B ^ C) = D ^ E
X >> 1 = 1001000000 Equals X/2
X >> 2 = 100100000 Equals X/4
X << 1 = 100100000000 Equals X*2
X << 2 = 1001000000000 Equals X*4
X << 1 = 010010000000 Equals X*2
X = 11010 = 26 = 2^1 + 2^3 + 2^4 = 2 + 8 + 16
X = 11010 = 26 // Last Bit shows Parity: Even / Odd
X = 11011 = 27
X % 2 = X & 1 = bit 0
Generally: X%(2^n) = X & (2^n -1)
Even test
if(n%2 == 1) -> Fails of n is negative
if( (n&1) == 1) -> Works always
*/
void printNumber(int n, int len)
{
if(!len)
return ;
printNumber(n>>1, len-1); // remove last bit
cout<<(n&1); // print last bit
}
// Let's count bits
int countNumBits1(int mask) { // O(bits Length)
int ret = 0; //1101001 -> 110100 -> 11010 -> 1101 -> 110 -> 11 -> 1 -> 0
while (mask) {
if(mask&1)
ret++;
mask >>= 1;
}
return ret;
}
// Get bit
int getBit(int num, int idx) {
return ((num >> idx) & 1) == 1; // 110100, idx = 4 --> 110 & 1 = 0
}
int setBit1(int num, int idx) {
return num | (1<<idx);
}
int setBit0(int num, int idx) {
return num & ~(1<<idx); // 110100, idx = 3 --> 110100 & ~000100 = 110100 & 111011
}
int flipBit(int num, int idx) {
return num ^ (1<<idx);
}
// Exercise: rotate a bit to right. 0000000000000000000011001101110 -> rotate 5 -> 0111000000000000000000000110011
/*
X-1 is very important!
X = 840 = 01101001000
X-1 = 839 = 01101000111 What happened? First bit 16=(2^4) is removed, and 15=2^4-1 is added.
X & (X-1) = 01101000000 first bit from right removed
X & ~(X-1) = 01101001000 & 10010111000 = 00000001000 value of 1<<SmaintestBitIdx
*/
int countNumBits2(int mask) { // O(bits Count) __builtin_popcount
int ret = 0;
while (mask) {
mask &= (mask-1);
++ret; // Simply remove the last bit and so on
}
return ret;
}
// len = 3: 000, 001, 010, 011, 100, 101, 110, 111
void printAllSubsets(int len) // Remember we did it recursively! This is much SIMPLER!
{
for (int i = 0; i < (1<<len); ++i)
printNumber(i, len);
// For reversed order. Either reverse each item or work from big to small
//for (int i = (1<<len); i >= 0 ; --i)
// printNumber(i, len);
}
/*
Gray Code: is a binary numeral system where two successive values differ in only one bit
0000 0000 0
0001 0001 1
0010 0011 2
0011 0010 1
0100 0110 2
0101 0111 3
0110 0101 2
0111 0100 1
1000 1100 2
1001 1101 3
1010 1111 4
1011 1110 3
1100 1010 2
1101 1011 3
1110 1001 2
1111
Build it incrementally, Let's see for len = 1
0
1
Great. For len = 1 we have correct List. For length 2, we know we need to add prefix 0 and 1 so doubling list
00
01
10
11
Great, Len = 2 also works well.
For length 3:
000
001
010
011
100
101
110
111
Failed. Note. We know 1st 4 numbers are correct. As the bits count did not change. Same for 2nd 4 numbers. All incremented 1.
Problem when moved from 4th to 5th. The point, they will always be 0111111 and 100000.
What about reversing 2nd list, so we got the biggest 111111 one beside end of 1st block 011111.
So all what we need. Given answer of list N-1. To generate N. use 0N + 1N' where N' is reverse list.
If we built the list incrementally including from N=2 we got.
0000 0000 0
0001 0001 1
0010 0011 2
0011 0010 1
0100 0110 2
0101 0111 3
0110 0101 2
0111 0100 1
1000 1100 2
1001 1101 3
1010 1111 4
1011 1110 3
1100 1010 2
1101 1011 3
1110 1001 2
By observation, in gray code, every bit is Xor of its bit and next one.
*/
int grayCode(int i) {
return i ^ (i>>1);
}
void printAllSubsetsGray(int len)
{
for (int i = 0; i < (1<<len)-1; ++i)
{
printNumber(i, len );
cout<<"\t\t";
printNumber( grayCode(i), len );
cout<<"\t"<<__builtin_popcount(grayCode(i));
cout<<"\n";
}
}
// Be careful with operators precedence problems. http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
// X & 7 == 1 is interpreted as X & (7 == 1) --> (X & 7) == 1
// 1<<10 -1 is interpreted as = 1<<9 --> (1<<10) - 1
// Watch out from overflow
// 1 << 60 Fails. 1 is 32 bitInteger --> 1LL << 60
// What about print all subsets of a GIVEN MASK. Easily recursive, let's do it iterative
// E.g. subsets of 101: 101, 100, 001, 000 -> Each one is tried 0, 1
// Let's first assume, mask was complete and we work on it reverse
void PrintAllSubsetsAllOnes(int len)
{
int mask = (1<<len)-1; // mask = 1111 for len = 4;
for (int i = mask; i >= 0 ; i = i - 1)
printNumber(i, len);
}
// So we decrease -1 to get next. Do u remember effect of X-1?
/*
11111
11110
11101
11100
11011
11010
11001
11000
10111
10110
10101
10100
10011
10010
10001
10000
01111
01110
01101
01100
01011
01010
01001
01000
00111
00110
00101
00100
00011
00010
00001
00000
*/
// What about trying the given mask - 1 till zero! Wrong 1s are added! Simply remove them
// 110101000 -1 = 110100111 & 110101000 = 110100000
void getAllSubMasks(int mask) {
for(int subMask = mask ; subMask ; subMask = (subMask - 1) & mask)
printNumber(subMask, 32); // this code doesn't print 0
// For reverse: ~subMask&mask = subMask^mask
}
// Efficiency
void intersection(int A[60], int len1, int B[60], int len2)
{
int mask1 = 0;
int mask2 = 0;
for (int i = 0; i < len1; ++i)
mask1 = setBit1(mask1, A[i]);
for (int i = 0; i < len2; ++i)
mask2 = setBit1(mask2, B[i]);
int inter = mask1 & mask2; // In O(1) noes intersection
for (int i = 0; i < max(len1, len2); ++i) {
if(getBit(inter, i))
cout<<i<<" ";
}
cout<<"\n";
}
// In small graphs, E.g. nodes < 64, we could maintain graph node neighbors in a mask
// Better Memory
// Say you have numbers in range 100 Milion, and you want to know if some number visited before or NOT
// Simply: bool vis[100 * 1000 * 1000]; // and mark in it.
// But 100 Milion is too much! What about 100Milion / 8 = 12500000 12 Milion and half!
// You know that each character is 8 bits. Bool too takes 8 bits. UTILIZE byte!
const int MAX = 100000000;
char vis[MAX/8 + 1];
void setVisited(int i) { // in past vis[i] = 1
// Now each byte is responsible for 8 bits.
// So first we need to know which char is for us. Then my byte is in i / 8
// And then which bit with us. Any inside this byte i am is i % 8
vis[i/8] |= ( 1<<(i % 8) );
// Or more efficiently
vis[i>>3] |= ( 1<<(i&7) );
}
bool isVisited(int i) { // in past if(vis[i])
return vis[i>>3] & (1<<(i&7));
}
// Much staff? Let's use STL!
#include <bitset> // The power, speed and memory handling in one place
void STL() {
const int N = 20; // const
string s = "000111";
bitset<N> x(s); // 00000000000000000111
x.set(); // 11111111111111111111
x.flip(); // 00000000000000000000
x = 10; // 00000000000000001010
x |= 3; // 00000000000000001011
x = (x<<3); // 00000000000001011000
x = ~x; // 11111111111110100111
x.set(15, 0); // 11110111111110100111
x.set(15); // 11111111111110100111
x.flip(0); // 11111111111110100110
x.count(); // Returns the number of bits that are set.
x.any(); // Returns true if any bits are set.
x.none(); // Returns true if no bits are set.
x.test(15);
x.to_ulong(); // Returns an unsigned long represent mask
// The most interesting
if(x[2] == 0)
;
x[0] = 1; // Set bit from most right to 1
x[N-1] = 0; // Set bit from most left to 0
cout<<x<<"\n"; // display a string of N bits
}