CodeForces Round #741(div.2)
The entrance of the contest => link
A. The Miracle and the Sleeper
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Problem
You are given two integers l
and r
, . Find the largest possible value of over all pairs (a,b)
of integers for which
As a reminder, is a remainder we get when dividing a
by b
. For example, .
Input
Each test contains multiple test cases.
The first line contains one positive integer t (), denoting the number of test cases. Description of the test cases follows.
The only line of each test case contains two integers l, r ().
Output
For every test case, output the largest possible value of a mod b over all pairs (a,b) of integers for which .
Example
input
1 | 4 |
output
1 | 0 |
Note
In the first test case, the only allowed pair is (a,b)=(1,1)
, for which .
In the second test case, the optimal choice is pair (a,b)=(1000000000,999999999)
, for which .
Solution
It’s pretty ez to determine that gets its max value as long as .
Consider a
as a determined constant value.
Supposing we got and (noticing that shoud stand as long as we have ), therefore we have and
Therefore we got , which means , that z
should never be greater than or equals to half of a
.
Meanwhile when we have , z
in consequence equals , which happens to be the best scenario to maximize z
.
Q.E.D.
As a
gets larger, z
in the consequence gets larger as well. Seems pretty ez, but what if when a
gets its maximum value which is r
, meanwhile b
still cannot satisfy the condition ?
Considering right now we have , still b
should equal to l
to maximize z
.
The code follows below:
1 | package com.company; |
B. Scenes From a Memory
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Problem
During the hypnosis session, Nicholas suddenly remembered a positive integer , which doesn’t contain zeros in decimal notation.
Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?
For some numbers doing so is impossible: for example, for number it’s impossible to delete some of its digits to obtain a not prime integer. However, for all n in the test cases of this problem, it’s guaranteed that it’s possible to delete some of their digits to obtain a not prime number.
Note that you cannot remove all the digits from the number.
A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. is neither a prime nor a composite number.
Input
Each test contains multiple test cases.
The first line contains one positive integer t (), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer k () — the number of digits in the number.
The second line of each test case contains a positive integer , which doesn’t contain zeros in decimal notation (). It is guaranteed that it is always possible to remove less than digits to make the number not prime.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all deletions.
If there are multiple solutions, print any.
Example
input
1 | 7 |
output
1 | 2 |
Note
In the first test case, you can’t delete digits from the number , as all the numbers , , and are prime. However, you can delete digit, obtaining a number .
In the second test case, you can delete all digits except one, as is a composite number.
Solution
According to the problem, we need to delete as many digits as possible so that the number is not a prime.
Therefore we have :
- If the number contains any digit of ‘1’, ‘4’, ‘6’, ‘8’, ‘9’, which is not a prime itself, in the best scenario, we can delete every other digits but this one. With one digit remaining.
- Otherwise we have to consider the condition where we have two digits left. Obviously when the same digit appears for more than once, we can keep only this two digits as the number ‘kk’ is not a prime ()
- Otherwise? The left scenario is where the digits ‘2’, ‘3’, ‘5’, ‘7’ each appears for no more than once.
- For any multi-digit number ending with ‘2’ or ‘5’ is not a prime, therefore if there is any digit comming before ‘2’ or ‘5’ we only keep two digits left.
- For any two-digit number ending with ‘7’, only ‘57’ and ‘27’ are not primes. Therefore if we have two of these digits, we keep two of them.
The code follows below:
1 | package com.company; |