Codeforces 补题 Educational Round 19

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值