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?
 

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 学生上课纪律差家长老师怎么办 老师管纪律学生打老师怎么办? 淘宝店铺层级排名下降怎么办 网购不支持该地区销售怎么办 分销公司佣金不给业务员怎么办 淘宝买家账号违规了怎么办 王者荣耀打野被队友抢野怎么办 艾肯材料换错了怎么办 淘宝直播粉丝5万怎么办 淘宝直播刚开始好难怎么办 分期乐账号忘记了怎么办 外链多了影响网站排名怎么办 自粘墙纸有气泡怎么办 贴壁纸阴角不力怎么办 自贴墙纸有气泡怎么办 壁纸贴的有起泡怎么办 pos机连接主机失败怎么办 无线pos机连接主机失败怎么办 水墨晕染模板用不了怎么办 申请入驻饿了么失败怎么办 天猫流量大淘宝怎么办 拼多多商家不退款怎么办 苹果手机开流量显示e怎么办 苹果手机有服务流量打不开怎么办 苹果七plus流量打不开怎么办 苹果手机突然打不开流量怎么办 天猫商家迟迟不发货怎么办 苹果7开不开机了怎么办 苹果机黑屏了开不起来怎么办 美团收银机连不上网怎么办 6s换电池后黑屏怎么办 苹果上的软件打不开也删不掉怎么办 苹果手机打开软件打不开怎么办 苹果电脑下载的软件打不开怎么办 苹果手机下载的软件都打不开怎么办 苹7果屏幕黑屏怎么办 苹果6s手机打不开怎么办 苹果ipad密码输入打不开怎么办 苹果平板黑屏了打不开怎么办 苹果6sp电影商城打不开怎么办 苹果6应用商城打不开怎么办