复习dddddddd

news2025/2/22 17:33:00

1.

思路:用队列先进先出的特性

#include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;


int main() {
    int n; cin >> n;
    queue<pair<int, int>>arr; // 下标,值
    n = pow(2, n);
    for (int i = 1; i <= n; i++) {
        int temp;
        cin >> temp;
        arr.push({ i,temp });
    }

    while (arr.size() > 2) {
        auto l = arr.front();
        arr.pop();
        auto r = arr.front();
        arr.pop();
        if (l.second > r.second)arr.push({ l.first,l.second });
        else arr.push({ r.first,r.second });
    }
    auto l = arr.front();
    arr.pop();
    auto r = arr.front();
    if (l.second > r.second)cout << r.first;
    else cout << l.first;

    return 0;
}

 2.

思路:二叉树的数组定义来写

#include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

struct Node {
    int l, r;
}s[1000001];
int tt = -1;

void dfs(int n,int coun) {
    if (n==0)return;
    tt = max(tt, coun);
    dfs(s[n].l, coun + 1);
    dfs(s[n].r, coun + 1);
}

int main() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> s[i].l >> s[i].r;
    dfs(1,1);
    cout << tt << endl;
    return 0;
}

 3.

思路: 用递归的方法来分出左右子树,以及以其为子节点为节点的左右子树,直到叶子节点

#include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

void func(string& s1, string& s2) {
    char root = s1[0];
    int l = s2.find(root), r = s1.size() - l - 1;
    if (l) {
        string str1 = s1.substr(1, l), str2 = s2.substr(0, l);
        func(str1, str2);
    }
    if (r) {
        string str1 = s1.substr(l + 1), str2 = s2.substr(l + 1);
        func(str1, str2);
    }
    cout << root;

}

int main() {
    string s1, s2;
    cin >> s2 >> s1;
    func(s1, s2);
    return 0;
}

4.

思路:广搜探索所有的组成方式

#include <iostream>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;

 
typedef pair<string, string> Rule;

 
int bfs(const string& A, const string& B, const vector<Rule>& rules) {
    if (A == B) return 0;  

    queue<pair<string, int>> q; 
    q.push({A, 0});

    unordered_set<string> visited;  
    visited.insert(A);

    while (!q.empty()) {
        auto current = q.front();
        q.pop();
        string currentStr = current.first;
        int steps = current.second;

        
        if (currentStr == B) {
            return steps;
        }

     
        if (steps >= 10) {
            continue;
        }

     
        for (const auto& rule : rules) {
            const string& from = rule.first;
            const string& to = rule.second;

            size_t pos = currentStr.find(from);
            while (pos != string::npos) {
   
                string newStr = currentStr;
                newStr.replace(pos, from.length(), to);

      
                if (visited.find(newStr) == visited.end()) {
                    visited.insert(newStr);
                    q.push({newStr, steps + 1});
                }
 
                pos = currentStr.find(from, pos + 1);
            }
        }
    }

    return -1; 
}

int main() {
    string A, B;
    cin >> A >> B;

    vector<Rule> rules;
    string a, b;
    while (cin >> a >> b) {
        rules.push_back({a, b});
    }

    int result = bfs(A, B, rules);

    if (result != -1) {
        cout << result << endl;
    } else {
        cout << "NO ANSWER!" << endl;
    }

    return 0;
}

 5.算阶

 思路:二进制枚举每个位置上能否放1,经过整个流程后还是1,则则这个位置是1,同时整个的值也要小于m才是ans

#include <iostream>
#include <vector>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

pair<string, int> operations[100005];
int n, m;

