CodeForces 1101A - Solution and Explanation
You can find problem statement here : CodeForces 1101A
Problem statement explanation :
2) it is less than 'l' or greater than 'r'.
3) it must be minimum possible number.
Example 1 : l=9 , r=11 , d=4 .The answer is '4'.
first find some number divisible by 4.They are "4,8,12,16". Among them '4'satisfies all condition.
1) 4 is divisible by 4
2) 4 is less than 9
3) 4 is the minimum number that satisfies previous 2 condition
Example 2 : l=7 , r=14 , d=7 .The answer is '21'.
first find some number divisible by 7.They are "7,14,21,18". Among them '7' satisfies all the condition.
Solution :
Naive Approach : you can run a loop to find the number that is divisible by 'd' but you will get TLE for this. because we have at max 500 queries.
Correct Approach : It is possible to find 'x' in O(1).
Complete Solution :
Problem statement explanation :
- There are q queries
- Every query has three input l,r and d.
- You have to find a positive number 'x' such that :
2) it is less than 'l' or greater than 'r'.
3) it must be minimum possible number.
Example 1 : l=9 , r=11 , d=4 .The answer is '4'.
first find some number divisible by 4.They are "4,8,12,16". Among them '4'satisfies all condition.
1) 4 is divisible by 4
2) 4 is less than 9
3) 4 is the minimum number that satisfies previous 2 condition
Example 2 : l=7 , r=14 , d=7 .The answer is '21'.
first find some number divisible by 7.They are "7,14,21,18". Among them '7' satisfies all the condition.
1) 21 is divisible by 7
2) 21 is greater than 14
3) 21 is the minimum number that satisfies previous 2 condition ( we can not take '7' because it is not less than 'l=7' && not greater than 'r=14' , '14' is not also possible answer) we take '21' as answer cause it is our next minimum number.
Solution :
Naive Approach : you can run a loop to find the number that is divisible by 'd' but you will get TLE for this. because we have at max 500 queries.
Correct Approach : It is possible to find 'x' in O(1).
if you divide a number(N) by another number(M) , you will get how many number is divisible by 'M' from 1 to 'N'. for example 10 / 2 = 5 , that means from 1 to 10 there are 5 numbers(2,4,6,8,10) that are divisible by 2.
if( l / d >= 1 )
x = d;
if( l != d && l / d >= 1)
x = d;
if( l / d == 0 ) {
x = ( ( r / d ) + 1 ) * d;
}
Complete Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int main(){ int n; cin >>n; while(n--) { int a , b , d; cin >> a >> b >> d; if(a != d && a/d >=1) { cout << d <<endl; } else { int k = b/d; cout << (k+1)*d <<endl; } } return 0; } |
Comments
Post a Comment