Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #include <memory.h>
- using namespace std;
- typedef long long ll;
- ll gcd(ll a, ll b) {
- if (a < b) swap(a, b);
- while (b) {
- a %= b;
- swap(a, b);
- }
- return a;
- }
- ll lcm(ll a, ll b) {
- return a / gcd(a, b) * b;
- }
- int n, k, x, y, a[888][888], p[888], cc, tt;
- ll ans, cur;
- bool w[888];
- int main() {
- freopen("doubledealing.in", "r", stdin);
- freopen("doubledealing.out", "w", stdout);
- tt = 0;
- while (1) {
- scanf("%d%d", &n, &k);
- if (!n) break;
- if (n <= k) {
- printf("1\n");
- continue;
- }
- x = 1;
- y = 1;
- for (int i = 1; i <= n; ++i) {
- a[x][y++] = i;
- if (y > k) {
- ++x;
- y = 1;
- }
- }
- y = k;
- x = 1;
- cc = n;
- while (y)
- if (!a[x][y] || a[x][y] > n) {
- --y;
- x = 1;
- } else p[a[x++][y]] = cc--;
- ans = 1;
- memset(w, 1, sizeof(w));
- for (int i = 1; i <= n; ++i)
- if (w[i]) {
- cur = 1;
- x = p[i];
- w[i] = false;
- while (w[x]) {
- w[x] = false;
- ++cur;
- x = p[x];
- }
- ans = lcm(ans, cur);
- }
- printf("%lld\n", ans);
- x = 1;
- for (int i = 1; i <= n; ++i) {
- a[x][y++] = 0;
- if (y > k) {
- ++x;
- y = 1;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement