CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Challenge 2013
    • May Cook-Off 2013
    • May Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » May Challenge 2012  » Divisible Pairs

Divisible Pairs

Problem code: DIVPAIR

  • All Submissions

All submissions for this problem are available.

Given N and M Dexter wants to know how many pairs a,b(1 <= a < b <=N) are there such that (a+b) is divisible by M. For example when N=4 and M=3, there are 2 possible pairs the sum of which is divisible by M and they are (1,2) and (2,4).

Input

First line of input contains T(<=100000) which is the number of test cases. Each of the next T lines contains two integers N(1 <= N <= 10^9) and M(2 <= M <= 10^9).

Output

Output one line per testcase, the number of pairs (a,b) as described before.

Example

Input:
3
2 3
4 3
1 6

Output:
1
2
0


Author: rustinpiece
Tester: subra
Editorial http://discuss.codechef.com/problems/DIVPAIR
Date Added: 11-03-2012
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

@admin in the case of N=4,M=3

dey_jayanta @ 2 May 2012 12:01 AM
@admin in the case of N=4,M=3 another pair (1,5) is also possible.So the output should be 3 instead of 2!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@dey_jayanta : 1<=a < b<=n

phantom11 @ 2 May 2012 12:24 AM
@dey_jayanta : 1<=a < b<=n ..Read the question carefully

thanks....

dey_jayanta @ 2 May 2012 12:25 AM
thanks....

can a and b be both equal ??

krans4u @ 2 May 2012 12:26 AM
can a and b be both equal ??

@ dey_jayanta is 5<=4 ??

vladamg98 @ 2 May 2012 12:28 AM
@ dey_jayanta is 5<=4 ??

@krans4u-do u know the

checklist @ 2 May 2012 01:20 AM
@krans4u-do u know the meaning of a < b????Read carefully

wow getting TLE....:'(

checklist @ 2 May 2012 01:24 AM
wow getting TLE....:'(

how to get over this time

vibhor500 @ 2 May 2012 02:03 AM
how to get over this time limit thing

indeed i have checked moi

altaf1148 @ 2 May 2012 02:31 AM
indeed i have checked moi code by putting the max value of n and m ,,,, it is giving result...

got TLE with cout, and AC

kohyatoh @ 2 May 2012 11:53 AM
got TLE with cout, and AC with printf. It'll be better to add notice for huge I/O?

@altaf : check

vladamg98 @ 2 May 2012 12:45 PM
@altaf : check 100000 1000000000 1 (and that 100000 times)

@vladamg98 m>=2 and m!=1

selfcompiler @ 2 May 2012 01:04 PM
@vladamg98 m>=2 and m!=1

scanf("%lld")may have some

lrcrichard @ 2 May 2012 01:10 PM
scanf("%lld")may have some problem, use the cin instead.

@Admin getting TLE please

a1_b21991 @ 2 May 2012 04:21 PM
@Admin getting TLE please tell something

@Admin getting TLE please

a1_b21991 @ 2 May 2012 04:21 PM
@Admin getting TLE please tell something

used 2 loops for and while

yashvardhan90 @ 2 May 2012 04:25 PM
used 2 loops for and while getting time limit exceeded. any solution ??

need some help ...how to deal

rong @ 2 May 2012 06:15 PM
need some help ...how to deal with the problem TLE...

I am not using a single loop,

santano1988 @ 2 May 2012 07:14 PM
I am not using a single loop, still getting a runtime error (other). I am using C#, please suggest what might the problem be?

I am implementing an O(1)

jainharshit @ 2 May 2012 07:39 PM
I am implementing an O(1) solution.. still getting TLE... what to do ?

There certainly is a problem

siteid123 @ 2 May 2012 09:44 PM
There certainly is a problem . As @jainharshit said, even O(T) is giving TLE.

@admin please look into this

siteid123 @ 2 May 2012 11:12 PM
@admin please look into this problem's time limits. Even this (1004669) is giving TLE.

Got rid of TLE but now

krans4u @ 2 May 2012 11:24 PM
Got rid of TLE but now getting wrong answer .. any typical test case please ??

is answer of n=10^9 and m=1

chirayu @ 3 May 2012 12:12 AM
is answer of n=10^9 and m=1 in range of ULL??

@chirayu and @s1aurabhpal_7 :

swaprocks123 @ 3 May 2012 01:28 AM
@chirayu and @s1aurabhpal_7 : malways starts from 2.

@admin: Since (1 <= a < b

poojits @ 3 May 2012 05:28 AM
@admin: Since (1 <= a < b <=N) For N=1 how would you define b since b>a and NOT >=a?

@poojits for N=1, there does

crg91 @ 3 May 2012 06:34 AM
@poojits for N=1, there does not exist any (a,b) pair. So answer should be 0

ans for 10^9 2 is

akash2504 @ 3 May 2012 11:01 AM
ans for 10^9 2 is 249999999500000000?????

I have used 2 loops except

damangarg001 @ 3 May 2012 11:04 AM
I have used 2 loops except the testcase loop, and it is showing me TLE... (:

I am getting this answer for

damangarg001 @ 3 May 2012 11:06 AM
I am getting this answer for 10^9 2 249999999509261247... can someone tell is this is correct???

awesome..even for

codeshef_coder @ 3 May 2012 12:04 PM
awesome..even for O(testcases) it is giving TLE

i applied O(t*n*2) still

altaf1148 @ 3 May 2012 04:32 PM
i applied O(t*n*2) still getting TLE ......

Anybody aware of any tricky

prakash1529 @ 3 May 2012 06:32 PM
Anybody aware of any tricky test case ?

hey how to check moi code

altaf1148 @ 3 May 2012 09:37 PM
hey how to check moi code give me some worst test cases .... i applied O(test cases)

i applied test case

altaf1148 @ 3 May 2012 09:48 PM
i applied test case 100000 10^9 2 and that 100000 times got 249999999500000000 100000 times execution time is 16.2 sec for the whole test case (i.e 100000) values ... is this correct ... @admin

can any one tell me what does

altaf1148 @ 3 May 2012 09:57 PM
can any one tell me what does time limit means ; does this mean that all test cases should pass within a given time ; or 1 test case should pass ???

@altaf1148 : Each test file

flying_ant @ 3 May 2012 10:17 PM
@altaf1148 : Each test file should run in the time limit specified. A test file can contain at most T test cases and this T, number of test cases, is given in input constraints. So T test cases should run in the specified time limit. In this case, 100000 test cases should run within 1s.

@flying_ant i applied O(T)

altaf1148 @ 3 May 2012 11:26 PM
@flying_ant i applied O(T) and still i m getting TLE .......

@altal - Pls before posting

Illusion03 @ 4 May 2012 12:11 AM
@altal - Pls before posting have a look at this. http://www.codechef.com/wiki/faq#Why_do_I_get_a_Time_Limit_Exceeded

http://www.codechef.com/wiki/

Illusion03 @ 4 May 2012 12:13 AM
http://www.codechef.com/wiki/faq#How_does_the_time_limit_work

@admin, I gave in the code

santano1988 @ 4 May 2012 12:26 AM
@admin, I gave in the code for C# but got a TLE error with O(1), but the exact same code got accepted in C. Is there something wrong in using C#.

i am getting 1444150 for

dmdmroy7 @ 4 May 2012 02:17 AM
i am getting 1444150 for N=3400 and M =4...wanted to know whether is it correct or not?

please provide some more test

dmdmroy7 @ 4 May 2012 02:21 AM
please provide some more test cases! ?:

My code is working for 0,1

vladamg98 @ 4 May 2012 01:23 PM
My code is working for 0,1 sec for 100000 1000000000 1 (and thaat 100000 times) and here is TLE =.= Can somebody see my last code, what's wrong?? http://www.codechef.com/viewsolution/1011433 ??

I am using o(1) and still

anubhavkkr @ 4 May 2012 03:20 PM
I am using o(1) and still getting TLE.Can you please tell me the input format????

getting TLE ...used long

supercooluku @ 4 May 2012 11:59 PM
getting TLE ...used long long....still getting TLE.... can sum1 put some critical test cases

i am getting runtime

aadisriram @ 5 May 2012 09:27 AM
i am getting runtime exception ! works perfect on local system ! can i please have the error code ?!

My code may or may not be

xachit @ 5 May 2012 06:43 PM
My code may or may not be correct but runs in O(1) and still I get TLE ??

getting TLE in O(1)

ridder @ 5 May 2012 09:13 PM
getting TLE in O(1) implementation...:(

can anyone tell the answer

ridder @ 5 May 2012 09:19 PM
can anyone tell the answer for N=10^9 and M=3 ??

@ridder I get for n = 10^9

ricola86 @ 5 May 2012 10:12 PM
@ridder I get for n = 10^9 and m=3 the answer: 166666666500000000.

I'm getting

gskhirtladze @ 6 May 2012 11:37 AM
I'm getting 166666666500000000 for n=10^9 and m=3. but my solution is still getting WA. for n=123456789 and m=11.my program printed : 692799022876224. is it correct answer?

my program is working for all

gauravrana981 @ 6 May 2012 01:32 PM
my program is working for all the cases discussed here but still getting WA. plz someone post more tricky test cases. thanks.

finally solved it.....had to

swaprocks123 @ 6 May 2012 03:07 PM
finally solved it.....had to thought about more than10 types of inputs....dont give up......

@ricola86-thanks, i guess

ridder @ 6 May 2012 05:30 PM
@ricola86-thanks, i guess your answer is correct......getting TLE though, my code runs under a second on my system.

@admin: plz reply to queries

jainharshit @ 6 May 2012 07:18 PM
@admin: plz reply to queries from people... !!

Finally got it, well happy.

ricola86 @ 6 May 2012 07:27 PM
Finally got it, well happy. My code wasn't working when n==m.

My code is working on all

vladamg98 @ 6 May 2012 09:44 PM
My code is working on all test cases, it's O(1), its working on ideone.com on example 100000 100000000 2 and that 100000 times for 0,1 sec and it's always getting TLE =.= I really don' know what's wrong, how can i reduce time?????

@vladamg98: if you are coding

gskhirtladze @ 6 May 2012 10:03 PM
@vladamg98: if you are coding on c++,try printf and scanf instead of cin and cout.

finally did it !!!

ridder @ 6 May 2012 10:22 PM
finally did it !!!

@Yogendra Mishra. thank you

gskhirtladze @ 7 May 2012 01:29 AM
@Yogendra Mishra. thank you very much, that helped me,but I'm still getting WA :(.

hah, finally got AC !!! :)

gskhirtladze @ 7 May 2012 01:36 AM
hah, finally got AC !!! :)

@gskhirtladze : what was your

rohanramanath @ 7 May 2012 02:15 AM
@gskhirtladze : what was your error ? I am still stuck with TLE :(

still getting WA :( have

amrendrakumar @ 7 May 2012 02:27 AM
still getting WA :( have passed all the test cases mentioned above :P

please give correct answer

shashwat001 @ 7 May 2012 03:15 AM
please give correct answer for any higher value of m and n.

alaS! i Got AC!!!

bodmas @ 7 May 2012 07:26 AM
alaS! i Got AC!!!

@gskhirtladze yes i'm using

vladamg98 @ 7 May 2012 12:25 PM
@gskhirtladze yes i'm using scanf/printf, long longs and i have no idea how O(T) can be TLE!!?

@ambuj - Yes.

Illusion03 @ 7 May 2012 07:12 PM
@ambuj - Yes.

is the largest answer

amrendrakumar @ 7 May 2012 11:49 PM
is the largest answer possible be accomodated in unsigned long long ??

finally finally got accepted

shashwat001 @ 8 May 2012 04:21 AM
finally finally got accepted after spending 7-8 hours. @ Yogendra Kumar: thank you for cases, my answer was wrong for second case.

@amrendrakumar ya it is

prashantcare @ 8 May 2012 10:22 AM
@amrendrakumar ya it is possible....:P:P

finally got CA......:))))

