hdu 4259 Double Dealing(循环节+最小公倍数)
来源:互联网 发布:在淘宝买东西可靠吗 编辑:程序博客网 时间:2024/06/06 00:07
Problem Description
Take a deck of n unique cards. Deal the entire deck out tok players in the usual way: the top card to player 1, the next to player 2, thekth to playerk, 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 playerk’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?
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 andk (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 310 352 40 0
Sample Output
1413题目大意:n张不同的卡片,k个玩家,卡片轮流发给1~k个人,每个人将得到的卡片按先后顺序从上向下叠放,第二轮将1~k个人的卡片依次按从下往上顺序依次取出排成一列, 然后重复操左...问几次操作后序列第一次变为初序列。例如有10张牌,3个玩家,初始序列 1 2 3 4 5 6 7 8 9 10 第一次发放 person1 p2 p3 第二次发放 p1 p2 p3 三次 p1 p2 p3 四次 p1 p2 p3 1 2 3 10 7 4 3 2 1 4 7 10 4 5 6 1 8 5 10 9 8 3 6 9 7 8 9 2 9 6 7 6 5 2 5 8 10 3 4 1 第四次后依次取出得序列 1 2 3 4 5 6 7 8 9 10,故输出4。分析: 可以预处理当前处序列中第x个,发放一次后下一次的位置next[x]。 那么就可以推算出每个x位置再次回到x位置所需步骤数,很显然,求出最小公倍数即可。 如果对于每个点都转一圈求步骤数求一次显然比较赘余,复杂度大约O(n^2),在此题中会超时。在x回到x位置的过程中到达过的位置都和x位置有一样的回转 周期(为什么?)那么每个点只需访问一次,复杂度降为O(n)。 对于当前第x位置,他在如上图所示的矩阵中(x轴方向从上往下,y轴方向从左向右)x坐标dx=ceil(1.0*x/k),y坐标dy=c%k(if(c%k==0)dy=k),即下一次 的发放后坐标。而对于(x,y)点,他下一次在序列中的序号是 前y列个数-x+1 。那么对于一个x位置,先转化为坐标位置,再计算出下一次的线性位置,成功 转换。#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<algorithm>using namespace std;typedef long long LL;int n,k;int next[900],person[900],persons[900];char vis[900];LL gcd(LL a,LL b){ LL c; while(c=a%b) { a=b; b=c; } return b;}/* 数据量有点大,写个挂跑起来更爽一点bool scan() { n=k=0; char c; while((c=getchar())!=' ')n=n*10+c-'0'; while((c=getchar())!='\n')k=k*10+c-'0'; return n==0 && k==0;} */int main(){ int i,j,m; while(scanf("%d%d",&n,&k)==2 && (n||k)) { int ave=n/k; int ys=n%k; int p=0; persons[0]=0; for(i=1;i<=k;i++) person[i]=ave+(i<=ys?1:0), persons[i]=persons[i-1]+person[i]; //前i个人牌数 for(i=1;i<=n;i++) { int r=i%k?i/k+1:i/k; // 行数 int c=i%k; if(c==0) c=k; // 列数 next[i]=persons[c]-r+1; //下次位置 } memset(vis,0,sizeof(vis)); LL ans=1; for(i=1;i<=n;i++) { if(vis[i]) continue; int tm=i; LL cnt=0; while(!vis[tm]) { cnt++; vis[tm]=1; tm=next[tm]; } if(cnt>1) ans=ans/gcd(ans,cnt)*cnt; } printf("%I64d\n",ans); }}
0 0
- hdu 4259 Double Dealing(循环节+最小公倍数)
- HDU 4259 - Double Dealing(求循环节)
- HDU 4259 Double Dealing
- TOJ 4351 HDU 4259 Double Dealing
- HDU 4259 Double Dealing【简单群置换】
- HDOJ 4259 Double Dealing
- HDU 4259 Double Dealing (离散数学)#By Plato
- HDU 4259 Double Dealing(置换群啊)
- hdu 4529 Double Dealing (置换群)
- HDU 4259 Double Dealing 8-25日网络赛1003题
- HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))
- HDOJ 4259 Double Dealing(置换群)
- hdu4259 Double Dealing----置换群
- hdu4259 Double Dealing (置换群).
- Double Dealing----HDU4259----找规律
- hdu 最小公倍数
- hdu 最小公倍数
- hdu 1108 最小公倍数
- OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法
- android Matrix图片随意的放大缩小,拖动
- 《STL源码剖析》---stl_deque.h阅读笔记(2)
- Struts2笔记七---拦截器
- 错排公式
- hdu 4259 Double Dealing(循环节+最小公倍数)
- 黑马视频学习笔记-数组(一)-初始化
- Humble Numbers - HDU 1058 递推
- 只有读书最平等
- 跟着ZHONGHuan学习设计模式--桥接模式
- 编译QT项目出现错误:error C2144: syntax error : 'void' should be preceded by ';'
- SJF2410烧录
- Struts2文件上传
- 项目开发界面开发部分总结