-2

I am asked to print all numbers modulus of $1000000007$.

My expression is $x*(1+f(x))/2$

For cases when x is even it is simple as i can do (x/2) first, then do:

$ ((x/2)*(1+f'(x)) modulus 1000000007 $

f'(x) is $f(x) modulus 1000000007$, f(x) is an exprssion wherein it has multiplications and additions already performed under modulus 1000000007 .

So for cases when x is odd if $((1+f'(x))modulus 1000000007)/2$ gives wrong answer.

  • Hint: for odd $a,n$ we have $\bmod n!: a/2 \equiv (a+n)/2,,$ and $a+n$ is even so the division is exact - see modular "division by $2$" here in the linked dupe. – Bill Dubuque May 13 '22 at 08:56
  • @BillDubuque I still can't follow , can you explain with an example, you can consider me a novice in modular arithmetics. –  May 13 '22 at 09:23
  • Lets say x=1000000011. As the division is perfect, So for (1+f(x)) to be divisible by two f(x) has to be odd. Now I dont know the actual value of f(x) since it has been taken modulus 1000000007 several times, so what comes out of f(x)%1000000007 is even, now what should I do? – –  May 13 '22 at 09:29
  • The link which you marked as dup, might be talking about same sunject matter but talks of a different function. So I dont know how to relate to solve my scenario. –  May 13 '22 at 09:31
  • Let $,a = x(1+f(x))\bmod n,$ then apply my first comment. See the linked dupe for specific examples. The point is that we can always choose an even number congruent to $a$, which makes it trivial to divide by $2,,$ as explained in the linked post. – Bill Dubuque May 13 '22 at 09:41
  • See also this more general post on solving linear congruences. Here you are solving $,2X\equiv a\pmod{n}\ $ – Bill Dubuque May 13 '22 at 09:49
  • @BillDubuque all these study material that you are linking are too overwhelming for a beginner to grasp at once. All I got is we need to calculate inverse in such cases to convert a division to multiplication. and in my case inverse of 1/2 seems to be (1+p)/2 ie (1+1000000007)/2=500000004 –  May 13 '22 at 11:08
  • Yes, that's the special case: $,a=1,\ n = p,$ odd, in my first comment. It is essential to learn basic modular arithmetic to understand matters like this - see any good textbook on elementary number theory. – Bill Dubuque May 13 '22 at 12:15

1 Answers1

0

The trick is that division by 2 is the same as multiplication by 1/2 and that 1/2 is just any number with the property that if you multiply that number by 2, you get 1.

So... if you can find any number $y$ such that $2y = 1 \mod 1000000007$ you can think about this number $y$ as 'being' (or playing the role of) 1/2 in the world modulo 1000000007.

And once you have $y$ you can think about division by 2 as multiplication by $y$.

Vincent
  • 10,614
  • Please strive not to post more (dupe) answers to dupes of FAQs, cf. recent site policy announcement here. – Bill Dubuque May 13 '22 at 08:59
  • Can you explain with an example lets say x=1000000011. As the division is perfect, So for (1+f(x)) to be divisible by two f(x) has to be odd. Now I dont know the actual value of f(x) since it has been taken modulus 1000000007 several times, so what comes out of f(x)%1000000007 is even, now what should I do? –  May 13 '22 at 09:28
  • here is an easier example. Suppose we take the modulus to be 9 instead of your huge number. The principle is the same, see also the linked quesion. You know that 2 * 5 = 10. But modulo 9 that means 2 * 5 = 1. So 5 serves as something that can play the role of 1/2 in this world. Now if we want to divide 7 by 2 modulo 9 what we can do is multiply 7 by 1/2, so by 5. We know that 7 * 5 = 35 = 8 mod 9 so my claim is that 7/2 = 8 mod 9. To check if that is true, all we need to do is see if 8 * 2 = 7 mod 9 and indeed this is the case! – Vincent May 13 '22 at 09:39
  • 1
    Similarly the 'abstract quantity' $f(x)/2 \mod 9$ must be the same as $5 f(x) \mod 9$ – Vincent May 13 '22 at 09:40
  • 4
    I shouldn't say I have fully managed to grasp what you said, but whatever example you gave coupled with my experimetation suggests for my case I have to replace (1/2) with (1000000007+1)/2. So my equation becomes x∗(1+f(x))*500000004... generally speaking under mod p, (p+1)/2 is the inverse of (1/2). Tell me if I am correct? –  May 13 '22 at 10:50
  • 1
    Yes, in as far I understand the problem you are working on, that is correct! – Vincent May 13 '22 at 15:02
  • 1
    Ehm except one thing in the formulation of the general case. (p + 1)/2 is the inverse of 2 in the mod p world, not the inverse of (1/2). I think we mean the same thing. Since (1/2) is the inverse of 2 in the real world, (p+1)/2 can be thought of as 'being' (or 'playing the role of') 1/2 in the mod p world – Vincent May 13 '22 at 15:05