prashantcare @ 8 May 2012 10:23 AM
finally got CA......:))))

Finally got Accepted after 18

abhipr1 @ 8 May 2012 12:24 PM
Finally got Accepted after 18 WA....:)

@thana - No.

Illusion03 @ 8 May 2012 04:32 PM
@thana - No.

@Illusion03 correct ans

thana @ 8 May 2012 04:57 PM
@Illusion03 correct ans please........

i hv tried all the above

gauravrana981 @ 8 May 2012 05:01 PM
i hv tried all the above cases and they are perfect... but still getting WA. help me out plz.

@Thana - Read the above

Illusion03 @ 8 May 2012 07:57 PM
@Thana - Read the above comments for the correct ans.

Same here. Each and every one

frizcount @ 8 May 2012 10:23 PM
Same here. Each and every one of the above test cases match in my solution. Can someone help by giving a corner test case?

@Yogendra Mishra : You can

admin @ 8 May 2012 10:43 PM

@Yogendra Mishra : You can not share the answers during a live contest. This may lead to disqualification

@All: Please do not share the

admin @ 8 May 2012 10:46 PM

@All: Please do not share the answers/hints during a live contest. This may lead to disqualification.

@ADMIN Sorry for

Yogendra Mishra @ 9 May 2012 11:36 PM
@ADMIN Sorry for that!!!!!!!!!!!!

grrr my brute force and O(1)

shredder @ 10 May 2012 01:55 PM
grrr my brute force and O(1) algo give the same outputs. is %lld fine? %l64d gives TLE.

For N=10^9 and M=2, is the

rushilpaul @ 11 May 2012 12:00 AM
For N=10^9 and M=2, is the answer 249999999500000000 ?

SUCCESSFUL SUBMISSIONS


Fetching successful submissions

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.