找每一位的循环节。求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;
}

HDOJ 4259 Double Dealing的更多相关文章

  1. hdu 4259 Double Dealing

    思路: 找每一个数的循环节,注意优化!! 每次找一个数的循环节时,记录其路径,下次对应的数就不用再找了…… 代码如下: #include<iostream> #include<cst ...

  2. HDU 4259 - Double Dealing(求循环节)

    首先将扑克牌进行一次置换,然后分解出所有的循环节,所有循环节的扑克牌个数的最小公倍数即为答案 #include <stdio.h> #include <string.h> #i ...

  3. HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))

    Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 4529 Double Dealing (置换群)

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

  5. JVM Specification 9th Edition (4) Chapter 4. The class File Format

    Chapter 4. The class File Format Table of Contents 4.1. The ClassFile Structure 4.2. Names 4.2.1. Bi ...

  6. 【HDOJ】1908 Double Queue

    双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...

  7. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  9. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

随机推荐

  1. H.264学习笔记1——相关概念

    此处记录学习AVC过程中的一些基本概念,不定时更新. frame:帧,相当于一幅图像,包含一个亮度矩阵和两个色度矩阵. field:场,一帧图像,通过隔行扫描得到奇偶两场,分别称为顶场和底场或奇场和偶 ...

  2. 手动将Excel数据导入SQL

    1. 右键点击数据库名称,Tasks --> Import Data... 2. Choose Data Source:MicroSoft Excel,选择Excel文件和Excel版本.Exc ...

  3. at, batch, atq, atrm - 排队、检查或删除以后要执行的作业

    总览 at [-V] [-q 队列] [-f 文件] [-mldbv] 时间 at -c 作业 [作业...] atq [-V] [-q 队列] [-v] atrm [-V] 作业 [作业...] b ...

  4. h5开发app,移动端 click 事件响应缓慢的解决方案

    造成点击缓慢的原因 从点击屏幕上的元素到触发元素的 click 事件,移动浏览器会有大约 300 毫秒的等待时间.为什么这么设计呢? 因为它想看看你是不是要进行双击(double tap)操作. 使用 ...

  5. jQuery动态移除和绑定事件

    function bindEvent() { //移除绑定事件 $('.btnsp').unbind('click'); //绑定事件 $('.btnsp').bind('click', functi ...

  6. hdu 1792 A New Change Problem(互质数之间最大不能组合数和不能组合数的个数)

    题意:求互质的m和n的最大不能组合数和不能组合数的个数 思路:m和n的最大不能组合数为m*n-m-n,不能组合数的个数为(m-1)*(n-1)/2 推导: 先讨论最大不能组合数 因为gcd(m,n)= ...

  7. 关于OpenMP的归约操作reduction

    这里提一个重要的点 像这样 ; void ff() { sum += 0.5; } //main() #pragma omp parallel for reduction(+:sum) ; i < ...

  8. Spring中注解注入bean和配置文件注入bean

    注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...

  9. Git 教程 -- 第一天

    什么是Git? Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. 为什么使用Git? 众所周知,版本控制系统分为集中式版本控制系统(SVN.CVS等)与分布式版 ...

  10. [Python3网络爬虫开发实战] 6.2-Ajax分析方法

    这里还以前面的微博为例,我们知道拖动刷新的内容由Ajax加载,而且页面的URL没有变化,那么应该到哪里去查看这些Ajax请求呢? 1. 查看请求 这里还需要借助浏览器的开发者工具,下面以Chrome浏 ...