artemgf

Городские кварталы

Apr 29th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 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. #include <sstream>
  10. #include <queue>
  11.  
  12. using namespace std;
  13.  
  14. long long NOD(long long a, long long b) {
  15.     long long nod = 1L;
  16.     long long tmp;
  17.     if (a == 0L)
  18.         return b;
  19.     if (b == 0L)
  20.         return a;
  21.     if (a == b)
  22.         return a;
  23.     if (a == 1L || b == 1L)
  24.         return 1L;
  25.     while (a != 0 && b != 0) {
  26.         if (((a & 1L) | (b & 1L)) == 0L) {
  27.             nod <<= 1L;
  28.             a >>= 1L;
  29.             b >>= 1L;
  30.             continue;
  31.         }
  32.         if (((a & 1L) == 0L) && (b & 1L)) {
  33.             a >>= 1L;
  34.             continue;
  35.         }
  36.         if ((a & 1L) && ((b & 1L) == 0L)) {
  37.             b >>= 1L;
  38.             continue;
  39.         }
  40.         if (a > b) {
  41.             tmp = a;
  42.             a = b;
  43.             b = tmp;
  44.         }
  45.         tmp = a;
  46.         a = (b - a) >> 1L;
  47.         b = tmp;
  48.     }
  49.     if (a == 0)
  50.         return nod * b;
  51.     else
  52.         return nod * a;
  53. }
  54.  
  55. int main()
  56. {
  57.     int n, m;
  58.  
  59.     cin >> n >> m;
  60.  
  61.     cout << n-1 + m-1 - NOD(m-1, n-1);
  62.    
  63.     _getch();
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment