Mr IDC and his Love
Valentine's Day is coming and our friend Mr.IDC has fallen in love with a girl. He just could not control his feelings and went to propose her.
When he proposed her the girl gave him a challenge and asked him to solve it and the condition was if he will solve the challenge successfully then only she will accept his proposal.
Since the girl likes mathematics very much so she asked him to calculate the sum of numbers from 1 to N as:
for ( i=1;i<=N;i++ ) sum += i % MOD;
( Since the girl is also in love with him so she does not want him to waste his time on large numbers, therefore she is gave him the MOD value also to reduce his calculations. )
Now she wants him to calculate the sum using the above definition. This sum will also prove how much he love her. ;)
First Line of input contains T, the number of testcases.
The next T lines will contain two integers N and MOD.
For each testcase print the output in a new line.
- 1 <= T <= 1000
- 1 <= N <= 109
- 1 <= MOD <= 109
Input: 3 20 5 10 2 5 3 Output: 40 5 6
For N = 20 m = 5,
Sum = 1%5 + 2%5 + 3%5 + 4%5 + 5%5 + 6%5 + 7%5 + 8%5 + 9%5 + 10%5 + 11%5 + 12%5 + 13%5 + 14%5 + 15%5 + 16%5 + 17%5 + 18%5 + 19%5 + 20%5 = 40
You can check yourself for testcase 2 and 3.
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.