随笔分类 -  算法 / 数论

摘要:求组合数 typedef long long LL; // 最大 C(66, 33) LL C(int a, int b) { LL res = 1; for (int i = a, j = 1; j <= b; i--, j++) { res = res * i / j; } return res 阅读全文

posted @ 2023-03-14 23:03 lyc2002 阅读(43) 评论(0) 推荐(0)

摘要:扩展欧几里得算法 代码 typedef long long LL; LL exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } LL d = exgcd(b, a % b, y, x); y -= 阅读全文

posted @ 2023-02-24 19:59 lyc2002 阅读(31) 评论(0) 推荐(0)

摘要:试除法求约数 时间复杂度 O(√n) 代码 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 != 阅读全文

posted @ 2023-02-24 15:24 lyc2002 阅读(67) 评论(0) 推荐(0)

摘要:试除法判断质数 时间复杂度 O(√n) 代码 bool is_prime(int x) { if (x == 1) return false; for (int i = 2; i <= x / i; i++) if (x % i == 0) return false; return true; } 阅读全文

posted @ 2023-02-23 22:04 lyc2002 阅读(48) 评论(0) 推荐(0)

摘要:时间复杂度 O(log max(a, b)) 最大公约数 代码 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } 最小公倍数 代码 int lcm(int a ,int b) { return a * b / gcd(b, a 阅读全文

posted @ 2023-02-22 16:58 lyc2002 阅读(49) 评论(0) 推荐(0)