0

Say I'm given 100 digit number (input can be string) . I want to find the value of that number % mod , where mod is 10^9+7. How can I go about getting the answer. I have already implemented the addition and subtraction functions on large numbers using string manipulation.

Thanks in advance !

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
anirudh_raja
  • 369
  • 2
  • 9
  • 20

1 Answers1

2

109 is -7 modulo 1000000007.

Break the 100 digit number N into blocks of 9 digits (a0, a1, ...) starting from the right. We have:

N = a0 + a1*10^9 + a2*10^18 + ...

The modulus is a0 - 7*a1 + 49*a2 - ...

For example, suppose you are to calculate 87564875326485487234854862386245865486238654862 modulo 1000000007. You break this string of digits into blocks of 9 starting from the right:

  • a0 = 238654862
  • a1 = 245865486
  • a2 = 245865486
  • a3 = 854862386
  • a4 = 485487234
  • a5 = 564875326
  • a6 = 87

Now you convert each block into an integer and calculate m = a0 - 7*a1 + 49*a2 - 323*a3 +... (hint: use 64 bit integers).

Now N = m (mod 1000000007).

Joni
  • 108,737
  • 14
  • 143
  • 193