artemgf

НОД 2010

Apr 16th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. #include <math.h>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. long long prom3(long long a, long long b) {
  14.     long long nod = 1L;
  15.     long long tmp;
  16.     if (a == 0L)
  17.         return b;
  18.     if (b == 0L)
  19.         return a;
  20.     if (a == b)
  21.         return a;
  22.     if (a == 1L || b == 1L)
  23.         return 1L;
  24.     while (a != 0 && b != 0) {
  25.         if (((a & 1L) | (b & 1L)) == 0L) {
  26.             nod <<= 1L;
  27.             a >>= 1L;
  28.             b >>= 1L;
  29.             continue;
  30.         }
  31.         if (((a & 1L) == 0L) && (b & 1L)) {
  32.             a >>= 1L;
  33.             continue;
  34.         }
  35.         if ((a & 1L) && ((b & 1L) == 0L)) {
  36.             b >>= 1L;
  37.             continue;
  38.         }
  39.         if (a > b) {
  40.             tmp = a;
  41.             a = b;
  42.             b = tmp;
  43.         }
  44.         tmp = a;
  45.         a = (b - a) >> 1L;
  46.         b = tmp;
  47.     }
  48.     if (a == 0)
  49.         return nod * b;
  50.     else
  51.         return nod * a;
  52. }
  53.  
  54. long long NOD(map <long long, int> sc)
  55. {
  56.     auto prom1=sc.begin();
  57.     long long prom2 = prom1->first;
  58.     for (auto i = sc.begin(); i != sc.end(); i++)
  59.     {
  60.         prom2 = prom3(prom2, i->first);
  61.     }
  62.     return prom2;
  63. }
  64.  
  65. int main()
  66. {
  67.     int n, prom2 = 0;
  68.     map <long long, int> sc;
  69.     vector <int> answer;
  70.     cin >> n;
  71.  
  72.     for (int i = 1; i <= n; i++)
  73.     {
  74.         int prom;
  75.         string h;
  76.         cin >> h;
  77.         cin >> prom;
  78.         if (h == "+")
  79.         {
  80.             if (sc.find(prom) != sc.end())
  81.             {
  82.                 sc[prom]++;
  83.                 if (sc.size() != 0)
  84.                     cout << prom2 << endl;
  85.                 else
  86.                     cout << 1 << endl;
  87.             }
  88.             else
  89.             {
  90.                 sc.insert(pair <long long, int>(prom, 1));
  91.                 prom2 = prom3(prom2, prom);
  92.                 cout << prom2 << endl;
  93.             }
  94.         }
  95.         else
  96.             if (sc.find(prom) != sc.end())
  97.                 if (sc[prom] == 1)
  98.                 {
  99.                     sc.erase(prom);
  100.                     if (sc.size() != 0)
  101.                     {
  102.                         prom2 = NOD(sc);
  103.                         cout << prom2 << endl;
  104.                     }
  105.                     else
  106.                         cout << 1 << endl;
  107.                 }
  108.                 else
  109.                 {
  110.                     sc[prom]--;
  111.                     if (sc.size() != 0)
  112.                         cout << prom2 << endl;
  113.                     else
  114.                         cout << 1 << endl;
  115.                 }
  116.             else
  117.                 if (sc.size() != 0)
  118.                     cout << prom2 << endl;
  119.                 else
  120.                     cout << 1 << endl;
  121.     }
  122.  
  123.     /*if (answer.size() != 0)
  124.         for (auto i = answer.begin(); i != answer.end(); i++)
  125.             cout << *i << endl;
  126.     else
  127.         cout << 1;*/
  128.  
  129.     _getch();
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment