Advertisement
Guest User

Untitled

a guest
Sep 16th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. ll gcd(ll a, ll b) {
  2.     if (!b) return a;
  3.     return gcd(b, a % b);
  4. }
  5.  
  6. ll lca(ll a, ll b) {
  7.     return a * b / gcd(a, b);
  8. }
  9.  
  10. class PyramidSequences {
  11. public:
  12.     static long long distinctPairs(int n, int m) {
  13.         ll N = (ll)(n - 1), M = (ll)(m - 1);
  14.         if (N < M) swap(N, M);
  15.         ll X = lca(N, M);
  16.         ll Y = X / M;
  17.         ll ans = Y * M + 1;
  18.             ll Z = (N + M - 1) / M;
  19.             int pos = 0;
  20.             for (int i = 0; Y > 0; i++) {
  21.                 bool fl = !((N - pos) / M >= Z - 1);
  22.                 ans -= i * min(Y, Z - (int)fl);
  23.                     Y -= Z - (int)fl;
  24.                     pos = (pos + (Z - (int)fl) * M) % N;
  25.             }
  26.             return ans;
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement