Educational Codeforces Round 19

教育场就是教你做人,自古教育场就比div2难点(雾

A. k-Factorization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
100000 2
output
2 50000 
input
100000 20
output
-1
input
1024 5
output
2 64 2 2 2 

 A题就是给你一个数n让你把它拆成k个数相乘,这不就是分解因子,本来以为还要用素因子个数

对于任意的整型N,分解质因数得到N= P1^x1 * P2^x2* …… * Pn^xn;

则N的因子个数M为 M=(x1+1) * (x2+1) * …… *(xn+1);

趁机贴下n!因子个数求法 

然后,求每一个质因子的指数个数,这里用到了一个公式,:

     ei=[N/pi^1]+ [N/pi^2]+ …… + [N/pi^n]  其中[]为取整

       附:这一步最近又想到了一个更好的方法  int ei=0;while(N)  ei+=(N/=pi);   怎么样?? 

           (想一想为什么,实在想不通你就举个例子试一下)

最后,就是套公式计算了,M=(e1+1)*(e2+1)*……*(en+1)

这个其实都用不到的,数据量很小的,直接求就可以的

#include <bits/stdc++.h>
using namespace std;
int n , k;
vector < int > v;
int main(){
    cin >> n >> k;
    v.clear();
    for(int i = 2 ; i <= n ; ++i){
        if(n % i == 0){
            while(n % i == 0){
                n /= i;
                v.push_back(i);
            }
        }
    }
    if(v.size() < k){
        cout << -1 ;
    }
    else{
        for(int i = 1 ; i < k ; ++i){
            printf("%d " , v[i - 1]);
        }
        int ans = 1;
        for(int i = k - 1 ; i < v.size() ; ++i){
            ans *= v[i];
        }
        printf("%d\n" , ans);
    }
}

 

B. Odd sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
4
-2 2 -3 1
output
3
input
3
2 -5 -3
output
-1
Note

In the first example sum of the second and the fourth elements is 3.

 

给你n个数,让你选一个子序列使此序列的和是奇数,而且最大

首先我想到的做法是dp,但是我并不会实现啊。感觉有点懵,反正就是一直枚举下,求和

但是一想,还有做法是,先求出所有正数的和,这个和一定是最大的,偶数减奇数还是奇数,他要是奇数就输出啊,因为已经最大了

所以就是找奇数了,如果找负数的话,当然是最大的那个奇数了,这个数你没有加过,要加的。

如果找偶数,当然是最小的奇数了,这个数你加过了,要减去

因为存在全是正或全是负的数列,所以max一下找到最大值即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    int s=0;
    int mi=1<<30;
    int ma=-1<<30;
    for(int i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        if(t>0)
        s+=t;
        if(t<0&&t%2&&t>ma)
        ma=t;
        if(t>0&&t%2&&t<mi)
        mi=t;
    }
    if(s%2==0)
    cout<<max(s-mi,s+ma)<<endl;
    else
    cout<<s<<endl;
}

dp做法,我想的dp确实不太对啊,我想的是我去处理每次的异或值,相同得0,不同得1,两个奇数偶数相加都是奇数,奇偶相加都是奇数,可是很明显是要分组的,因为这样就不用考虑那么多了,可是最后都是贪心算法

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define N 100001
int dp[N][2];

int main(){
  int n;
  for(int x = 0; x < 2; x++)
   dp[0][x] = INT_MIN / 2;
  scanf("%d", &n);
  for(int i = 1; i <= n; i++){
    int a;
    scanf("%d", &a);
    int x = (a % 2 != 0);
    for(int y = 0; y < 2; y++){
      int k = (x + y) % 2;
      dp[i][k] = max(dp[i - 1][k], dp[i - 1][y] + a);
    }
    dp[i][x] = max(dp[i][x], a);
  }
  printf("%d\n", dp[n][1]);
  return 0;
}

 

C. Minimal string
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves:

  • Extract the first character of s and append t with this character.
  • Extract the last character of t and append u with this character.

Petya wants to get strings s and t empty and string u lexigraphically minimal.

You should write a program that will help Petya win the game.

Input

First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters.

Output

Print resulting string u.

Examples
input
cab
output
abc
input
acdb
output
abdc

 

 

先翻译一下题目吧,给你一个字符串s,你可以拿s的首字母加到字符串t后面,你还可以把t的首字母加到u后面,本来t、u都是空的,让你找最小字典序字符串u

我觉得它像个汉内塔一样,也像个单调栈?反正就是贪心下,先输出小的,然后维护下就ok

#include <bits/stdc++.h>
using namespace std;
int num[255];
string s;
int check(char c)
{
    for(int i='a';i<c;i++)
    if(num[i])
    return 0;
    return 1;
}
stack<char>q;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>s;
    for(int i=0;s[i];i++)
    num[s[i]]++;
    int cnt=0;
    while(cnt<s.size())
    {
        if(q.empty())
        {
            q.push(s[cnt]);
            num[s[cnt]]--;
            cnt++;
        }
        else if(check(q.top()))
        {
            cout<<q.top();
            q.pop();
        }
        else
        {
            q.push(s[cnt]);
            num[s[cnt]]--;
            cnt++;
        }
    }
    while(!q.empty())
    {
        cout<<q.top();
        q.pop();
    }
    return 0;
}

 

posted @ 2017-06-10 17:47  暴力都不会的蒟蒻  阅读(363)  评论(0编辑  收藏  举报