-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWC222_02.py
46 lines (37 loc) · 1.13 KB
/
WC222_02.py
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
import math
import collections
class Solution:
def countPairs(self, deli):
targetSum = [pow(2, i) for i in range(0, 41)]
dd = collections.Counter(deli)
sortedKeys = sorted(dd.keys())
MOD = 10 ** 9 + 7
def summod(res, x):
res = res + x % MOD
res = res % MOD
return res
res = 0
for key in sortedKeys:
for tsum in targetSum:
if key * 2 > tsum:
continue
elif (key * 2 == tsum):
res = summod(res, dd[key] * (dd[key] - 1) // 2)
else:
if dd.get(tsum - key, 0):
res = summod(res, dd[key] * dd[tsum - key])
return res
import time
start = time.clock()
nums = [149,107,1,63,0,1,6867,1325,5611,2581,39,89,46,18,12,20,22,234]
ExpectOutput = 12
solu = Solution() # 先对类初始化,才能进行调用
# temp = solu.removeSubfolders(s)
temp = solu.countPairs(nums)
if (temp == ExpectOutput):
print('right')
else:
print('wrong')
print(temp)
elapsed = (time.clock() - start)
print("Time used:", elapsed)