• Loading...

Problem 12: Modulus

Points:

Small Case: 5 Answer: 99999307

Large Case: 10 Answer: 80397263

Given the values of M and N, find (0 mod M + N mod M + 2N mod M +...+ (M-1)N mod M) mod 100000007.

Small Case: M=200000 ,N=300000
Large case: M=4000000000000 ,N=6000000000


Solution:

For given values of M and N, equation:
(0 mod M + N mod M + 2N mod M +...+ (M-1)N mod M)
consists of d copies of M/d numbers
0 + d + 2d + 3d + ...+ (m-d)
where d=GCD(M,N).

Reference: R. L. Graham, D. E. Knuth, O. Patashnik, Concrete Mathematics 1990, Page 129


Code:


#include <stdio.h>
#define mod 100000007

long long GCD(long long a, long long b)
{
  if(a%b==0)
    return b;
  return GCD(b,a%b);
}

int main()
{
  long long m,n,res=0,i;
  scanf("%lld%lld",&m,&n);
  
  long long d = GCD(m,n);
  long long k = m/d;
  for(i=0;i<k;i++)
    res = (res + ((i*d)%mod))%mod;
  printf("%lld\n",(d*res)%mod);
}

Submit your solution

Submission to this bonus problem has been closed.

discussions


bbbasicWrong statement?
Q:Is the statement surely correct? It seems that the answer is 0 whenever N is even.
A:yes, the statement is correct

post a question

Loading...

Problem 1
Problem 2
Problem 3
Problem 4
Problem 5 (bonus) Expired
Problem 1
Problem 2
Problem 3
Problem 4 (bonus) Expired
Top Scorers
rng_58 235
Anton_Lunyov 225
tourist 190
Silence 180
cedricl2 175
cgy4ever 175
DrKorbin 165
Koen 155
bbbasic 150
lunae_2 150
Submissions
Problem 1
10 pt
not submitted
98
submissions

19
correct (19 %)



25 pt
not submitted
2
submissions

1
correct (50 %)


Problem 2
5 pt
not submitted
95
submissions

38
correct (40 %)



10 pt
not submitted
19
submissions

15
correct (78 %)


Problem 3
5 pt
not submitted
153
submissions

117
correct (76 %)



15 pt
not submitted
44
submissions

25
correct (56 %)


Problem 4
10 pt
not submitted
152
submissions

54
correct (35 %)



15 pt
not submitted
23
submissions

10
correct (43 %)


Problem 5
10 pt
not submitted
36
submissions

28
correct (77 %)



20 pt
not submitted
6
submissions

2
correct (33 %)


Problem 6
5 pt
not submitted
41
submissions

29
correct (70 %)



10 pt
not submitted
15
submissions

14
correct (93 %)


Problem 7
5 pt
not submitted
38
submissions

17
correct (44 %)



10 pt
not submitted
16
submissions

15
correct (93 %)


Problem 8
5 pt
not submitted
127
submissions

104
correct (81 %)



15 pt
not submitted
47
submissions

34
correct (72 %)


Problem 9
5 pt
not submitted
86
submissions

35
correct (40 %)



15 pt
not submitted
16
submissions

15
correct (93 %)


Problem 10
5 pt
not submitted
45
submissions

23
correct (51 %)



15 pt
not submitted
15
submissions

14
correct (93 %)


Problem 11
5 pt
not submitted
81
submissions

27
correct (33 %)



15 pt
not submitted
8
submissions

3
correct (37 %)


Problem 12
5 pt
not submitted
104
submissions

88
correct (84 %)



10 pt
not submitted
31
submissions

21
correct (67 %)