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.
The first line contains two integers n and k (1 ≤ n ≤ 1015, 1 ≤ k ≤ 109).
If n has less than k divisors, output -1.
Otherwise, output the k-th smallest divisor of n.
4 2
2
5 3
-1
12 5
6
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.
1 #include2 #define ll long long int 3 #define N 1000005 4 #define mem(a) memset(a,0,sizeof(a)) 5 ll k[N]; 6 using namespace std; 7 ll n; 8 int m; 9 set s;10 int main(){11 cin>>n>>m;12 int cnt=sqrt(n);13 for(int i=1;i<=cnt;i++){14 if(n%i==0){15 s.insert(i);16 s.insert(n/i);17 }18 }19 set ::iterator it;20 bool prime=true;21 for(it=s.begin();it!=s.end();it++){22 m--;23 if(m==0){24 cout<<*it<
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.
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 two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.
2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2
3 14
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.
1 #include2 #define ll long long int 3 #define N 300050 4 using namespace std; 5 multiset c,t,r; 6 ll n,m,k,s; 7 ll x; 8 ll inde; 9 string st;10 int main(){11 cin>>n>>m>>k>>s;12 while(s--){13 cin>>x>>st;14 if(st[0]=='U'){15 c.insert(x);16 }else{17 t.insert(x);18 }19 }20 multiset ::iterator it;21 ll cnt=0;22 for(it=c.begin();it!=c.end();it++){23 if(n){24 cnt+=*it;25 inde++;26 n--;27 }else{28 r.insert(*it);29 }30 }31 for(it=t.begin();it!=t.end();it++){32 if(m){33 cnt+=*it;34 inde++;35 m--;36 }else{37 r.insert(*it);38 }39 }40 for(it=r.begin();it!=r.end();it++){41 if(k){42 cnt+=*it;43 inde++;44 k--;45 }46 }47 cout< <<" "< <