CodeForces - 798C

本文探讨了一种通过特定操作使序列的元素最大公约数大于1的方法,旨在寻找最少的操作次数以达到目标。介绍了输入输出格式,并给出了解决方案的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 numbers ai, 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.

Example
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.

首先题干那种操作只能保证两个奇数出来一定是偶数
单个奇数变偶数只要两次没错就只能保证这个….
其他的啥都保证不了..除了先天出现的gcd还是别想了….
想到这个就暗全变偶数猜一发能过就过过不了算了

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
int tu[100001];
int n;
typedef long long ll;
int gcd(int a, int b)
{
    if (b == 0) return a;
    return gcd(b, a%b);
}
int main()
{
    cin >> n;
    for(int a=1;a<=n;a++)scanf("%d",&tu[a]);
    int gg = tu[1];
    for (int a = 2; a <= n; a++)gg = gcd(gg, tu[a]);
    if (gg > 1)
    {
        cout << "YES" << endl;
        cout << 0;
        return 0;
    }
    int cz = 0;
    for (int a = 1; a <= n; a++)
    {
        int z = a - 1, y = a + 1;
        if (tu[a] % 2 == 0)continue;
        if (y <= n)
        {
            if (tu[y] % 2)tu[y] = tu[a] = 2,cz++;
            else
            {
                cz += 2;
                tu[a] = 2;
            }
            continue;
        }
        if (z > 0)
        {
            cz += 2;
            tu[a] = 2;
        }
    }
    cout << "YES" << endl;
    cout << cz;
}
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值