HDOJ 4259 Double Dealing
找每一位的循环节。求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
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?
answers which will fit in a signed 64-bit integer.
10 3
52 4
0 0
4
13
#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的更多相关文章
- hdu 4259 Double Dealing
思路: 找每一个数的循环节,注意优化!! 每次找一个数的循环节时,记录其路径,下次对应的数就不用再找了…… 代码如下: #include<iostream> #include<cst ...
- HDU 4259 - Double Dealing(求循环节)
首先将扑克牌进行一次置换,然后分解出所有的循环节,所有循环节的扑克牌个数的最小公倍数即为答案 #include <stdio.h> #include <string.h> #i ...
- 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) ...
- hdu 4529 Double Dealing (置换群)
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- 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 ...
- 【HDOJ】1908 Double Queue
双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ(2056)&HDOJ(1086)
Rectangles HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
随机推荐
- [Android]Android Design之Navigation Drawer
概述 在以前ActionBar是Android 4.0的独有的,后来的ActionBarSherlock的独步武林,对了还有SlidingMenu,但是这个可以对4.0下的可以做很好的适配.自从Goo ...
- 数据结构算法 - ConcurrentHashMap 源码解析
五个线程同时往 HashMap 中 put 数据会发生什么? ConcurrentHashMap 是怎么保证线程安全的? 在分析 HashMap 源码时还遗留这两个问题,这次我们站在 Java 多线程 ...
- Linux之基础命令——打包压缩
tar(备份与打包) linux中tar命令相当于win下的多个文件压缩成一个压缩文件. -c:创建新的备份文件 -x:还原压缩的备份文件 -f:需要备份归档的文件名 -j:使用bzip2解压缩 -z ...
- sh NonUniqueObjectException
话题引入: 使用hibernate进行更新操作时,首先调用了findById方法获取要修改的对象,此时session没有被关闭,接着重新创建一个对象,将要修改的属性值赋值给这个对象.调用修改方法抛出如 ...
- linux crontab创建计划任务
1.编辑计划任务 编辑crontab文件 crontab -e 2.查看计划任务日志 查看crontab日志 tail -100f /var/log/cron 3.创建计划任务格式 (1)基本格式 : ...
- ASP.NET自学之路(转载)
第一步 掌握一门NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NET是一个全面向对象的技术,不懂OO,那绝对学不下去! 第 ...
- 微信小程序---账号注册与开发工具
(1)申请帐号 进入小程序注册页 根据指引填写信息和提交相应的资料,就可以拥有自己的小程序帐号. 在这个小程序管理平台,你可以管理你的小程序的权限,查看数据报表,发布小程序等操作. 登录 小程序后台 ...
- java_lock锁
lock锁是一个接口,jdk5.0新增的接口: 在线程中创建一个他的实现类对象Reentrantlock,默认为fals可以改为true,改为true后是有序的 把操作共享资源的代码放入try中,在t ...
- Spring Boot . 4 -- 定制 Spring Boot 配置
覆写 Auto-Configuration 的类 利用外部属性进行动态配置 [本文] 定制 Error 页面 [第二篇] Spring Boot的自动配置可以节省很多无趣的配置工作,但是并不是所有的自 ...
- 微信小程序UI组件库 iView Weapp快速上手
概述 今天在网上突然看到iView新出了一个微信小程序的组件库iView Weapp,自己就上手试了一下,发现用起来还是不错的,把自己使用的过程与大家分享下. 一 预览iView组件 1.可以在手机上 ...