-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter_gen.py
55 lines (50 loc) · 1.14 KB
/
iter_gen.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
47
48
49
50
51
52
53
# -*- coding:utf-8 -*-
class Gospers_Hack:
def __init__(self, m, n ):
# n: 总比特数
self.m = m # 有效比特数
self.first = pow(2, m ) - 1
self.last = pow(2, n ) - pow(2, n-m )
def __iter__(self):
self.x = self.first
return self
def next(self):
xr = self.x
if (xr > self.last ):
raise StopIteration
x = self.x
s = x & (-x) # 找到最右边1的位置
r = s + x # 找到最右边连续1(k个)的终点位置+1,并将此位置置1
x = r | (((x ^ r) >> 2) // s) # 将(k-1)个连续1放到尾部
self.x = x
return xr
def combi(n,k):
if n < k:
return 0
else :
denominator = 1
numerator = 1
for i in range(1,k+1):
denominator *= i
numerator *= (n-i+1)
return numerator/denominator
def Cindex(x,k,m):
C = [0 for i in range(k+1)]
count = 1
for i in range(m):
if (x >> i) & 1 :
C[count] = i
count += 1
index = 0
for i in range(1,k+1):
index += combi(C[i],i)
return index
# # test
# gh = Gospers_Hack(2-1,24)
# print 'first:',gh.first
# print 'last:',gh.last
# count = 0
# for S in Gospers_Hack(2-1,24):
# print count,":",S,
# print "Cindex:",Cindex(S,1,24)
# count += 1