image.png

Primes and factors素数和因子

image.png

Number of primes

image.png

One interesting class of prime:
image.png

试除法判断素数

  1. bool is_prime(int x)
  2. {
  3. if (x < 2) return false;
  4. for (int i = 2; i <= x / i; i++)
  5. if (x %i == 0) return false;
  6. return true;
  7. }

分解质因子

  1. void divide(int x)
  2. {
  3. for (int i = 2; i <= x / i; i++)
  4. if (x % i == 0)
  5. {
  6. int s = 0; // 计数器,记录个数
  7. while (x % i == 0) x /= i, s++;
  8. cout << i << ' ' << s << endl;
  9. }
  10. if (x > 1) cout << x << ' ' << 1 << endl;
  11. puts("");
  12. }

朴素筛法O(nlogn)

  1. void get_primes(int n)
  2. {
  3. for (int i = 2; i <= n; i++)
  4. {
  5. if (!st[i])
  6. {
  7. primes[cnt++] = i;
  8. }
  9. for (int j = i + i; j <= n; j += i) st[j] = true;
  10. }
  11. }

image.png

埃式筛法O(n) O(nloglogn)

  1. void get_primes(int n)
  2. {
  3. for (int i = 2; i <= n; i++)
  4. {
  5. if (st[i]) continue;
  6. primes[cnt++] = i;
  7. for (int j = i + i; j <= n; j += i)
  8. st[j] = true;
  9. }
  10. }

image.png

线性筛 O(n)

  1. // 核心:n只会被他的最小质因子筛掉
  2. void get_primes(int n)
  3. {
  4. for (int i = 2; i <= n; i++)
  5. {
  6. if (!st[i]) primes[cnt++] = i;
  7. for (int j = 0; primes[j] <= n / i; j++)
  8. {
  9. st[primes[j] * i] = true;
  10. if (i % primes[j] == 0) break;
  11. }
  12. }
  13. }

image.png

约数个数

image.png

  1. #include <iostream>
  2. #include <unordered_map>
  3. using namespace std;
  4. typedef long long LL;
  5. const int mod = 1e9 + 7;
  6. int main()
  7. {
  8. int n;
  9. cin >> n;
  10. unordered_map<int, int> primes;
  11. while (n--)
  12. {
  13. int x;
  14. cin >> x;
  15. for (int i = 2; i <= x / i; i++)
  16. while (x % i == 0)
  17. {
  18. x /= i;
  19. primes[i] ++;
  20. }
  21. if (x > 1) primes[x]++;
  22. }
  23. LL res = 1;
  24. unordered_map<int, int>::iterator it;
  25. for (it = primes.begin(); it != primes.end(); it++)
  26. res = res * ((*it).second + 1) % mod;
  27. cout << res << endl;
  28. return 0;
  29. }

image.png

约数之和

image.png

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <unordered_map>
  4. using namespace std;
  5. typedef long long LL;
  6. const int mod = 1e9 + 7;
  7. int main()
  8. {
  9. int n;
  10. cin >> n;
  11. unordered_map<int, int> primes;
  12. while (n--)
  13. {
  14. int x;
  15. cin >> x;
  16. for (int i = 2; i <= x / i; i++)
  17. while (x % i == 0)
  18. {
  19. x /= i;
  20. primes[i]++;
  21. }
  22. if (x > 1) primes[x]++;
  23. }
  24. LL res = 1;
  25. for (auto prime : primes)
  26. {
  27. int p = prime.first, a = prime.second;
  28. LL t = 1;
  29. while (a--) t = (t * p + 1) % mod;
  30. res = res * t % mod;
  31. }
  32. cout << res << endl;
  33. return 0;
  34. }

image.png

欧几里得算法(最大公约数)

image.png
image.png

  1. int gcd(int a, int b) {
  2. if (b == 0)
  3. return a;
  4. return gcd(b, a % b);
  5. }
  6. int gcd(int a, int b)
  7. {
  8. return b ? gcd(b, a % b) : a;
  9. }
  10. int gcd(int a, int b) {
  11. int tmp;
  12. while(b != 0) {
  13. tmp = b;
  14. b = a % b;
  15. a = tmp;
  16. }
  17. return a;
  18. }

