洗牌算法-神级算法来源

中文版本见https://github.com/lmdyyh/The-Art-Of-Programming-By-July/tree/master/ebook/zh,第35章

##### 2.3.5、How comes the Magic Conclusion?

Our problem has been resolved, but this chapter has not been completed,how comes the Magic Conclusion that determines the perfect shuffle algorithm: if 2 * n = (3 ^ k - 1), we have exactly k cycles with 1, 3, 32 , . . . , 3k−1 each belonging to a different cycle, and each cycle leader's starting positions are 1,3,9, ... 3 ^ (k-1).

![](../images/35/35.3.jpg)

The key to prove this conclusion is this: the merged cycle must contain all integers from 1 to M,and not one less. This proved is a bit of trouble, because the proof will involve number theory, such as group theory, but then a long way to go step by step can be reached.

First, let us clear the following related concepts, theorems, and definitions (figuring out these things, we will prove that a half):

>

Concept 1 mod expresses the remainder resulting from dividing two specified, such as 3 mod 5 = 3,5 mod 3 = 2;

>

Definition 1 Euler function φ (m) represents the number of positive integers that no more than m (ie, less than or equal m) and relatively-prime with m.

>

Definition 2 If φ (m) = Ordm (a),then a is called primitive root of m, which Ordm (a) is defined as: a ^ d (mod m), where d = 0,1,2,3 ..., but take the minimum d that make the equation established.

A combination of the above definition 1, and definition 2 shows 2 is a primitive root of 3. For 2^0 mod 3 = 1, 2^1 mod 3 = 2, 2^2 mod 3 = 1, 2^3 mod 3 = 2,{a^0 mod m,a^1 mod m,a^2} which obtains the set S = {1,2},

contains all numbers relatively-prime with 3, i.e. d = φ (2) = 2, meets the definition of primitive roots.

Yet 2 is not a primitive root of 7,for 2^0 mod 7 = 1, 2^1 mod 7 = 2, 2^2 mod 7 = 4, 2^3 mod 7 = 1,2^4 mod 7 = 2,2^5 mod 7 = 4,2^6 mod 7 = 1,where the set S={1,2,4} always contains 1、2、4, which does not include all of the relatively-prime number with 7(it does not include 3,6,5), i.e., d = 3, but φ (7) = 6, so that d! = φ (7), does not meet the definition of primitive roots.

Furthermore, if a is a primitive root of another number m, which represents the set S = {a ^ 0 mod m, a ^ 1 mod m, a ^ 2 mod m ......} contains all numbers that is less than m and relatively-prime with m, otherwise it is not a primitive root of m. And the set S = {a ^ 0 mod m, a ^ 1 mod m, a ^ 2 mod m ......} may exist repeated remainder, but when a relatively-prime with m, I guarantee in the set {a ^ 0 mod m, a ^ 1 mod m, a ^ 2 mod m}, the first number is a ^ 0 mod m, When the first time that the number is repeated, this number must be 1, that is, it must start from the leader when it comes to remainder cycle.

For modular exponentiation, the primitive root of a modulo m is defined as the smallest positive integer d in ![](../images/35/35.4.jpg),st:![](../images/35/35.5.jpg).

As another example,2 is a primitive root of 9,for ![](../images/35/35.6.jpg),to make ![](../images/35/35.7.jpg)

divided by 9 equals a constant of 1,so the smallest positive integer d = 6, and φ (m) = 6, meet the definition of primitive root.

>

Theorem 1 congruence theorem: two integers a, b, if they are divided by a positive integer m getting the same remainder, that called a, b ​​congruence modulus m, denoted by ![](../images/35/35.8.jpg).

>

Theorem 2 when p is an odd prime and a is a primitive root of ![](../images/35/35.9.jpg),then a is a primitive root of ![](../images/35/35.10.jpg) for any k>=1.

>

Theorem 3 Fermat's Little Theorem: If a and m are relatively-prime, then a ^ φ (m) mod m = 1

>

Theorem 4 If (a, m) = 1 and a is a primitive root of m, then a is the generator of (Z / mZ) * .

Take a = 2, m = 3.

We know that 2 is a primitive root of 3, 2 is a primitive root of 9, we define S (k) represents the above set S, and taking the x = 3 ^ k (x represents the number of the set S).

So:

S(1) = {1, 2}

S(2) = {1, 2, 4, 8, 7, 5}

We do not change the order of the elements in the cycle, the cycle containing 1 consists of all numbers relatively prime to 3^k and less than it.

How about the number that not relatively prime to 3^k,If 0 <i <3 ^ k and 3 ^ k are not relatively prime to i,so the greatest common divisor of i and 3 ^ k must be the form of 3^t(contains only divisor 3),and t <k. I.e. gcd (i, 3 ^ k) = 3 ^ t, divided by 3 ^ t both sides of the equation , i.e. gcd (i / (3 ^ t), 3 ^ (k - t)) = 1, i / (3 ^ t) are relatively prime to 3 ^ (k - t), and i / (3 ^ t) <3 ^ (k - t), according to the definition of S (k), we can see i / (3 ^ t) in the set S (k - t).

Similarly,any number x in S (k - t) satisfies gcd (x, 3 ^ k) = 1, so gcd (3 ^ k, x * 3 ^ t) = 3 ^ t, and x * 3 ^ t <3 ^ k. x*3^t which in the set S (k - t) and I form one-to-one correspondence relationship.

