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时,
i∗i=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;
}