Educational Codeforces Round 17 A+B

A. k-th divisor

time limit per test:2 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

You are given two integers n and k. Find k-th smallest divisor of n, or report that it doesn’t exist.

Divisor of n is any such natural number, that n can be divided by it without remainder.

Input

The first line contains two integers n and k (1 ≤ n ≤ 1015, 1 ≤ k ≤ 109).

Output

If n has less than k divisors, output -1.

Otherwise, output the k-th smallest divisor of n.

Examples

Input
4 2

Output
2

Input
5 3

Output
-1

Input
12 5

Output
6

Note

In the first example, number 4 has three divisors: 1, 2 and 4. The second one is 2.

In the second example, number 5 has only two divisors: 1 and 5. The third divisor doesn’t exist, so the answer is -1.
题意:问n的第k小的因子是谁,不存在输出-1。
题解:o(sqrt(n))求每个因子。set存一下即可。
代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e7+10;
set<ll>st;
set<ll>::iterator it;
int main()
{
    ll n,k;
    cin>>n>>k;
    for(ll i=1;i*i<=n;i++)
    {
        if(n%i==0)
        {
            st.insert(i);
            st.insert(n/i);
        }
    }
    for(it=st.begin();it!=st.end();it++)
    {
        if(--k==0)
        {
            cout<<*it<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;
}

B. USB vs. PS/2

time limit per test:2 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!

The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options.

You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.

You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.

Input

The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 105) — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.

The next line contains one integer m (0 ≤ m ≤ 3·105) — the number of mouses in the price list.

The next m lines each describe another mouse. The i-th line contains first integer vali (1 ≤ vali ≤ 109) — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.

Output

Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.

Example

Input
2 1 1
4
5 USB
6 PS/2
3 PS/2
7 PS/2

Output
3 14

Note

In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
题意:一台电脑有a个usb接口,b个PS/2接口,c个万能接口。给出m个鼠标的价钱和类型。问最多购买鼠标的数量和在这之下的花费最小值。
题解:贪心。将价格升序排序,优先消费a,b接口。
代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<int,string> >vr;
int a,b,c,m,pr;
string na;
string n1="USB";
string n2="PS/2";
int main()
{
    cin>>a>>b>>c;
    cin>>m;
    for(int i=1; i<=m; i++)
    {
        cin>>pr>>na;
        vr.push_back({pr,na});
    }
    sort(vr.begin(),vr.end());
    int cnt=0;
    ll ans=0;
    for(int i=0; i<m; i++)
    {
        if(a&&vr[i].second==n1)
        {
            cnt++;
            a--;
            ans+=vr[i].first;
            continue;
        }
        if(b&&vr[i].second==n2)
        {
            cnt++;
            b--;
            ans+=vr[i].first;
            continue;
        }
        if(c)
        {
            cnt++;
            c--;
            ans+=vr[i].first;
            continue;
        }
    }
    cout<<cnt<<" "<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值