diff --git a/numbers.cc b/numbers.cc index 4a829ad..4d954dc 100644 --- a/numbers.cc +++ b/numbers.cc @@ -117,6 +117,30 @@ return y; } +template +N powmod(const N &x, N i, const N &m) +{ +if(i==0) return 1; +if(i==1) return x%m; +N y,z; +z=x%m; +while(!(i&1)) + { + z = (z*z)%m; + i >>= 1; + } +y=z; +while((i >>= 1)/*!=0*/) + { + z = (z*z)%m; + if(i&1) y = (y*z)%m; + } +return y; +} + + + + //force instantization @@ -126,6 +150,7 @@ template FACTORIZATION factorization(const N &x); \ template N nextprime(N x); \ template std::ostream & operator<<(std::ostream &s, const FACTORIZATION &x); \ template N pow(const N &x, N i); \ +template N powmod(const N &x, N i, const N &m); \ template N eulerphi(const FACTORIZATION &f); \ diff --git a/numbers.h b/numbers.h index bc70856..c7249e8 100644 --- a/numbers.h +++ b/numbers.h @@ -62,6 +62,9 @@ N eulerphi(const N &x) {return eulerphi(factorization(x));} template N pow(const N &x, N i); +template +N powmod(const N &x, N i, const N &m); + }//namespace #endif