c mod m = (a ⋅ b) mod m
c mod m = [(a mod m) ⋅ (b mod m)] mod m
c^1230 mod m = (c ^ 123 % m)^10 % m
A few distributive properties of modulo are as follows:
1. ( a + b ) % c = ( ( a % c ) + ( b % c ) ) % c
2. ( a * b ) % c = ( ( a % c ) * ( b % c ) ) % c
3. ( a – b ) % c = ( ( a % c ) - ( b % c ) ) % c ( see notes at bottom )
4. ( a / b ) % c NOT EQUAL TO ( ( a % c ) / ( b % c ) ) % c
So, modulo is distributive over +, * and - but not / .
voidshuffleArrayIteratively(int[] cards) {for (int i =0; i <cards.length; i++) {int k =rand(0, i);// inclusiveint temp = cards[k]; cards[k] = cards[i]; cards[i] = temp; }}
Reservoir sampling
(*S has items to sample,R will contain the result*)ReservoirSample(S[1..n],R[1..k])// fill the reservoir arrayfor i =1 to kR[i] :=S[i]// replace elements with gradually decreasing probabilityfor i = k+1 to n j :=random(1, i)// important: inclusive rangeif j <= kR[j] :=S[i]
公约数/公倍数:
// 迭代privateintgcd(int a,int b) {while (b !=0) {int tmp = b; b = a % b; a = tmp; }return a;}// 最小公倍数 = (a * b) / gcd(a, b)// 递归intgcd(int a,int b) {if (b ==0) {return a; } else {returngcd(b, a % b); }}