IMG_2557.PNGIMG_2558.PNGIMG_2559.PNGIMG_2560.PNG

欧拉函数

image.png
image.png

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin >> n;
  8. while (n--)
  9. {
  10. int a;
  11. cin >> a;
  12. int res = a;
  13. for (int i = 2; i <= a / i; i++)
  14. {
  15. if (a % i == 0)
  16. {
  17. //res = res * (1 - 1 / i); //整除的问题,要调整一下
  18. res = res / i * (i - 1);
  19. while (a % i == 0) a /= i;
  20. }
  21. }
  22. if (a > 1) res = res / a * (a - 1);
  23. cout << res << endl;
  24. }
  25. return 0;
  26. }

筛法求欧拉函数

快速幂

  1. // 返回a^k % p
  2. int qmi(int a, int k, int p)
  3. {
  4. int res = 1;
  5. while (k) //当k不是0的时候
  6. {
  7. if (k & 1) res = (LL)res * a % p;
  8. k >>= 1;
  9. a = (LL)a * a % p;
  10. }
  11. return res;
  12. }

Modular arithmetic模运算

image.png

核心:随时取模

Fermat’s theorem费马定理

https://en.wikipedia.org/wiki/Fermat%27s_theorem

The works of the 17th-century mathematician Pierre de Fermat engendered many theorems. Fermat’s theorem may refer to one of the following theorems:

Fermat’s Last Theorem费马大定理

https://en.wikipedia.org/wiki/Fermat%27s_Last_Theorem
image.png
image.png

Fermat’s little theorem费马小定理

https://en.wikipedia.org/wiki/Fermat%27s_little_theorem
image.png
image.png

Euler’s theorem欧拉定理

image.png

Derangements错位排列

image.png

https://blog.csdn.net/bengshakalakaka/article/details/83420150
image.png

加法原理和乘法原理

image.png

排列和排列数

image.png

组合和组合数

image.png
image.png
image.png
更多的查看:https://oi-wiki.org/math/combinatorics/combination/

示例代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int n, m;
  5. ll C(int n, int m){
  6. ll sum = 1ll;
  7. for (int i = 1; i <= m; i++)
  8. sum = sum * (n - m + i) / i;
  9. return sum;
  10. }
  11. int main(){
  12. cin >> n >> m;
  13. cout << C(n, m) << '\n';
  14. return 0;
  15. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 110;
  5. int n, m;
  6. ll C[N][N];
  7. int main(){
  8. cin >> n >> m;
  9. C[0][0] = 1;
  10. for (int i = 1; i <= n; i++){
  11. C[i][0] = C[i][i] = 1;
  12. for (int j = 1; j <= n; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  13. }
  14. cout << C[n][m] << '\n';
  15. return 0;
  16. }

image.png
image.png
image.png
image.png
image.png

杨辉三角Pascal`s triangle

image.png

常见计数方法

特殊优先

image.png

捆绑与插空

image.png
image.png
image.png
image.png
image.png

容斥原理

https://oi-wiki.org/math/combinatorics/inclusion-exclusion-principle/#_2

Catalan numbers卡特兰数

image.png
image.png
image.png
image.png
说明:
一棵二叉树,根结点占 1 个结点,假设左子树 x 个结点,那么右子树有 n - 1 - x 个结点。
枚举 x for 0 … n-1,即可得到上面的式子。

image.png
image.png
说明:
number theory数论 - 图51个合法的组合数,减去非法组合数 number theory数论 - 图52,就得到合法的组合数

image.png
image.png

卡特兰数(Catalan)公式、证明、代码、典例.

https://blog.csdn.net/sherry_yue/article/details/88364746

杨辉三角

https://www.shuxuele.com/pascals-triangle.html
image.png
image.png
image.png
image.png
image.png
image.png
image.png

杨辉三角的应用

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

插板法原理及应用

http://blog.sina.com.cn/s/blog_1312fdb050102xmyc.html
image.png
image.png
image.png
image.png
image.pngimage.png