Intend to save the number theory board
1. GCD and LCM
are divided into and out:
gcd(a,b)=gcd(b,a%b);
More subtraction technique:
gcd(a,b)=gcd(a,b-a);
Record last night’s question: add a link description to the portal
2. Prime factor decomposition ( unique decomposition theorem )
void divide(int x){
for(int i=2;i<=x/i;i++)
if(x%i==0){
int s=0;
while(x%i==0) x/=i,s++;
cout<<i<<" "<<s<<endl;
}
if(x>1) cout<<x<<" "<<1<<endl;
cout<<endl;
}
3. Prime number
sieve
int pri[N],cnt;
bool st[N];
void prim(int x){
for(int i=2;i<=x;i++){
if(!st[i]){
pri[cnt++]=i;
for(int j=i+i;j<=x;j+=i)
st[j]=true;
}
}
}
Euler Sieve
int pri[N],cnt;
bool st[N];
void prim(int x){
for(int i=2;i<=x;i++){
if(!st[i]) pri[cnt++]=i;
for(int j=0;pri[j]<=x/i;j++){
st[pri[j]*i]=true;
if(i%pri[j]==0) break;
}
}
}
Interval sieve
4. Trial division method to find the approximate number
vector<int> get_divisors(int x)
{
vector<int> res;
for (int i = 1; i <= x / i; i ++ )
if (x % i == 0)
{
res.push_back(i);
if (i != x / i) res.push_back(x / i);
}
sort(res.begin(), res.end());
return res;
}
5. Approximate number
If N = p1^c1 * p2^c2 *… *pk^ck
divisor number: (c1 + 1) * (c2 + 1) *… * (ck + 1)
sum of divisors: (p1^0 + p1^1 +… + p1^c1) *… * (pk^0 + pk^1 +… + pk^ck)
unordered_map<int, int> primes;
while (n -- )
{
int x;
cin >> x;
for (int i = 2; i <= x / i; i ++ )
while (x % i == 0)
{
x /= i;
primes[i] ++ ;
}
if (x > 1) primes[x] ++ ;
}
LL res = 1;
for (auto p : primes) res = res * (p.second + 1) % mod;
6. Sum of approximate numbers
unordered_map<int, int> primes;
while (n -- )
{
int x;
cin >> x;
for (int i = 2; i <= x / i; i ++ )
while (x % i == 0)
{
x /= i;
primes[i] ++ ;
}
if (x > 1) primes[x] ++ ;
}
LL res = 1;
for (auto p : primes)
{
LL a = p.first, b = p.second;
LL t = 1;
while (b -- ) t = (t * a + 1) % mod;
res = res * t % mod;
}
7. The number of Euler functions
1 ~ N that are relatively prime to N is called Euler's function
int a;
cin>>a;
int res=a;
for(int i=2;i<=a/i;i++)
if(a%i==0){
res=res/i*(i-1);
while(a%i==0) a/=i;
}
if(a>1) res=res/a*(a-1);
cout<<res<<endl;
8. Sieve method to find Euler's function
int prime[N],cnt,phi[N];
bool st[N];
ll get_eulers(int n){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!st[i]){
prime[cnt++]=i;
phi[i]=i-1;
}
for(int j=0;prime[j]<=n/i;j++){
st[prime[j]*i]=true;
if(i%prime[j]==0){
phi[i*prime[j]]=prime[j]*phi[i];
break;
}
phi[i*prime[j]]=(prime[j]-1)*phi[i];
}
}
ll res=0;
for(int i=1;i<=n;i++) res+=phi[i];
return res;
}
9. Fast power
ll ksm(ll a,ll b,ll p){
ll res=1;
a%=p;
while(b){
//&运算当相应位上的数都是1时,该位取1,否则该为0。
if(b&1)
res=1ll*res*a%p;//转换为ll型
a=1ll*a*a%p;
b>>=1;//十进制下每除10整数位就退一位
}
return res;
}
10. Turtle speed multiplication
Find the value of a multiplied by b modulo p.
1≤a,b,p≤10^18
ll cul(ll a,ll b,ll p){
ll res=0;
while(b){
if(b&1)
res=(res+a)%p;
a=(a+a)%p;
b>>=1;
}
return res;
}
11. Multiplicative inverse element
If the integers b and m are relatively prime, and for any integer a, if b|a is satisfied, then there is an integer x such that a/b≡a∗x(mod m), then x is the inverse of the modulo m multiplication of b Yuan, denoted as b−1(mod m).
The necessary and sufficient condition for the existence of the multiplicative inverse element of b is that b and the modulus m are relatively prime. When the modulus m is a prime number, bm−2 is the multiplicative inverse of b.
cin>>a>>p;
if(a%p==0) puts("impossible");
else cout<<ksm(a,p-2,p)<<endl;
12. Extended Euclidean algorithm
Given n pairs of positive integers ai, bi, for each pair of numbers, find a set of xi, yi such that it satisfies ai∗xi+bi∗yi=gcd(ai,bi).
int exgcd(int a,int b,int &x,int &y){
if(!b){
x=1,y=0;
return a;
}
int d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
13. China's remainder theorem
Given 2n integers a1, a2,...,an and m1,m2,...,mn, find a smallest non-negative integer x that satisfies ∀i∈[1,n],x≡mi(mod ai).
14. Gaussian Elimination
15. Matrix Multiplication
From AcWing