Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. using namespace::std;
  4.                
  5.  
  6.  
  7.  
  8. void Input(int&a, int&p, int&y)
  9. {
  10.     cout << "Введите a,p,y: ";
  11.     cin >> a >> p >> y;
  12. }
  13.  
  14.  
  15. int calcK(int p)
  16. {
  17.     return (int)(sqrt(p)) + 1;
  18. }
  19.  
  20.  
  21.  
  22.  
  23. int ModPow(int base, int exp, int modulus)
  24. {
  25.   base %= modulus;
  26.   int result = 1;
  27.   while (exp > 0)
  28.   {
  29.     if (exp & 1) result = (result * base) % modulus;
  30.     base = (base * base) % modulus;
  31.     exp >>= 1;
  32.   }
  33.   return result;
  34. }
  35.  
  36. map <int, int> BuildFirstSeri(int y,int a,int m,int p)
  37. {
  38.     map<int, int> seri;
  39.     //map <int, int> otherseri;
  40.     for (int i = 0; i < m; i++)
  41.     {
  42.         /*otherseri.insert(pair<int, int>(i,(int)pow(a,i)*y%p));*/
  43.         seri.insert(pair<int, int>(i, y%p*ModPow(a, i, p)%p));
  44.     }
  45.     return seri;
  46. }
  47.  
  48. int getSolution (map<int,int> firstseri,int a, int k,int p)
  49. {
  50.     cout << "Second seri: ";
  51.     for (int i = 0; i < k; i++)
  52.     {
  53.         int tmpvalue=ModPow(a,(i+1)*k,p);
  54.  
  55.         cout << tmpvalue << " ";
  56.         for (auto x : firstseri)
  57.         {
  58.             if (x.second == tmpvalue)
  59.                 return k*(i+1)-x.first;
  60.         }
  61.        
  62.     }
  63.     return -1;
  64. }
  65.  
  66.  
  67. void PrintMap(map<int, int> mapToConsole)
  68. {
  69.     for (auto x : mapToConsole)
  70.         cout << x.second << " ";
  71.     cout << endl;
  72.  
  73. }
  74.  
  75. // y=a^xmodp
  76. int main()
  77. {
  78.     setlocale(LC_ALL, "Russian");
  79.     int y, a, p,k,m;
  80.     map<int, int> firstSeri;
  81.     Input(a,p,y);
  82.     k=m=calcK(p);
  83.     cout << "k:" << k << endl;
  84.     firstSeri = BuildFirstSeri(y, a, m,p);
  85.     cout << "First seri:";
  86.     PrintMap(firstSeri);
  87.     cout <<endl<< "solution: " << getSolution(firstSeri, a, k, p)<<endl;
  88.    
  89.    
  90.  
  91.     system("pause");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement