Document directory
- Header file
- Decomposition of N Factors
- Hexadecimal conversion
- Newton Iteration Method
- Matrix Multiplication
Header file
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <ctype. h>
# Include <math. h>
# Include <time. h>
# Include <set>
# Include <map>
# Include <stack>
# Include <queue>
# Include <string>
# Include <bitset>
# Include <vector>
# Include <deque>
# Include <utility>
# Include <list>
# Include <sstream>
# Include <iostream>
# Include <functional>
# Include <numeric>
# Include <algorithm>
Using namespace STD;
Template <class T> inline t iabs (const T & V) {return v <0? -V: V ;}
Template <class T> inline t strto (string s) {istringstream is (s); t V; is> V; return V ;}
Template <class T> inline string tostr (const T & V) {ostringstream OS; OS <v; return OS. STR ();}
Template <class T> inline int Cmin (T & A, const T & B) {return B <? A = B, 1:0 ;}
Template <class T> inline int Cmax (T & A, const T & B) {return a <B? A = B, 1:0 ;}
Template <class T> inline int cbit (t n) {return n? Cbit (N & (n-1) + 1:0 ;}
# Define EP 1e-10
# Define CLR (ARR, v) memset (ARR, V, sizeof (ARR ))
# Define Sq (A) (a) * ())
# Define DEBUG (a) printf ("% s = % s \ n", # a, toStr (a). c_str ())
# Define FOR (I, s, e) for (int (I) = (s); (I) <(e); I ++)
Typedef unsigned _ int64 u64;
Bool is [130000]; // used to calculate the prime number []
Int prm [30000]; // used to save prime numbers
Int totleprm; // record the total number of prime numbers
Map <int, int> factor; // the form used for factorization.
_ Int64 ans [50000]; // used to calculate the approximate number
Set <__ int64> p; // used to record an appointment
Returns the number of prime numbers.
// Bool is [130000]; // used to calculate the prime number []
// Int prm [30000]; // used to save prime numbers
Int totleprm; // record the total number of prime numbers
Int getprm (_ int64 n)
{
Int I, j, k = 0;
Int S, E = (INT) (SQRT (0.0 + n) + 1 );
Memset (is, 1, sizeof (is ));
PRM [k ++] = 2;
Is [0] = is [1] = 0;
For (I = 4; I <n; I + = 2) is [I] = 0;
For (I = 3; I <E; I + = 2) if (is [I])
{
PRM [k ++] = I;
For (S = I * 2, j = I * I; j <n; j + = s)
Is [J] = 0;
}
For (; I <n; I + = 2)
If (is [I]) prm [k ++] = I;
Return k;
}
Evaluate a * B % c
Requirements: the range of a and B is generally within the range of the hugeint. When the value of hugeint is unsigned _ int64, a and B must be numbers that can be expressed by _ int64.
U64 product_mod (u64 a, u64 B, u64 c)
{
U64 ret = 0, tmp = a % c;
While (B)
{
If (B & 0x1)
If (ret + = tmp)> = c)
Ret-= c;
If (tmp <= 1)> = c)
Tmp-= c;
B> = 1;
}
Return ret;
}
Evaluate a ^ B % c
Requirements: a and B generally fall within the _ int64 range. When _ int64 is unsigned _ int64, a and B must be the numbers that can be expressed by _ int64.
U64 power_mod (u64 A, u64 B, u64 C)
{
U64 R = 1, D =;
While (B)
{
If (B & 1) R = product_mod (R, D, C );
D = product_mod (D, D, C );
B> = 1;
}
Return R;
}
Evaluate the Euler's function value of x
// Prm [I] is the I-th prime number.
U64 euler (u64 x)
{
Int I;
U64 res = x;
For (I = 0; prm [I] <(_ int64) sqrt (x * 1.0) + 1 & I <totleprm; I ++)
If (x % prm [I] = 0)
{
Res = res/prm [I] * (prm [I]-1 );
While (x % prm [I] = 0) x/= prm [I];
}
If (x> 1) res = res/x * (x-1 );
Return res;
}
Evaluate the Euler's function value of x
U64 phi [2, 1000001];
Void euler (int maxn)
{
Int I;
Int j;
For (I = 2; I <= maxn; I ++)
Phi [I] = I;
For (I = 2; I <= maxn; I + = 2)
Phi [I]/= 2;
For (I = 3; I <= maxn; I + = 2)
If (phi [I] = I)
{
For (j = I; j <= maxn; j + = I)
Phi [j] = phi [j]/I * (I-1 );
}
}
Decomposition of n Factors
// The decomposed factor is stored in map <int, int> factor.
// When value is 1, each value has a factor [prm [I] + 1;
// When value =-1, there is a factor, factor [prm [I]-1;
Int getFactor (u64 n, int value)
{
Factor. Clear ();
Int sum = 0;
Int I;
Map <int, int >:: iterator it;
Int e = (INT) (SQRT (0.0 + n) + 1 );
For (I = 0; I <totleprm & PRM [I] <E; I ++)
{
If (N % PRM [I] = 0)
{
While (N % PRM [I] = 0 & n! = 1)
{
It = factor. Find (PRM [I]);
If (IT = factor. End ())
Factor [PRM [I] = value;
Else
Factor [PRM [I] + = value;
N = N/PRM [I];
Sum ++;
}
}
}
If (n! = 1)
{
It = factor. Find (N );
If (IT = factor. End ())
Factor [N] = value;
Else
Factor [N] + = value;
Sum ++;
}
Return sum;
}
Evaluate all n approx.
// Set <__ int64> P; used to save the approximate number
Set <__ int64 >:: iterator IP address;
// Map <int, int> factor; n factorization
Set <u64> getappnum (u64 N ){
Set <u64> P;
Set <u64>: iterator IP address;
U64 ans [10000];
Factor. Clear ();
Getfactor (n, 1 );
P. insert (1 );
Map <int, int >:: iterator it;
For (IT = factor. Begin (); it! = Factor. End (); It ++)
While (IT-> second )! = 0)
{
Int flag = 0;
For (IP = P. Begin (); IP! = P. End (); IP ++)
Ans [flag ++] = (* IP) * (IT-> first );
For (INT I = 0; I <flag; I ++)
P. insert (ANS [I]);
(It-> second )--;
}
Return p;
}
Random Number, which can be simply replaced by rand ()
_ Int64 rAndom ()
{
_ Int64;
A = rand ();
A * = rand ();
A * = rand ();
A * = rand ();
Return;
}
Rabinmiller method to test whether n is a prime number
Int pri [] = {, 19 };
Bool isprime (_ int64 n)
{
If (n <2)
Return false;
If (n = 2)
Return true;
If (! (N & 1 ))
Return false;
_ Int64 k = 0, I, j, m,;
M = n-1;
While (m % 2 = 0)
M = (m> 1), k ++;
For (I = 0; I <10; I ++)
{
If (pri [I]> = n) return 1;
A = power_mod (pri [I], m, n );
If (a = 1)
Continue;
For (j = 0; j <k; j ++)
{
If (a = N-1) break;
A = product_mod (a, a, n );
}
If (j <k)
Continue;
Return false;
}
Return true;
}
A non-1 factor of N is given by pollard_rov decomposition. If N is returned, it is not found once.
_ Int64 pollard_rov (_ int64 C, _ int64 N)
{
_ Int64 I, X, Y, K, D;
I = 1;
X = rand () % N;
Y = X;
K = 2;
Do
{
I ++;
D = gcd (N + Y-X, N );
If (D> 1 & D <N) return D;
If (I = K) Y = X, K * = 2;
X = (product_mod (X, X, N) + N-C) % N;
} While (Y! = X );
Return N;
}
Find the minimum prime factor of N
_ Int64 Company (_ int64 N)
{
If (isprime (N) return N;
Do
{
_ Int64 T = maid (rand () % (N-1) + 1, N );
If (T <N)
{
_ Int64 A, B;
A = rock (T );
B = rock (N/T );
Return A <B? A: B;
}
}
While (true );
}
Understanding of homogeneous equations using China's residual theory a = bi (modni)
Long solmodu (long z, long B [], long n [])
{
Int I;
Long a, m, x, y, t;
M = 1; a = 0;
For (I = 0; I <z; I ++) m * = n [I];
For (I = 0; I <z; I ++)
{
T = m/n [I];
ExEuclid (n [I], t, x, y );
A = (a + t * y * B [I]) % m;
}
Return (a + m) % m;
}
Hexadecimal conversion
Convert a 10-digit number to m-digit number.
Returns the length of the converted number, which exists in array a, and the number on the I bit represents a [I] * (m ^ I)
Int changExquisite (int a [], u64 n, int m)
{
Int I = 0;
While (n> 0)
{
A [I ++] = n % m;
N = n/m;
}
Return I;
}
Newton Iteration Method
Double NT_sqrt (double n)
{
Double m = 1;
While (fabs (m-(m + n/m)/2)> 1e-6 ){
M = (m + n/m)/2;
}
Return m;
}
Matrix Multiplication
# Define N 100;
Struct Mat
{
Long matrix [N] [N];
};
Mat mul (Mat a, Mat B, int N)
{
Int I, j, k;
Mat c;
For (I = 0; I <N; I ++)
For (j = 0; j <N; j ++)
{
C. matrix [I] [j] = 0;
For (k = 0; k <N; k ++)
{
C. matrix [I] [j] + = a. matrix [I] [k] * B. matrix [k] [j];
}
}
Return c;
}
Evaluate the n power of the matrix
Mat po (Mat a, int n)
{
If (n = 1)
Return;
Mat k = po (a, n/2 );
If (n % 2 = 0)
Return mul (k, k, 2 );
Else
Return mul (k, k, 2), a, 2 );
}
The number represented by string s is used to touch an integer.
U64 strmod (char * s, u64 t)
{
U64 sum = 0;
Int I, len = strlen (s );
For (I = 0; I <len; I ++)
{
Sum = sum * 10 + s [I]-'0 ';
While (sum> = t)
Sum-= t;
}
Return sum;
}
Find GCD and LCM
U64 gcd (u64 a, u64 B)
{
Int c;
If (a <B)
{
C =;
A = B;
B = c;
}
While (a % B)
{
C = a % B;
A = B;
B = c;
}
Return B;
}
U64 getlcm (u64 A, u64 B)
{
Return a * B/gcd (A, B );
}
Initialize the prime number table if the number of I that meets gcd (n, I) = 1 and I <m is true
Int geteul (int n, int m)
{
Int factor [100];
Int num = 0;
Int e = (INT) (SQRT (0.0 + n) + 1 );
For (INT I = 0; I <totleprm & PRM [I] <= E; I ++)
If (N % PRM [I] = 0)
{
Factor [num ++] = PRM [I];
While (n % prm [I] = 0 & n! = 1)
N = n/prm [I];
}
If (n! = 1)
Factor [num ++] = n;
Int ans = 0;
FOR (I, 1, (1 <num ))
{
Int sign = I;
Int lcm = 1;
Int time = 0;
Int j = 0;
While (sign> 0)
{
If (sign % 2 = 1)
{
Lcm = getLCM (lcm, factor [j]);
Time ++;
}
Sign = sign> 1;
J ++;
}
If (time % 2 = 1)
Ans + = m/LCM;
Else
Ans-= m/LCM;
}
Return M-ans;
}
Evaluate A ^ B
U64 power (u64 A, u64 B)
{
U64 r = 1, D =;
While (B)
{
If (B & 1) r = r * D;
D = D * D;
B> = 1;
}
Return R;
}
The number of 1 in the Binary Expression of a number
Int getOneNum (u64 n)
{
Int sum = 0
While (n> 0)
{
If (n & 1)
Sum ++;
N = n> 1;
}
Return sum;
}
Obtain the I-bit of the Binary Expression of a number.
Int isone (u64 N, int I)
{
If (N & (1 <I) = 0)
Return 0;
Return 1;
}
Evaluate the number of k-n reciprocal elements. A prime number table is required.
U64 kthprim (u64 N, u64 K)
{
U64 nn = N;
U64 factor [100];
Int num = 0;
For (INT I = 0; I <totleprm & PRM [I] * PRM [I] <= N; I ++)
If (NN % PRM [I] = 0)
{
Factor [num ++] = PRM [I];
While (NN % PRM [I] = 0 & NN! = 1)
Nn/= PRM [I];
}
If (NN! = 1)
Factor [num ++] = nn;
U64 L = 1;
// Note that the upper bound must be large enough.
U64 H = 1000000000;
U64 m;
While (L
{
M = (L + H)> 1;
U64 ans = m;
FOR (I, 1, (1 <num ))
{
U64 lcm = 1;
U64 ans_help = 0;
Int sum = 0;
FOR (j, 0, num)
If (isOne (I, j) = 1)
{
Sum ++;
Lcm = lcm * factor [j];
}
If (sum & 1)
Ans-= m/lcm;
Else
Ans + = m/lcm;
}
If (ans = k)
{
While (gcd (m, n )! = 1) m --;
Return m;
}
If (ANS <K)
L = m + 1;
Else
H = m;
}
Return 0;
}