Educational Codeforces Round 17 A+B
2017-01-27 12:17
351 查看
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存一下即可。
代码:
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接口。
代码:
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; }
相关文章推荐
- Educational Codeforces Round 17
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings(前缀后缀的妙用)
- 【Educational Codeforces Round 17 A题】有所不同的大暴力
- [Educational Codeforces Round 17 D (762D)] Maximum path
- [Educational Codeforces Round 17 E (762E)] Radio stations
- Educational Codeforces Round 17 C. Two strings(二分)
- Educational Codeforces Round 17 C. Two strings(字符串前缀后缀处理,好题)
- Educational Codeforces Round 17 C 二分
- [Educational Codeforces Round 17 F (762F)] Tree nesting
- Educational Codeforces Round 17 C. Two strings 打表二分
- Educational Codeforces Round 17 D dp
- Educational Codeforces Round 17 D. Maximum path DP
- Educational Codeforces Round 17 题解
- Educational Codeforces Round 17
- Educational Codeforces Round 17 颓废记
- Educational Codeforces Round 17-D. Maximum path
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings 详细的题解 (前后缀应用)
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings (二分 + 字符串前后缀)
- Codeforces Educational Codeforces Round 17 Problem.A kth-divisor (暴力+stl)
- [Educational Codeforces Round 17 C (762C)] Two strings