Registered User - 10 months ago 41

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

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;`

Source (Stackoverflow)