int calc(int bit, int now) {
    for (int i = 1; i <= n; i++) {
        int x = operations[i].second >> bit & 1;
        if (operations[i].first == "AND") now &= x;
        else if (operations[i].first == "OR") now |= x;
        else now ^= x;
    }
    return now;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        char str[5]; int num;
        scanf("%s%d", str, &num);
        operations[i] = make_pair(str, num);
    }

    int val = 0, ans = 0;
    for (int bit = 29; bit >= 0; bit--) {
        int res0 = calc(bit, 0);
        int res1 = calc(bit, 1);
        if ((val + (1 << bit)) <= m && res0 < res1) {
            val += 1 << bit; ans += res1 << bit;
        }
        else {
            ans += res0 << bit;
        }
    }

    cout << ans << endl;
    return 0;
}

6.

思路:

解题思路

  1. 唯一分解定理
    将 a 分解为素数的乘积:

    a=p1k1​​⋅p2k2​​⋅⋯⋅pnkn​​

    则 ab 的因子和为:

    σ(ab)=(1+p1​+p12​+⋯+p1k1​⋅b​)⋅(1+p2​+p22​+⋯+p2k2​⋅b​)⋅⋯⋅(1+pn​+pn2​+⋯+pnkn​⋅b​)
  2. 等比数列求和
    对于每个素数 pi​,其对应的等比数列和为:

    Si​=1+pi​+pi2​+⋯+piki​⋅b​=pi​−1piki​⋅b+1​−1​
  3. 模运算性质
    由于结果需要对 9901 取模,我们需要在计算过程中对每一步结果取模,避免溢出。

  4. 快速幂算法
    计算 piki​⋅b+1​ 时,使用快速幂算法提高效率。

  5. 逆元计算
    在计算等比数列和时,分母 pi​−1 可能不为 1,因此需要计算 pi​−1 在模 9901 下的逆元。

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

const int MOD = 9901;

