Advertisement
bartekltg

modulo

Mar 23rd, 2017 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. typedef long long int lli ;
  2.  
  3. long long int modular_inv(long long int a, long long int N ) ;
  4.  
  5.  
  6. class stoper
  7. {public:
  8.     chrono::time_point<chrono::high_resolution_clock> a,b;
  9.     void start(){a = chrono::high_resolution_clock::now();}
  10.     void stop() {b = chrono::high_resolution_clock::now();}
  11.     double val()
  12.     {
  13.         chrono::duration<double> elapsed_seconds = b-a;
  14.         return elapsed_seconds.count();
  15.     }
  16.     void show()
  17.     {
  18.         chrono::duration<double> elapsed_seconds = b-a;
  19.         cout << elapsed_seconds.count()<<"s ";
  20.     }
  21. };
  22.  
  23.  
  24. int ile_dzielnikow(int x)
  25. {
  26.  
  27.     int licznik=1;
  28.     for (int i=2;i<=x;i++)
  29.     {
  30.         int w=0;
  31.         while (x%i==0)
  32.         {
  33.             x=x/i;
  34.             w++;
  35.         }
  36.         licznik*=(1+w);
  37.     }
  38.     if (x>1) licznik*=2;
  39.     return licznik;
  40. }
  41.  
  42.  
  43.  
  44.  
  45. class dynmodulo
  46. {public:
  47.     unsigned int value;
  48.     unsigned int n;
  49.     dynmodulo(unsigned long long int x, int nn){ n=nn;value = x%n;}
  50.     dynmodulo(long long int x, int nn){n=nn; if (x>0)       value = x%n;    else        value = n-(-x%n); }
  51.     dynmodulo(int  x, int nn){n=nn; if (x>0)        value = x%n;    else        value = n-(-x%n); }
  52.     dynmodulo(unsigned int  x, int nn){n=nn; value = x%n;}
  53.     dynmodulo(){ value = 0;n=0;}
  54.     dynmodulo operator + (dynmodulo a) { return dynmodulo(value+a.value,n); }
  55.     dynmodulo operator - (dynmodulo a) { return dynmodulo(value-a.value,n); }
  56.     dynmodulo operator * (dynmodulo a) { return dynmodulo(value*(unsigned long long int) a.value,n); }
  57.     friend std::ostream& operator<< (std::ostream& stream, const dynmodulo a) { return stream<<a.value<<" (mod"<<a.n<<")"; }
  58.     friend std::istream & operator>> (std::istream & stream,  dynmodulo &a) { int t; stream>>t; a=dynmodulo(t,a.n);   return stream; }
  59.     dynmodulo inv() { return dynmodulo(modular_inv(value,n),n) ; }
  60.     dynmodulo operator / (dynmodulo a) { return (*this)*a.inv(); }
  61. };
  62.  
  63. template < int n>
  64. class modulo
  65. {public:
  66.     unsigned int value;
  67.     modulo(unsigned long long int x){ value = x%n;}
  68.     modulo(long long int x){ if (x>=0)      value = x%n;    else        value = n-(-x%n); }
  69.     modulo(int  x){ if (x>=0)       value = x%n;    else        value = n-(-x%n); }
  70.     modulo(unsigned int  x){ value = x%n;}
  71.     modulo(){ value = 0;}
  72.     modulo operator + (modulo a) { return modulo(value+a.value); }
  73.     modulo operator - (modulo a) { return modulo(value-a.value); }
  74.     modulo operator * (modulo a) { return modulo(value*(unsigned long long int) a.value); }
  75.     bool operator == (const modulo &a) { return value==a.value; }
  76.     template <class T>
  77.     bool operator == (const T &a) { return a==value; }
  78.     friend std::ostream& operator<< (std::ostream& stream, const modulo a) { return stream<<a.value<<" (mod"<<n<<")"; }
  79.     friend std::istream & operator>> (std::istream & stream,  modulo &a) { int t; stream>>t; a=modulo(t);   return stream; }
  80.     modulo inv() { return modulo(modular_inv(value,n)) ; }
  81.     modulo operator / (modulo a) { return (*this)*a.inv(); }
  82. };
  83.  
  84.  
  85.  
  86. template <class T>
  87. T power(T a, int ex)
  88. {
  89.     T r; // for NRVO
  90.     if (ex==0)
  91.     {
  92.         T r = 1;
  93.         return r;
  94.     }
  95.     else
  96.     {
  97.         if (ex%2==1) return a*power(a,ex-1);
  98.         else
  99.         {
  100.             T t=power(a,ex/2);
  101.             return t*t;
  102.         }
  103.     }
  104. }
  105.  
  106. template <class T>
  107. inline T sqr(const T x )
  108. {
  109.     return x*x;
  110. }
  111.  
  112. template <class T>
  113. T modpower(T const a, const int ex,T const m)
  114. {
  115.     if (ex==0) return T(1);
  116.     else
  117.     {
  118.         if (ex%2==1)
  119.         {
  120.             //T t=modpower(a,ex-1,m);
  121.             /*if ((t<0)||(t>=m))
  122.             {
  123.                 t++;t--;
  124.             }*/
  125.  
  126.             //return (a*t)%m;
  127.             return (a*modpower(a,ex-1,m))%m;
  128.         }
  129.         else
  130.         {
  131.             //T t=modpower(a,ex/2,m);
  132.            // if ((t<0)||(t>=m))
  133.            // {
  134.            //     t++;t--;
  135.            // }
  136.             //return (t*t)%m;
  137.             return sqr(modpower(a,ex/2,m))%m;
  138.         }
  139.     }
  140. }
  141.  
  142.  
  143. /*
  144. template <class T>
  145. inline T sqr(const T x ){
  146.     return x*x;
  147. }
  148. template <class T>
  149. T modpower(T const a, const int ex,T const m)
  150. {
  151.     if (ex==0) return T(1);
  152.     else    {
  153.         if (ex%2==1) {
  154.             return (a*modpower(a,ex-1,m))%m;
  155.         } else        {
  156.             return sqr(modpower(a,ex/2,m))%m;
  157.         }
  158.     }
  159. }
  160. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement