找每一位的循环节。求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
The University of Chicago Invitational Programming Contest 2012
#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 chifor(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;
}

转载于:https://www.cnblogs.com/zhchoutai/p/8525481.html

HDOJ 4259 Double Dealing相关推荐

  1. HDU 4259 Double Dealing【简单群置换】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4259 题目大意:给出n张卡片以及k个人,现在对卡片进行分堆,然后分发(这样卡片改变了顺序),依次这样问 ...

  2. HDU4259(简单群置换)

    题目:Double Dealing #include <iostream> #include <string.h> #define LL long long const int ...

  3. 【HDOJ】4579 Random Walk

    1. 题目描述 一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路 显然是个DP.公式推导也相当容易.不妨设$ ...

  4. 【hdoj】3007 Buried memory 【计算几何--最小圆覆盖】

    传送门:Buried memory 苍天饶过谁,第三次在hdoj上 交计算几何的题了,没一次是AC的. ┭┮﹏┭┮都是模板题啊,我都是抄板子的啊,为什么会这样,我怎么这么菜. 题意: 求最小圆覆盖 的 ...

  5. 二分搜索 HDOJ 2289 Cup

    题目传送门 1 /* 2 二分搜索:枚举高度,计算体积与给出的比较. 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #in ...

  6. 双拓扑排序 HDOJ 5098 Smart Software Installer

    题目传送门 1 /* 2 双拓扑排序:抄的,以后来补 3 详细解释:http://blog.csdn.net/u012774187/article/details/40736995 4 */ 5 #i ...

  7. BZOJ 4259: 残缺的字符串 [FFT]

    4259: 残缺的字符串 题意:s,t,星号任意字符,匹配方案数 和上题一样 多乘上一个\(a_{j+i}\)就行了 #include <iostream> #include <cs ...

  8. 【HDOJ】【3853】LOOPS

    概率DP/数学期望 kuangbin总结中的第7题 其实跟UVA 11762 Race To 1 那道题差不多--直接推下公式,然后倒推即可 Trick:有的点可能是p1[i][j]==1--这样的点 ...

  9. Hdoj 2671 Can't be easier

    Hdoj 2671 Can't be easier 简单的数学问题: 如果两点异侧直接求距离,同侧则先求出其中一点的对称点后用距离公式 #include<iostream> #includ ...

最新文章

  1. 开发三年,如何摆脱日复一日的CRUD?
  2. python多个日志模块怎么隔离_在多个模块中使用Python日志logging
  3. 操作系统内存管理-Linux版
  4. spring_在Spring中使用多个动态缓存
  5. 黑马在线教育项目---5、使用填充器创建数据库数据
  6. matlab怎么在c 中调用,在C中调用Matlab (转)
  7. python三维数组怎么表示_python – numpy中的三维数组
  8. datatable如何生成级联数据_如何把Excel表数据批量生成条形码
  9. wxpython控件绑定事件_wxPython事件处理
  10. mtensor一个tensor计算库,支持cuda延迟计算
  11. java并发编程之HappenBefore
  12. html5+css3第一次作业_老娘我,饿死也不要陪孩子做手工作业,高考陪考真香!...
  13. 小白学习使用gitee问题产生汇总(持续更新)
  14. setfacl 权限导出_setfacl getfacl 将某个目录的访问权限赋值给某个用户
  15. 阿里巴巴设计大会PPT,出彩的设计让人惊叹
  16. 第1章 ADAMS/View 基础知识
  17. java在浏览器闪退_Edge浏览器闪退怎么办,Edge浏览器闪退如何修复
  18. html制作windows 10,Windows 10迎来Alpha版HTML编辑器应用
  19. 几何学五大公理_几何学的五个公理是什么?
  20. 超赞 不愧是美团内部的JVM学习手册,从头到尾全是精华

热门文章

  1. 计算机二级 监控,计算机二级VB辅导:用VB编写网络监控软件
  2. SpringBoot2.0学习 (一)
  3. avi格式如何转化成mp4
  4. 人工智能的几个小故事
  5. 对LOWORD, HIWORD, LOBYTE, HIBYTE的理解
  6. django数据库操作和中间件
  7. STM32变频器技术方案,源码+图纸,供参考设计与学习
  8. Web Service Case Study: 内容供应服务
  9. java怎么控制log文件大小,限制 Apache日志文件大小的方法
  10. xv6中pipedup