×

# Logic for RIDDLE99 LoC JUN17

 0 I could get the first 2 test cases correctly but got WA (not TLE) for 3 and 4. Tried to think a lot but couldn't understand what is wrong here. Any help is appreciated. Thanks! Here is the logic I used for this question:  import java.util.*; class c { public static void main(String []args) { long t,a,b,m; Scanner sc = new Scanner(System.in); t=sc.nextLong(); for(long i=1;i<=t;i++) { a=sc.nextLong(); b=sc.nextLong(); m=sc.nextLong(); long c=0; if(m>b)System.out.println("0"); else if(m

 0 Your code gives wrong output for such type of test cases, which is common on larger numbers- Input 1 1007 2009767 55 Your Output 36522 Correct Output 36523  On changing 1007 to 1045 (which is the 19th multiple of 55) your code prints correct output since you made sure to include those type of cases. The difference between your logics is that in computer, you cannot claim that b/m -a/m = (b-a)/m always. Take example of- b=10, a=8,m=9 10/9-8/9=1 (10-8)/9=0  Check this test case also- Input 1 11 100 9 Your Output 9 Correct Output 10 What you are doing is, (100-11)/9=89/9=9 Neither 100 nor 11 are multiple of 9. What he did is, 100/9 - (11-1)/9=11-1=10.  To correct it, you should adjust a and b to nearest allowed multiple of the number. Like, a should be raised to nearest multiple of m, and b should be lowered to nearest multiple of n. Then your code prints correct answer. In the end, by (b-a)/m we are seeing how many multiples of m we can fit inside the range STARTING from a. If you start from 11, you can fit multiples in range of 11 to (11+81) = [11,92]. The multiples in this range is 9. But we can prove by contradiction, that if you start from 18, multiples will be more. [11+7,92+7]=[18,99]. There is 1 more multiple here. This is the edge case to your logic. answered 04 Jul '17, 01:35 15.2k●1●18●61 accept rate: 18% 2 @vijju123 his code fails when (b-a)/m!=b/m-a/m and here is the test case with small numbers 1 5 25 3 (04 Jul '17, 01:42) 1 Yes, i figured it out dear. Was editing the answer whole time XD. Did a proper research on this behavior today, you can say :p (04 Jul '17, 01:45)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,701
×210
×193
×181
×1
×1

question asked: 04 Jul '17, 00:34

question was seen: 490 times

last updated: 05 Jul '17, 00:56