#include <iostream>
#include <string.h>
#define LL long long
const int N=1005;
int n,k;
int a[N][N];
bool f[N];
int num[N];
LL gcd(LL a,LL b)
{
return b? gcd(b,a%b):a;
}
int main()
{
int i,j;
while(std::cin>>n>>k)
{
if(n==0&&k==0) break;
if(n<k)
{
std::cout<<"1"<<std::endl;
continue;
}
int m=1;
for(i=1;i<=k&&i<=n;i++)
for(j=(n-i)/k*k+i;j>0;j-=k)
num[m++]=j;
int x;
LL res=1;
memset(f,false,sizeof(f));
for(i=1;i<=n;i++)
{
x=i;
if(f[i]) continue;
int count=0;
while(1)
{
f[x]=true;
x=num[x];
count++;
if(x==i) break;
}
res=res/gcd(res,count)*count;
}
std::cout<<res<<std::endl;
}
return 0;
}
标签:gcd,int,置换,HDU4259,num,long,简单,include,LL From: https://blog.51cto.com/u_16146153/6388606