CF#798 C. Mike and gcd problem(思维)

探讨通过特定操作使序列的各元素最大公约数大于1的方法,及其实现思路。
C. Mike and gcd problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .

Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbersai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.

 is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

Input

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

Output

Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.

If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

Examples
input
2
1 1
output
YES
1
input
3
6 2 4
output
YES
0
input
2
1 3
output
YES
1
Note

In the first example you can simply make one move to obtain sequence [0, 2] with .

In the second example the gcd of the sequence is already greater than 1.


题意:给出一个序列 A ,我们可以将相邻两元素 a [ i ] , a [ i + 1 ] 改为 a [ i ] - a [ i + 1 ] 和 a [ i ] + a [ i + 1 ] ,问至少多少次操作后能使 gcd ( A1 ~ An) 大于1,若无法完成,则输出 NO

思路:显然我们把所有奇数修改成偶数即可满足条件,每次修改时若  a [ i ] 和 a [ i + 1 ] 同为奇数则修改一次,若一奇一偶则修改两次


#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;

int n;
int a[N],c[N];

int gcd(int n,int m)
{
    int t = 1;
    while(t)
    {
        t = n % m;
        n = m;
        m = t;
    }
    return n;
}

int main()
{
    while(scanf("%d", &n) == 1)
    {
        memset(c,0,sizeof(c));
        for(int i=0; i < n; i++)
        {
            scanf("%d",& a[i]);
        }
        int ans = 0,tmp = gcd(a[0], a[1]);
        for(int i = 0; i < n; i++)
        {
            tmp = gcd(tmp, a[i]);
            if(a[i] & 1 && !c[i])
            {
                if(a[i + 1] & 1) ans++;
                else ans += 2;
                c[i] = 1;
                c[i + 1] = 1;
            }
        }
        if(tmp != 1) printf("YES\n0\n");
        else printf("YES\n%d\n", ans);
    }
}

 
<think> 我们正在解决MCP-Kali-Server项目部署中的资源国内源替换问。用户希望将克隆项目、创建虚拟环境、安装依赖库、配置和启动服务端等步骤中涉及的资源替换为国内源,以加速部署过程。 解决方案: 1. 项目克隆:使用国内镜像源(如GitHub镜像站) 2. Python虚拟环境:使用国内源下载Python(如果需要)或使用系统自带Python 3. 依赖库安装:使用国内PyPI镜像(如清华、阿里云) 4. 服务端配置和启动:不涉及外部资源,无需替换 具体步骤: 一、项目克隆替换为国内源 由于GitHub克隆可能较慢,我们可以使用镜像站。常见镜像站有: - https://hub.njuu.cf - https://hub.yzuu.cf - https://ghproxy.com 将原始URL:https://github.com/Wh0am123/MCP-Kali-Server.git 替换为镜像URL(以hub.njuu.cf为例): https://hub.njuu.cf/Wh0am123/MCP-Kali-Server.git 命令示例: ```bash git clone https://hub.njuu.cf/Wh0am123/MCP-Kali-Server.git ``` 二、创建Python虚拟环境 这一步通常不需要外部资源,但若要下载特定Python版本,可使用国内源(如华为镜像)下载Python安装包。不过,Kali Linux通常自带Python,因此我们主要关注pip源的替换。 三、依赖库安装替换为国内源 在安装依赖库时,使用国内PyPI镜像可以极大加快速度。常用镜像: - 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple - 阿里云:https://mirrors.aliyun.com/pypi/simple - 豆瓣:https://pypi.douban.com/simple 有两种方式指定镜像源: a) 临时使用(在pip install命令后添加-i参数) b) 永久修改(创建或修改pip配置文件) 在本项目中,我们使用临时方式,因为项目可能只需要一次性安装依赖。 修改后的安装命令: ```bash pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 如果没有requirements.txt,则手动安装依赖: ```bash pip install flask requests impacket bloodhound coercer -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 四、配置服务端和启动服务端 这两步不涉及从外部下载资源,因此不需要替换源。 完整修正后的部署流程: 1. 克隆项目(使用镜像源): ```bash git clone https://hub.njuu.cf/Wh0am123/MCP-Kali-Server.git cd MCP-Kali-Server ``` 2. 创建Python虚拟环境: ```bash python3 -m venv venv source venv/bin/activate ``` 3. 安装依赖库(使用清华源): ```bash pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 如果requirements.txt不存在: ```bash pip install flask requests impacket bloodhound coercer -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 4. 配置服务端: ```bash cp config.example.py config.py # 编辑config.py(可选) ``` 5. 启动服务端: ```bash python3 kali_server.py ``` 注意:如果使用国内镜像源后遇到SSL证书问,可以在pip命令中添加`--trusted-host`参数: ```bash pip install ... -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn ``` 生成相关问: §§相关问§§ 1. 如何永久修改pip的源为国内镜像? 2. 除了GitHub镜像站,还有哪些方法可以加速GitHub项目的克隆? 3. 在Kali Linux中如何安装特定版本的Python? 4. 如何解决pip安装依赖时的SSL证书验证错误? </think> 以下是MCP-Kali-Server项目部署过程中关键资源的国内镜像源解决方案,针对克隆、环境创建、依赖安装等环节进行了优化: --- ### 一、项目克隆国内源 **问**:`git clone`连接被重置 **解决方案**: ```bash # 使用GitHub镜像站(推荐) git clone https://hub.yzuu.cf/Wh0am123/MCP-Kali-Server.git # 备用镜像(选择延迟最低的) git clone https://gitclone.com/github.com/Wh0am123/MCP-Kali-Server.git ``` > **说明**:国内镜像站通过缓存GitHub内容实现加速,速度可提升3-10倍[^1] --- ### 二、Python虚拟环境配置 **依赖**:Python解释器 **国内源配置**: ```bash # 更新APT源为阿里云镜像 sudo sed -i 's|http://http.kali.org|https://mirrors.aliyun.com/kali|g' /etc/apt/sources.list # 安装Python环境 sudo apt update && sudo apt install -y python3.11 python3.11-venv # 创建虚拟环境(使用系统自带Python) python3.11 -m venv venv ``` --- ### 三、依赖库安装国内源 **核心依赖**:`flask`, `requests`, `impacket`等 **PyPI镜像配置**: ```bash # 永久配置清华源 echo "[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf # 安装依赖(自动使用镜像源) source venv/bin/activate pip install -r requirements.txt # 若无文件则手动安装: pip install flask impacket bloodhound coercer ``` > **可选镜像站**: > - 阿里云:`https://mirrors.aliyun.com/pypi/simple` > - 豆瓣:`https://pypi.douban.com/simple` > - 华为云:`https://repo.huaweicloud.com/repository/pypi/simple` --- ### 四、服务端配置优化 **缺失文件处理**: ```bash # 若缺少config.example.py,从GitHub直接下载 curl -O https://raw.githubusercontent.com/Wh0am123/MCP-Kali-Server/main/config.example.py # 创建配置文件 cp config.example.py config.py sed -i 's/5000/8080/g' config.py # 修改默认端口 ``` --- ### 五、服务启动命令 **修复文件路径错误**: ```bash # 确认入口文件位置(通常为app.py或main.py) find . -name "*.py" | grep -E 'app|main|server' # 启动服务(示例) python3 src/kali_server.py # 根据实际路径调整 ``` --- ### 六、完整优化流程 ```mermaid graph LR A[克隆项目] --> B[hub.yzuu.cf镜像] B --> C[配置阿里云APT源] C --> D[创建Python虚拟环境] D --> E[设置清华PyPI源] E --> F[安装依赖] F --> G[修改配置文件] G --> H[启动服务] ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值