Codeforces 补题 Educational Round 19

本文解析了两个典型的编程问题:一是寻找整数n的k个因数,使其乘积等于n;二是从整数序列中找出子序列,使其和最大且为奇数。通过具体的代码实现展示了问题解决的方法。

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

Problem-A

Given a positive integer n, find k integers (not necessary distinct)
such that all these integers are strictly greater than 1, and their
product is equal to n.

Input The first line contains two integers n and k (2 ≤ n ≤ 100000,
1 ≤ k ≤ 20).

Output If it’s impossible to find the representation of n as a product
of k numbers, print -1.

Otherwise, print k integers in any order. Their product must be equal
to n. If there are multiple answers, print any of them.

理性分析后这题不难,主要是求出可能的因子序列,然后将其缩减到合适长度就行。这里有个坑就是刚开始为了方便用来 i*i<=n 但实际上当n还有剩余是,这部分是考虑不到的。例如测试数据为 9999 3
那么当i=11时,ii=121>n=101这里写图片描述

#include <cstdio>
#include <iostream>

using namespace std;

const int maxn = 1e5 + 10;
int n,k,seq[maxn],sz;

int main(){
    sz=0;
    scanf("%d%d",&n,&k);
    for(int i=2;i<=n;i++)
        while(n%i==0){
            seq[sz++]=i;
            n /= i;
        }


    if(sz<k){
        printf("-1\n");
        return 0;
    }

    while(sz>k){
        int t=seq[sz-1]*seq[sz-2];
        sz-=2;
        seq[sz++]=t;
    }

    for(int i=0;i<sz;i++)
        printf("%d ",seq[i]);
    printf("\n");

    return 0;
}

Problem-B

You are given sequence a1, a2, …, an of integer numbers of length n.
Your task is to find such subsequence that its sum is odd and maximum
among all such subsequences. It’s guaranteed that given sequence
contains subsequence with odd sum.

Subsequence is a sequence that can be derived from another sequence by
deleting some elements without changing the order of the remaining
elements.

You should write a program which finds sum of the best subsequence.

Input The first line contains integer number n (1 ≤ n ≤ 105).

The second line contains n integer numbers a1, a2, …, an
( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence
with odd sum.

Output Print sum of resulting subseqeuence.

这题算是DP的一种吧,我是看了别人的答案才有思路的,首先题目的目标是求出最大的子集奇数和,数据有正数和负数。首先应该把正数加到一起,看它是不是为奇数,如果是奇数,那么就一定是最大的。如果不是,那么就应该考虑减掉里面最小的正奇数,或者加上一个负奇数,总之就是在里面权衡找到最大的奇数。

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

const int maxn = 1e5+5;

int main(){
    int n,a[maxn],sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        if(a[i]>0)
            sum+=a[i];
    }
    if(sum&1) cout<<sum<<endl;
    else{
        int mn = 2e9;
        for(int i=0;i<n;i++)
            if(a[i]>0&&a[i]&1)
                mn = min(mn,a[i]);
        int ans = sum-mn;
        for(int i=0;i<n;i++)
            if(a[i]<0&&a[i]&1)
                ans = max(ans,sum+a[i]);
        cout<<ans<<endl;
    }
    return 0;
}
### 关于Codeforces Educational Round 171 #### A. The Contest 题目描述指出,在决定举办一场比赛之后,参赛者需要准备一系列材料,包括但不限于问题陈述、解决方案等。然而,协调员可能会突然要求更改某些测试案例的形式[^1]。 对于A题《The Contest》,时间限制为每组测试2秒,内存限制为256兆字节。输入来自标准输入流,输出至标准输出流[^3]。此题目的核心在于处理多组测试数据的情况,这通常意味着程序应当能够高效读取并解析多个独立的测试实例,并分别给出解答。 #### 输入格式说明 根据以往的比赛经验,输入的第一行可能包含两个整数`n`和`m`(1 ≤ n, m ≤ 5·10^5),表示用户的数量以及朋友关系集合的数量[^2]。不过需要注意的是,具体到Educational Round 171中的实际数值范围和其他细节会有所不同,请参照官方发布的最新信息为准。 #### 示例代码实现(Python) 下面是一个简单的模板用于解决涉及多组测试的数据结构化编程挑战: ```python def solve_problem(input_data): results = [] t = int(input()) # 获取总的测试次数t for _ in range(t): # 解析每一组测试的具体参数 params = list(map(int, input().split())) result = process(params) # 假设process函数实现了具体的逻辑运算 results.append(result) return "\n".join(str(x) for x in results) if __name__ == "__main__": from sys import stdin, stdout print(solve_problem(stdin)) ``` 该脚本展示了如何接收多轮次的标准输入并将它们作为单独的任务来执行,最后统一输出所有的计算结果。请注意调整`params`变量的内容以适应特定问题的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值