ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1560. Elementary Symmetric Functions

How find 24^(-1) mod p
Posted by svr 22 Sep 2007 22:32
One method uses formula a4=(t1^4-6*t1^2*t2+3*t2^2+8*t1*t3-6*t4)/24 mod p
For it we must find previously 24^(-1) mod p
But it needs in loop with ~ p=10^9 iterations
May this calculation lead to TL before main program?
Re: How find 24^(-1) mod p
Posted by Vedernikoff Sergey 22 Sep 2007 22:38
Why can't we use extended euclid?
Re: How find 24^(-1) mod p
Posted by Ardian KP 23 Sep 2007 14:10
or use fermat little theorem

a^(p-1) = 1 (mod p)
a^(p-2).a = 1 (mod p)

so a^(-1) = a^(p-2) :)
Re: How find 24^(-1) mod p
Posted by Walrus (Dmitry Bochkarev) 30 Sep 2007 01:03
// O(a)
inline int inv(int a)
{
    for (int i = 1; ; i++)
    {
        int64 b = (int64) P * i + 1;
        if (b % a == 0)
            return int(b / a);
    }
    return 0;
}
Re: How find 24^(-1) mod p
Posted by svr 30 Sep 2007 09:39
Best advise was extended evclid
I could apply it during the context.
But 1560 wuild bettter to solve in Java using BigInt
We simply use /24 at the end and avoid many %p operations.
Re: How find 24^(-1) mod p
Posted by Denis Koshman 15 Aug 2008 16:16
a/b mod p = a*b^-1 mod p = a*b^(-1 mod p-1) mod p = a*b^(p-2) mod p. b^(p-2) can be precomputed using logarithmic powering. I needed only divisors 2, 6 and 24.