Skip to content

Commit 830fa23

Browse files
committed
merging
1 parent ee86d27 commit 830fa23

File tree

8 files changed

+291
-46
lines changed

8 files changed

+291
-46
lines changed

python/Xor/.idea/Xor.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/Xor/.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/Xor/.idea/workspace.xml

Lines changed: 178 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/Xor/raw/Xor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ https://www.geeksforgeeks.org/given-a-set-find-xor-of-the-xors-of-all-subsets/
22
https://www.geeksforgeeks.org/sum-xor-possible-subsets/
33
https://www.geeksforgeeks.org/maximum-xor-using-k-numbers-1-n/
44
https://www.geeksforgeeks.org/calculate-xor-1-n/
5-
http://codeforces.com/blog/entry/10229
5+
http://codeforces.com/blog/entry/10229
6+
7+
Teorias
8+
https://accu.org/index.php/journals/1915
9+
https://en.wikipedia.org/wiki/Symmetric_difference
10+
11+
https://github.com/hypersolid/competitive-programming/blob/master/hackerrank/algorithms/dp/Xor-And-Sum.java
12+
https://github.com/havelessbemore/hackerrank/blob/master/algorithms/dynammic_programming/xor-and-sum.java

python/Xor/raw/problems

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
All About XOR
2+
https://accu.org/index.php/journals/1915
3+
4+
5+
http://mathworld.wolfram.com/XOR.html
6+
https://www.physicsforums.com/threads/xor-in-set-theory.743760/
7+
8+
Equal Sum and XOR
9+
https://www.geeksforgeeks.org/equal-sum-xor/
10+
11+
12+
Find two numbers from their sum and XOR
13+
https://www.geeksforgeeks.org/find-two-numbers-sum-xor/
14+
15+
16+
https://math.stackexchange.com/questions/2572213/sum-of-xor-matrix
17+
18+
https://stackoverflow.com/questions/36477623/given-an-xor-and-sum-of-two-numbers-how-to-find-the-number-of-pairs-that-satisf
19+
20+
21+
https://www.hackerrank.com/challenges/maximizing-xor/problem
22+
https://www.hackerrank.com/contests/hourrank-13/challenges/sum-vs-xor/problem
23+
https://www.codechef.com/ACM16CHN/problems/CHN16H
24+
25+
26+
27+
Theory - cyclic shifts bitwise
28+
https://www.codechef.com/SNCKPB16/problems/MINSHIFT
29+
https://www.geeksforgeeks.org/rotate-bits-of-an-integer/
30+
https://cboard.cprogramming.com/c-programming/145685-cycle-shift-bitwise-operators-help-pls.html
31+
32+
33+
Symmetric difference
34+
https://en.wikipedia.org/wiki/Symmetric_difference
35+
https://math.stackexchange.com/questions/84184/relation-between-xor-and-symmetric-difference

python/Xor/solutions/Xor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from math import log10, floor
2+
3+
14
def isPowerOf2(n):
25
return (n & (n - 1)) == 0
36

@@ -11,6 +14,7 @@ def sum_n_xor(n):
1114
acc = acc ^ x
1215
return acc
1316

17+
1418
def sum_n_xor2(n):
1519
o = n & 3
1620
if o == 0:
@@ -23,8 +27,17 @@ def sum_n_xor2(n):
2327
return 0
2428

2529

30+
def count_bits_base2(n):
31+
return floor(log10(n) / log10(2)) + 1
32+
33+
34+
print(count_bits_base2(10))
35+
print(count_bits_base2(1 << 50))
36+
print(count_bits_base2(15))
37+
'''
2638
print([(x, sum_n_xor(x)) for x in range(4, 25)])
2739
print([(x, sum_n_xor2(x)) for x in range(4, 25)])
40+
'''
2841

2942
if __name__ == '__main__':
3043
pass

python/Xor/solutions/XorAndSum.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'''
22
https://www.hackerrank.com/challenges/xor-and-sum/problem
3+
https://en.wikipedia.org/wiki/Binary_number#Binary_counting
34
'''
45

56

6-
def toDec(str_bin):
7+
def to_dec(str_bin):
78
a, e = 0, 0
89
for i in range(len(str_bin) - 1, -1, -1):
910
a += (1 if str_bin[i] == '1' else 0) << e
@@ -26,11 +27,56 @@ def naive_test(a, b):
2627
def solver():
2728
bin_a = input()
2829
bin_b = input()
29-
a, b = toDec(bin_a), toDec(bin_b)
30+
a, b = to_dec(bin_a), to_dec(bin_b)
3031
return naive_test(a, b)
3132

3233

33-
print(solver())
34+
_LIMIT_SUM = 314159
35+
_M = 1000000007
36+
37+
38+
def solver2():
39+
bin_a = input()
40+
bin_b = input()
41+
_sz_a = len(bin_a)
42+
_sz_b = len(bin_b)
43+
buffer_binary_a = [0] * _sz_a
44+
acc_buffer_binary_b = [0] * _sz_b
45+
acc_buffer_inv_binary_b = [0] * _sz_b
46+
acc = 0
47+
power_of_2 = 1
48+
for x in range(_sz_a - 1, -1, -1):
49+
buffer_binary_a[_sz_a - 1 - x] = 1 if bin_a[x] == '1' else 0
50+
51+
for x in range(_sz_b - 1, -1, -1):
52+
idx = _sz_b - 1 - x
53+
acc_buffer_binary_b[idx] = 1 if bin_b[x] == '1' else 0
54+
if x < _sz_b - 1:
55+
acc_buffer_binary_b[idx] += acc_buffer_binary_b[idx - 1]
56+
57+
for x in range(0, _sz_b):
58+
acc_buffer_inv_binary_b[x] = 1 if bin_b[x] == '1' else 0
59+
if x > 0:
60+
acc_buffer_inv_binary_b[x] += acc_buffer_inv_binary_b[x - 1]
61+
62+
for x in range(0, _LIMIT_SUM):
63+
xth_bit_a = buffer_binary_a[x] if x < _sz_a else 0
64+
xth_bit_b = acc_buffer_binary_b[x] if x < _sz_b else acc_buffer_binary_b[_sz_b - 1]
65+
if xth_bit_a == 1:
66+
acc += (((_LIMIT_SUM+1-xth_bit_b)%_M)*(power_of_2%_M))%_M
67+
else:
68+
acc += ((xth_bit_b%_M)*(_M%power_of_2))%_M
69+
power_of_2 = (power_of_2%_M)*(2%_M)%_M
70+
71+
for x in range(0, _sz_b):
72+
acc += (power_of_2*acc_buffer_inv_binary_b[_sz_b-1-x])%_M
73+
acc %= _M
74+
power_of_2 = (power_of_2%_M)*(2%_M)%_M
75+
76+
return acc
77+
78+
79+
solver2()
3480

3581
if __name__ == '__main__':
3682
pass

python/problems.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
https://www.hackerrank.com/challenges/dynamic-programming-classics-the-longest-common-subsequence/problem
2+
https://www.hackerrank.com/challenges/unbounded-knapsack/problem
3+
https://www.hackerrank.com/challenges/xor-and-sum/problem
4+
https://www.hackerrank.com/challenges/longest-increasing-subsequent/problem
5+
https://www.hackerrank.com/challenges/summing-pieces/problem
6+
https://www.hackerrank.com/challenges/sum-vs-xor/problem

0 commit comments

Comments
 (0)