Registered User - 1 year ago 51

C++ Question

I have a code like

`for(...)`

{

K=K*X; //X is some equation

answer=answer+K%C;

}

The variable

`answer`

`K%C`

`K`

`K=K%C`

How could I control value of K while not getting wrong results either?

Answer Source

You can exploit the identity

`ab mod n = (a mod n)(b mod n) mod n`

And replace `K = K * X;`

with `K = ((K % C) * (X % C)) % C;`

Then `answer = answer + K % C;`

becomes `answer += K;`