// 快速幂算法
long long fastPow(long long base, long long exp, long long mod) {
    long long result = 1;
    while (exp > 0) {
        if (exp & 1) {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}

// 扩展欧几里得算法求逆元
int extendedEuclidean(int a, int m, int& x, int& y) {
    if (m == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int gcd = extendedEuclidean(m, a % m, x1, y1);
    x = y1;
    y = x1 - (a / m) * y1;
    return gcd;
}

// 计算 a 在模 m 下的逆元
int modularInverse(int a, int m) {
    int x, y;
    int gcd = extendedEuclidean(a, m, x, y);
    if (gcd != 1) {
        return -1; // 逆元不存在
    } else {
        return (x % m + m) % m; // 确保结果为正
    }
}

// 分解质因数
vector<pair<int, int>> primeFactorization(int n) {
    vector<pair<int, int>> factors;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            int count = 0;
            while (n % i == 0) {
                n /= i;
                count++;
            }
            factors.push_back({i, count});
        }
    }
    if (n > 1) {
        factors.push_back({n, 1});
    }
    return factors;
}

// 计算 a^b 的因子和对 MOD 取模
int sumOfDivisors(int a, int b) {
    if (a == 0) return 0; // 0^b 的因子和为 0
    if (b == 0) return 1; // a^0 的因子和为 1

    // 分解质因数
    vector<pair<int, int>> factors = primeFactorization(a);

    long long result = 1;
    for (const auto& factor : factors) {
        int p = factor.first;
        int k = factor.second;

        // 计算等比数列和 S = (p^(k*b + 1) - 1) / (p - 1)
        long long numerator = (fastPow(p, k * b + 1, MOD) - 1 + MOD) % MOD;
        long long denominator = (p - 1) % MOD;

        // 如果 p == 1,等比数列和为 k * b + 1
        if (p == 1) {
            result = (result * (k * b + 1)) % MOD;
            continue;
        }

        // 计算分母的逆元
        int inv = modularInverse(denominator, MOD);
        if (inv == -1) {
            // 如果分母为 0,等比数列和为 k * b + 1
            result = (result * (k * b + 1)) % MOD;
        } else {
            // 计算 S 并对 MOD 取模
            long long S = (numerator * inv) % MOD;
            result = (result * S) % MOD;
        }
    }

    return result;
}

int main() {
    int a, b;
    cin >> a >> b;
    cout << sumOfDivisors(a, b) << endl;
    return 0;
}

 7.逆元

逆元是数论中的一个重要概念,尤其在模运算中广泛应用。它的核心思想是解决模意义下的“除法”问题。因为模运算中除法并不直接定义,而是通过乘法逆元来实现。


1. 逆元的定义

给定一个整数 a 和一个模数 m,如果存在一个整数 x,使得:

a⋅x≡1(modm)

则称 x 是 a 在模 m 下的乘法逆元,记作 a−1。


2. 将模等式转化为常规等式

模等式 a⋅x≡1(modm) 可以转化为常规等式:

a⋅x+m⋅k=1

其中 k 是某个整数。这个等式表明,a⋅x 与 1 的差是 m 的倍数。


3. 逆元的存在条件

a 在模 m 下的逆元存在的充要条件是 a 和 m 互质,即:

gcd(a,m)=1

如果 a 和 m 不互质,则 a 在模 m 下没有逆元。


4. 逆元的计算方法

(1) 扩展欧几里得算法

扩展欧几里得算法可以求解方程 a⋅x+m⋅k=1。如果 gcd(a,m)=1,则 x 就是 a 在模 m 下的逆元。

步骤:

  1. 使用扩展欧几里得算法求解 gcd(a,m) 以及 x 和 k。
  2. 如果 gcd(a,m)=1,则 x 是 a 的逆元。
  3. 将 x 调整为正数(通过取模)。
#include <iostream>
using namespace std;

// 扩展欧几里得算法
int extendedEuclidean(int a, int m, int& x, int& y) {
    if (m == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int gcd = extendedEuclidean(m, a % m, x1, y1);
    x = y1;
    y = x1 - (a / m) * y1;
    return gcd;
}

// 计算 a 在模 m 下的逆元
int modularInverse(int a, int m) {
    int x, y;
    int gcd = extendedEuclidean(a, m, x, y);
    if (gcd != 1) {
        return -1; // 逆元不存在
    } else {
        return (x % m + m) % m; // 确保结果为正
    }
}

int main() {
    int a, m;
    cout << "请输入 a 和 m: ";
    cin >> a >> m;

    int inv = modularInverse(a, m);
    if (inv == -1) {
        cout << a << " 在模 " << m << " 下没有逆元。" << endl;
    } else {
        cout << a << " 在模 " << m << " 下的逆元是: " << inv << endl;
    }

    return 0;
}

 

(2) 费马小定理

如果模数 m 是素数,且 a 不是 m 的倍数,则根据费马小定理:

a^m−1≡1(modm)

因此,a 的逆元为:

a−1≡a^m−2(modm)

#include <iostream>
using namespace std;

// 快速幂算法
long long fastPow(long long a, long long b, long long mod) {
    long long result = 1;
    while (b > 0) {
        if (b & 1) {
            result = (result * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }
    return result;
}

// 计算 a 在模 m 下的逆元(费马小定理)
int modularInverse(int a, int m) {
    return fastPow(a, m - 2, m);
}

int main() {
    int a, m;
    cout << "请输入 a 和 m: ";
    cin >> a >> m;

    if (m <= 1 || a % m == 0) {
        cout << a << " 在模 " << m << " 下没有逆元。" << endl;
    } else {
        int inv = modularInverse(a, m);
        cout << a << " 在模 " << m << " 下的逆元是: " << inv << endl;
    }

    return 0;
}

逆元运用

5. 逆元的应用

  1. 模意义下的除法
    • 在模运算中,a/b 等价于 a⋅b−1(modm)。
  2. 线性同余方程
    • 解方程 a⋅x≡b(modm) 时,需要用到 a 的逆元。
  3. 组合数学
    • 计算组合数 C(n,k) 时,通常需要用到模意义下的除法。
  4. 密码学
    • RSA 加密算法中,逆元用于计算私钥。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2303538.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

大数据技术Kafka详解 ⑥ | Kafka大厂面试题

目录 1、为什么要使用kafka? 2、kafka消费过的消息如何再消费? 3、kafka的数据是放在磁盘上还是内存上&#xff0c;为什么速度会快? 4、kafka数据怎么保障不丢失? 4.1、生产者数据的不丢失 4.2、消费者数据的不丢失 4.3、kafka集群中的broker的数据不丢失 5、采集数…

Jupyter里面的manim编程学习

1.Jupyterlab的使用 因为我之前一直都是使用的vscode进行manim编程的&#xff0c;但是今天看的这个教程使用的是Jupyter&#xff0c;我也很是好奇这个manim在Jupyter这样的交互式下面会生成怎么样的效果&#xff0c;所以今天尝试了jupyter&#xff0c;并且对于两个进行比较和说…

hot100_19. 删除链表的倒数第 N 个结点

hot100_19. 删除链表的倒数第 N 个结点 思路 给你一个链表&#xff0c;删除链表的倒数第 n 个结点&#xff0c;并且返回链表的头结点。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5], n 2 输出&#xff1a;[1,2,3,5] 示例 2&#xff1a; 输入&#xff1a;head […

✨1.HTML、CSS 和 JavaScript 是什么?

✨✨ HTML、CSS 和 JavaScript 是构建网页的三大核心技术&#xff0c;它们相互协作&#xff0c;让网页呈现出丰富的内容、精美的样式和交互功能。以下为你详细介绍&#xff1a; &#x1f98b;1. HTML&#xff08;超文本标记语言&#xff09; 定义&#xff1a;HTML 是一种用于描…

机器学习的数学基础(三)——概率与信息论

目录 1. 随机变量2. 概率分布2.1 离散型变量和概率质量函数2.2 连续型变量和概率密度函数 3. 边缘概率4. 条件概率5. 条件概率的链式法则6. 独立性和条件独立性7. 期望、方差和协方差7.1 期望7.2 方差7.3 协方差 8. 常用概率分布8.1 均匀分布 U ( a , b ) U(a, b) U(a,b)8.2 Be…

flutter将utf-8编码的字节序列转换为中英文字符串

这里遇到的问题是&#xff0c;我通过某种方式拿到了utf-8编码的字节序列&#xff0c;我只知道他们对应的是中英文字符。怎么将其转成中英文&#xff0c;并打印&#xff0c;让我对utf-8编码有了些许许的了解。 这里记录一下转换代码&#xff1a; String wifiName \xE9\xA1\xB…

IM聊天系统架构实现

一、IM系统整体架构 二、企业级IM系统如何实现心跳与断线重连机制&#xff1b; 1、重连机制&#xff08;服务端下线&#xff09; 服务端下线&#xff0c;客户端netty可以感知到&#xff0c;在感知的方法中进行重连的操作&#xff0c;注意重连可能连接到旧的服务器继续报错&…

基于腾讯云大模型知识引擎×DeepSeek构建八字、六爻赛博算卦娱乐应用

引言 随着DeepSeek的火爆&#xff0c;其强大的思维链让不少人越用越香&#xff0c;由于其缜密的思维和推理能力&#xff0c;不少人开发出了不少花里胡哨的玩法&#xff0c;其中一种就是以八字、六爻为代表的玄学文化正以“赛博玄学”的新形态席卷年轻群体。 针对于八字、六爻…

linux文件管理命令ln

linux文件管理命令ln 1、软链接2、硬链接3、命令参数3.1、必要参数3.2、选择参数 4、应用示例4.1、创建硬链接4.2、创建软链接&#xff08;符号链接&#xff09;4.3、 对目录创建软链接4.4、强制覆盖目标文件 5、应用场景 它的功能是为某一个文件在另外一个位置建立一个同步的链…

CT dicom 去除床板 去除床位,检查床去除

1. 前言 医院拍摄患者CT与MRI 图像&#xff0c; 但是CT图像中就会出现检查床的区域&#xff0c;来看CT扫描设备是什么样子的&#xff0c;红色标出区域 可以在图中看到&#xff0c;在头部位置安装有固定头部的类似支架的东西&#xff0c;这个东西拍摄出来时什么样子呢&#xff…

扩散模型中,Flow Matching的训练方式相比于 DDPM 训练方法有何优势?

在扩散模型中,Flow Matching(FM)相比DDPM(Denoising Diffusion Probabilistic Models)的训练方法具有以下核心优势: 1. 更简单的训练目标 DDPM:通过逐步预测噪声来间接优化数据分布的变分下界(ELBO),需要设计多步的噪声调度策略,训练目标依赖马尔可夫链的分解。Flow…

【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5

往期 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_0&#xff1a;介绍了题目和背景【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_1&#xff1a;题目输入的格式说明&#xff0c;选择了邻接表…

Python实现GO鹅优化算法优化随机森林分类模型项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后关注获取。 1.项目背景 在当今数据驱动的世界中&#xff0c;机器学习技术被广泛应用于各种领域&#xff0c;如金融、医疗、…

【目标检测】【YOLOv4】YOLOv4:目标检测的最佳速度与精度

YOLOv4&#xff1a;目标检测的最佳速度与精度 0.论文摘要 有许多特征被认为可以提高卷积神经网络&#xff08;CNN&#xff09;的准确性。需要在大规模数据集上对这些特征的组合进行实际测试&#xff0c;并对结果进行理论上的验证。某些特征仅适用于特定模型和特定问题&#…

常用电脑,护眼软件推荐 f.lux 3400K | 撰写论文 paper

常用电脑&#xff1f;平均每天用 5 个小时&#xff1f;你就要考虑用一个护眼软件了&#xff0c;对皮肤也好。因为电脑屏幕有辐射&#xff0c;比如蓝光。 f.lux 作为一款专业护眼软件&#xff0c;值得使用。之前用了三年的 Iris Pro&#xff0c;现在 f.lux 做的更好了。 使用…

新手小白如何挖掘cnvd通用漏洞之存储xss漏洞(利用xss钓鱼)

视频教程和更多福利在我主页简介或专栏里 &#xff08;不懂都可以来问我 专栏找我哦&#xff09; 如果对你有帮助你可以来专栏找我&#xff0c;我可以无偿分享给你对你更有帮助的一些经验和资料哦 目录&#xff1a; 一、XSS的三种类型&#xff1a; 二、XSS攻击的危害&#x…

ros通信与回调函数多线程应用

一、ros topic通信流程 参考资料: 一个ros节点至少有几个线程(1058): https://zhuanlan.zhihu.com/p/680188065 鱼香rostopic底层流程(1005~1008): https://zhuanlan.zhihu.com/p/656716718 王方浩-ROS发布订阅实现(二): https://zhuanlan.zhihu.com/p/439208597 基础的topic…

智能交通系统(Intelligent Transportation Systems):智慧城市中的交通革新

智能交通系统&#xff08;Intelligent Transportation Systems, ITS&#xff09;是利用先进的信息技术、通信技术、传感技术、计算机技术以及自动化技术等&#xff0c;来提升交通系统效率和安全性的一种交通管理方式。ITS通过收集和分析交通数据&#xff0c;智能化地调度、控制…

基于LM Arena 的 LLM 基准测试排行榜:DeepSeek-R1 排名第 5

打开 Arena 网站&#xff1a;https://lmarena.ai/&#xff0c;点开 Leaderboard 可以看到上图的排行榜&#xff0c;可以看到 DeepSeek-R1 排名第 5。

【QT】第一个 QT程序(对象树)

&#x1f308; 个人主页&#xff1a;Zfox_ &#x1f525; 系列专栏&#xff1a;Qt 目录 一&#xff1a;&#x1f525; QtHelloWorld程序 &#x1f98b; 使⽤"标签"实现纯代码⽅式实现可视化操作实现 &#x1f98b; 使⽤"按钮"实现可视化操作实现纯代码实现…