数论集合算法符号的c++代码运用
2025-08-04 21:41:03
发布于:广东
- 数论相关符号(π、φ、μ、∑、∏)
#include <iostream>
#include <cmath> // 含 M_PI(圆周率π)
#include <vector>
using namespace std;
// φ(n):欧拉函数(小于n且与n互质的数的个数)
int euler_phi(int n) {
int result = n;
for (int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
// μ(n):莫比乌斯函数(数论中用于筛法)
int mobius_mu(int n) {
int cnt = 0; // 质因子次数是否超过1
for (int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
n /= p;
cnt++;
if (n % p == 0) // 有平方因子
return 0;
}
}
if (n > 1) cnt++;
return (cnt % 2 == 0) ? 1 : -1; // 偶数个质因子为1,奇数为-1
}
// ∑:求和(1到n的和)
int sum(int n) {
int res = 0;
for (int i = 1; i <= n; ++i)
res += i;
return res;
}
// ∏:求积(1到n的阶乘)
long long product(int n) {
long long res = 1;
for (int i = 1; i <= n; ++i)
res *= i;
return res;
}
int main() {
cout << "π(圆周率)≈ " << M_PI << endl; // 圆周率π
cout << "φ(6) = " << euler_phi(6) << endl; // 欧拉函数:φ(6)=2
cout << "μ(6) = " << mobius_mu(6) << endl; // 莫比乌斯函数:μ(6)=1(质因子2、3,偶数个)
cout << "∑(1到5) = " << sum(5) << endl; // 求和:1+2+3+4+5=15
cout << "∏(1到5) = " << product(5) << endl; // 求积:5! = 120
return 0;
}
- 集合运算(∈、∉、∩、∪)
#include <iostream>
#include <unordered_set>
using namespace std;
// 检查元素是否属于集合(∈)
bool is_element_of(int x, const unordered_set<int>& s) {
return s.count(x) > 0;
}
// 集合交集(∩)
unordered_set<int> intersection(const unordered_set<int>& a, const unordered_set<int>& b) {
unordered_set<int> res;
for (int x : a) {
if (b.count(x)) // x同时属于a和b
res.insert(x);
}
return res;
}
// 集合并集(∪)
unordered_set<int> union_set(const unordered_set<int>& a, const unordered_set<int>& b) {
unordered_set<int> res = a; // 先复制a
res.insert(b.begin(), b.end()); // 加入b的元素(自动去重)
return res;
}
int main() {
unordered_set<int> s = {1, 2, 3, 4};
int x = 2, y = 5;
cout << x << " ∈ {1,2,3,4}? " << (is_element_of(x, s) ? "是" : "否") << endl; // 是
cout << y << " ∉ {1,2,3,4}? " << (!is_element_of(y, s) ? "是" : "否") << endl; // 是
unordered_set<int> a = {1, 2, 3}, b = {3, 4, 5};
auto inter = intersection(a, b);
cout << "a ∩ b = {"; // 交集:{3}
for (int x : inter) cout << x << " ";
cout << "}\n";
auto unin = union_set(a, b);
cout << "a ∪ b = {"; // 并集:{1,2,3,4,5}
for (int x : unin) cout << x << " ";
cout << "}\n";
return 0;
}
- 算法复杂度与逻辑符号(O、∀、∃、⊕)
#include <iostream>
#include <vector>
using namespace std;
// O(n)复杂度示例:检查数组中是否存在元素x(∃)
bool exists(const vector<int>& arr, int x) {
for (int num : arr) { // 线性遍历,时间复杂度O(n)
if (num == x)
return true; // 存在x(∃x)
}
return false;
}
// O(n)复杂度示例:检查所有元素是否为正数(∀)
bool all_positive(const vector<int>& arr) {
for (int num : arr) { // 线性遍历,时间复杂度O(n)
if (num <= 0)
return false; // 存在非正数,不满足∀
}
return true; // 所有元素为正数(∀x>0)
}
// 异或运算(⊕)
int xor_operation(int a, int b) {
return a ^ b; // C++中^表示异或(⊕)
}
int main() {
vector<int> arr = {2, 4, 6, 8};
cout << "∃ 4 in arr? " << (exists(arr, 4) ? "是" : "否") << endl; // 是
cout << "∀ 元素>0? " << (all_positive(arr) ? "是" : "否") << endl; // 是
cout << "3 ⊕ 5 = " << xor_operation(3, 5) << endl; // 3(011)⊕5(101)=6(110)
return 0;
}
- 其他符号(∞、∇梯度示例)
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// 表示无穷大(∞):用一个极大值模拟
const double INF = 1e18;
// 简单梯度(∇)计算:对函数f(x,y)=x²+y²求梯度(2x, 2y)
vector<double> gradient(double x, double y) {
return {2*x, 2*y}; // ∇f = (df/dx, df/dy)
}
int main() {
cout << "无穷大(∞)模拟值:" << INF << endl;
double x = 3, y = 4;
auto grad = gradient(x, y);
cout << "f(x,y)=x²+y²在(" << x << "," << y << ")处的梯度∇ = (" << grad[0] << "," << grad[1] << ")" << endl; // (6,8)
return 0;
}
全部评论 7
太牛了
1周前 来自 广东
1nice
1周前 来自 广东
1可以可以
1周前 来自 广东
1挺牛掰啊
1周前 来自 广东
1666,有点实力
1周前 来自 广东
1禁止主包自娱自乐
1周前 来自 浙江
0这是人 机吧🤔
1周前 来自 浙江
0
有帮助,赞一个