找每一位的循环节。求lcm

Double Dealing

Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1806    Accepted Submission(s): 622

Problem Description
Take a deck of n unique cards. Deal the entire deck out to k players in the usual way: the top card to player 1, the next to player 2, the kth to player k, the k+1st to
player 1, and so on. Then pick up the cards – place player 1′s cards on top, then player 2, and so on, so that player k’s cards are on the bottom. Each player’s cards are in reverse order – the last card that they were dealt is on the top,
and the first on the bottom.
How many times, including the first, must this process be repeated before the deck is back in its original order?

 

Input
There will be multiple test cases in the input. Each case will consist of a single line with two integers, n and k (1≤n≤800, 1≤k≤800). The input will end with a line with two 0s.
 

Output
For each test case in the input, print a single integer, indicating the number of deals required to return the deck to its original order. Output each integer on its own line, with no extra spaces, and no blank lines between answers. All possible inputs yield
answers which will fit in a signed 64-bit integer.
 

Sample Input

1 3
10 3
52 4
0 0
 

Sample Output

1
4
13
 

Source
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; int n,m; typedef long long int LL; int next[880],to[880];
bool vis[880]; LL gcd(LL a,LL b)
{
if(b==0) return a;
return gcd(b,a%b);
} LL lcm(LL a,LL b)
{
return a/gcd(a,b)*b;
} int get_int()
{
char ch;
int ret=0;
while(ch=getchar())
{
if(ch>='0'&&ch<='9')
{
ret=ret*10+ch-'0';
}
else break;
}
return ret;
} int main()
{
while(true)
{
n=get_int();m=get_int();
if(n==0&&m==0) break;
if(n<=m)
{
puts("1"); continue;
}
///mo ni yi chi
for(int i=1;i<=n;i++)
next[i]=i;
for(int i=1;i<=m;i++)
{
to[i]=n/m;
if(i<=n%m) to[i]++;
to[i]+=to[i-1];
}
for(int i=1;i<=n;i++)
{
next[i]=to[(i-1)%m+1]--;
}
LL ans=1;
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
int t=next[i];
LL temp=1;
while(t!=i)
{
vis[t]=true;
t=next[t];
temp++;
}
ans=lcm(ans,temp);
}
printf("%I64d\n",ans);
}
return 0;
}

最新文章

  1. 在JaveWeb项目中配置Spring 匿名访问时,匹配规则的变相实现/*
  2. MFC学习随笔(2)
  3. 获取spring容器要小心的坑
  4. nginx location语法使用说明
  5. MySQL的基本命令
  6. Atom编辑器入门到精通(一) 安装及使用基础
  7. iPhone开发:Objective C 代码规范-iOS总结版
  8. JavaScript类数组对象参考
  9. 018 关联映射文件中&lt;class&gt;标签中的lazy(懒加载)属性
  10. Spring 高级依赖注入方式
  11. 爬虫 requests模块的其他用法 抽屉网线程池回调爬取+保存实例,gihub登陆实例
  12. docker 系列 - Java程序制作Docker Image推荐方案(转载)
  13. foreach 语句
  14. Confluence 6 配置边栏
  15. PowerDesigner设置表字段唯一约束
  16. 1768:最大子矩阵(NOIP2014初赛最后一题)
  17. CentOS安装、配置RabbitMQ
  18. ldd pvs dis on solaris 10
  19. leetcode763
  20. http协议的各类状态码

热门文章

  1. Node.js——流的下载
  2. 乐视max2 刷入第三方recovery 然后刷入root 包 root
  3. codevs 1082 线段树练习 3 --分块练习
  4. day21-5 类的多态与多态性
  5. 13C++异常处理
  6. python嵌套列表
  7. mysql cluster配置
  8. Python:安装3.6
  9. buf.swap32()
  10. buf.toString()