That is, the new set generated by each number of the form x* 3^t in S (k - t) contains all the numbers that the greatest common divisor with 3^k is 3^t, and it is also a cycle, the leader of the original cycle and this cycle are 1 and 3^t respectively.

So, for all number less than 3^k, according to the greatest common divisor with 3^k, we can assign it to a cycle, and the K cycles contain exactly all the number less than 3^k.

For example, as caopengcs said, when we take "a = 2, m = 3,

We know that 2 is a primitive root of 3, 2 ​​is a primitive root of 9,we define S (k) represents set S, and x = 3 ^ k.

So S(1) = {1, 2}

S(2) = {1, 2, 4, 8, 7, 5}

For instance k = 3. We have:

S(3) = {1, 2 ,4 , 8, 16, 5, 10, 20, 13, 26, 25, 23, 19, 11, 22, 17, 7, 14} contains all numbers less than 27 and relatively prime to 27 , the leader of the cycle is 1, which is decided by the difinition of primitive root.

Then the number which the greatest common divisor with 27 is 3, is obtained by letting S (2) multiply by 3. S (2) * 3 = {3, 6, 12, 24, 21, 15},the leader is 3 without changing the order.

The number which the greatest common divisor with 27 is 9, is obtained by letting S (2) multiply by 9. S(1) * 9 = {9, 18},the leader is 9 without changing the order.

For each number less than 27,the greatest common divisor with 27 is just 1,3,or 9 and with one-to-one relationship.Therefore, S (2) * 3 contains all the number less than 27 and the greatest common divisor with 27 is 3, S (1) * 9 contains all the number less than 27 and the greatest common divisor with 27 is 9. "

In other words, if defined as an integer, assuming / N is defined as the set containing all the remainders generated by Z divided by N, including {0 ... N-1}, and (/ N) * which is defined as the set containing the number in {0 ... N-1} which relatively prime to N.

when n = 13, 2n +1 = 27, i.e.,/ N = {0,1,2,3, ....., 26}, (/ N) * is equivalent to the set containing the number which relatively prime to {0,1,2 , 3, ....., 26}.

2 ^ k (mod 27) is included in (/ 27) *, so these numbers are available in the following three cycles:

take 1 as the leader, (/27)*={1,2,4,8,16,5,10,20,13,26,25,23,19,11,22,17,7,14},that is to say,the set of positive integers that less than and relatively prime to 27 is {1,2,4,8,16,5,10,20,13,26,25,23,19,11,22,17,7,14},so ϕ(m) = ϕ(27)=18, the d meeting the requirement of ![](../images/35/35.11.jpg) is 18, so 2 is the primitive root of 27.

Take 3 as the leader,you can get{3,6,12,24,21,15},this is the cycle with the leader 3. The cycle is all the number which is a multipe of 3, and not a multiple of 9. Why? Because 2^k and 27 are relatively prime.

For more details :If the remainder of 3 × 2 ^ k divided by 27 can be divisible by 9, there is an n meeting 3 * 2 ^ k = 9n (mod 27), i.e., 3 * 2 ^ k-9n can be divisible by 27, so 3 * 2 ^ k-9n = 27m, where n, m is an integer, so that, divided by 3 on both side, we can obtain 2 ^ k = 9m +3 n, ie, 2 ^ k is a multiple of 3, which is contradictory wirh the case 2 ^ k relatively prime to 27, so the remainder of 3 × 2 ^ k divided by 27 can not be divisible by 9.

In addition,the remainder of 2 ^ k divided by 27 can be any number other than a multiple of 3, so, the remainder of 2 ^ k divided by 27 may be 1,2,4,5,7,8, when the remainder is 1,there exists a k such that 2 ^ k-1 = 27m, m is an integer.

Obtained by multiplying both sides of equation 3: 3 * 2 ^ k-3 = 81m is a multiple of 27, so the remainder of 3 * 2 ^ k divided by 27 is 3;

Similarly, when the remainder is 2, 2 ^ k - 2 = 27m, => 3 * 2 ^ k-6 = 81m, so the remainder of 3 * 2 ^ k divided by 27 is 6;

When the remainder is 4, 2 ^ k - 4 = 37m, => 3 * 2 ^ k - 12 = 81m, so the remainder of 3 * 2 ^ k divided by 27 is 12;

Similarly, you can get to 15,21,24. Thus also confirms the above conclusion: Take 3 as the leader, you can get {3,6,12,24,21,15}.Take 9 as the leader, which is very simple, this cycle is {9,18}.

You will find that,all the natural numbers less than 27, ethier inside in the first cycle relatively prime to 27 or inside in the second cycle that are multiples of 3, but not a multiple of 9; or inside the third cycle, which is a multiple of 9.What make it sense is 2 is a primitive root of 27. This completes the proof.

Finally, we also re-verify the above process:

For ![](../images/35/35.12.jpg),so:

i = 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

Since n = 13,2 n +1 = 27,according to ![](../images/35/35.13.jpg),the number in position i will come to the following location:

i = 2 4 6 8 10 12 14 16 18 20 22 24 26 1 3 5 7 9 11 13 15 17 19 21 23 25 0

According to changes in i and i ' before and after transformation , we will get three cycles:

1->2->4->8->16->5->10->20->13->26->25->23->19->11->22->17->7->14->1;

3->6->12->24->21->15->3

9->18->9

yes, the figures in this three cycles are in line with those got before.Verification is complete.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值