Advertisement
agul

OpenCup :: Stage 8 - GP of America :: Problem C

May 31st, 2012
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <memory.h>
  4.  
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. ll gcd(ll a, ll b) {
  10.     if (a < b) swap(a, b);
  11.     while (b) {
  12.         a %= b;
  13.         swap(a, b);
  14.     }
  15.     return a;
  16. }
  17.  
  18. ll lcm(ll a, ll b) {
  19.     return a / gcd(a, b) * b;
  20. }
  21.  
  22. int n, k, x, y, a[888][888], p[888], cc, tt;
  23. ll ans, cur;
  24. bool w[888];
  25.  
  26. int main() {
  27.     freopen("doubledealing.in", "r", stdin);
  28.     freopen("doubledealing.out", "w", stdout);
  29.     tt = 0;
  30.     while (1) {
  31.         scanf("%d%d", &n, &k);
  32.         if (!n) break;
  33.         if (n <= k) {
  34.             printf("1\n");
  35.             continue;
  36.         }
  37.         x = 1;
  38.         y = 1;
  39.         for (int i = 1; i <= n; ++i) {
  40.             a[x][y++] = i;
  41.             if (y > k) {
  42.                 ++x;
  43.                 y = 1;
  44.             }
  45.         }
  46.         y = k;
  47.         x = 1;
  48.         cc = n;
  49.         while (y)
  50.             if (!a[x][y] || a[x][y] > n) {
  51.                 --y;
  52.                 x = 1;
  53.             } else p[a[x++][y]] = cc--;
  54.         ans = 1;
  55.         memset(w, 1, sizeof(w));
  56.         for (int i = 1; i <= n; ++i)
  57.             if (w[i]) {
  58.                 cur = 1;
  59.                 x = p[i];
  60.                 w[i] = false;
  61.                 while (w[x]) {
  62.                     w[x] = false;
  63.                     ++cur;
  64.                     x = p[x];
  65.                 }
  66.                 ans = lcm(ans, cur);
  67.             }
  68.         printf("%lld\n", ans);
  69.         x = 1;
  70.         for (int i = 1; i <= n; ++i) {
  71.             a[x][y++] = 0;
  72.             if (y > k) {
  73.                 ++x;
  74.                 y = 1;
  75.             }
  76.         }
  77.     }
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement