Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/STACK:268435456")
- #define _CRT_SECURE_NO_WARNINGS
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <vector>
- #include <set>
- #include <map>
- #include <deque>
- #include <queue>
- #include <list>
- #include <cstring>
- #include <complex>
- #include <ctime>
- #include <bitset>
- #include <iomanip>
- #include <sstream>
- using namespace std;
- const double PI = 3.1415926535897932384626433832795;
- template<class T> T min(T &a, T &b) { return (a<b) ? a : b; }
- template<class T> T max(T &a, T &b) { return (a>b) ? a : b; }
- template<class T> T sqr(T &a) { return a*a; }
- template<class T> T abs(T &a) { return (a<0) ? (-a) : a; }
- typedef long long ll;
- typedef long long LL;
- typedef pair<int,int> ii;
- #define all(v) (v).begin(),(v).end()
- #define sz(v) ((int)((v).size()))
- #define PB push_back
- #define MP make_pair
- #define CLR(a) memset((a),0,sizeof(a))
- #define fori(i,n) for(int i=0;i<(n);i++)
- //------------------------------------------------------------------------------
- bool used[805];
- ll gcd(ll a, ll b) {
- return b == 0 ? a : gcd(b, a%b);
- }
- int p[805];
- int main()
- {
- #ifdef _MSC_VER
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- freopen("doubledealing.in", "r", stdin);
- freopen("doubledealing.out", "w", stdout);
- #endif
- int n, k;
- while (true) {
- scanf("%d%d", &n, &k);
- if (n == 0 && k == 0) break;
- /*vector<vector<int> > a(k, vector<int>());
- for (int i = 0; i < n; i++) {
- a[i%k].PB(i);
- }
- vector<int> p;
- for (int i = 0; i < k; i++) {
- for (int j = sz(a[i])-1; j >= 0; j--) {
- p.PB(a[i][j]);
- }
- }*/
- int len = 0;
- int nn = n/k*k;
- for (int i = 0; i < k; i++) {
- int last = nn + i;
- if (last >= n) last -= k;
- for (int j = last; j >= 0; j -= k) {
- p[len++] = j;
- }
- }
- ll ans = 1;
- CLR(used);
- for (int i = 0; i < n; i++) {
- if (!used[i]) {
- int x = 1;
- used[i] = true;
- int k = p[i];
- while (true) {
- if (k == i) break;
- x++;
- used[k] = true;
- k = p[k];
- }
- ans = ans / gcd(ans,x) * x;
- }
- }
- printf("%lld\n", ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement