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)

Total Submission(s): 1924    Accepted Submission(s): 679

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

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  4257 4258 4260 4261 4262

求置换群循环节的lcm

注意lcm(x1..xn)=lcm(x1,lcm(x2..xn)!=x1*..*xn/gcd

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
char s[]="no solution\n";

class Math
{
public:
    ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);}
    ll abs(ll x){if (x>=0) return x;return -x;}
    ll exgcd(ll a,ll b,ll &x, ll &y)
    {
        if (!b) {x=1,y=0;return a;}
        ll g=exgcd(b,a%b,x,y);
        ll t=x;x=y;y=t-a/b*y;
        return g;
    }
    ll pow2(ll a,int b,ll p)
    {
        if (b==0) return 1;
        if (b==1) return a;
        ll c=pow2(a,b/2,p);
        c=c*c%p;
        if (b&1) c=c*a%p;
        return c;
    }
    ll Modp(ll a,ll b,ll p)
    {
        ll x,y;
        ll g=exgcd(a,p,x,y),d;
        if (b%g) {return -1;}
        d=b/g;x*=d,y*=d;
        x=(x+abs(x)/p*p+p)%p;
        return x;
    }
    int h[MAXN];
    ll hnum[MAXN];
    int hash(ll x)
    {
        int i=x%MAXN;
        while (h[i]&&hnum[i]!=x) i=(i+1)%MAXN;
        hnum[i]=x;
        return i;
    }
    ll babystep(ll a,ll b,int p)
    {
        MEM(h) MEM(hnum)
        int m=sqrt(p);while (m*m<p) m++;
        ll res=b,ans=-1;

        ll uni=pow2(a,m,p);
        if (!uni) if (!b) ans=1;else ans=-1; //特判
        else
        {

            Rep(i,m+1)
            {
                int t=hash(res);
                h[t]=i+1;
                res=(res*a)%p;
            }
            res=uni;

            For(i,m+1)
            {
                int t=hash(res);
                if (h[t]) {ans=i*m-(h[t]-1);break;}else hnum[t]=0;
                res=res*uni%p;
            }

        }
        return ans;
    }
}S;

int a[10000+10];
bool b[10000+10];
int p[10000+10];
int main()
{
//    freopen("C.in","r",stdin);
//    freopen(".out","w",stdout);

    int n,k;
    while(cin>>n>>k)
    {
        if (n+k==0) return 0;
        int s=0;
        For(j,k)
            for(int i=n/k*k+j>n?n/k*k+j-k:n/k*k+j;i>=1;i-=k) a[++s]=i;

    //    For(i,n) cout<<a[i]<<' ';

        int tot=0;

        MEM(b)
        For(i,n)
        {
            if (!b[i])
            {
                int t=i; b[i]=1;
                int len=1;
                do {
                    b[t]=1;
                    t=a[t]; ++len;
              //       cout<<t<<endl;

                } while (!b[t]);
                len--;

                p[++tot]=len;
            }
        }

        sort(p+1,p+1+tot);
        tot=unique(p+1,p+1+tot)-(p+1);

//        For(i,tot) cout<<p[i]<<' ';

        ll ans=1;
        For(i,tot) ans=ans/S.gcd(p[i],ans)*p[i];

        cout<<ans<<endl;

    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-31 20:16:15

HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))的相关文章

HDU 4259 Double Dealing【简单群置换】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4259 题目大意:给出n张卡片以及k个人,现在对卡片进行分堆,然后分发(这样卡片改变了顺序),依次这样问多少次后卡片顺序回到原来一样. 比如给出的10,3. 第一次分堆是这样的 第一人:1,4,7,10 第二人:2,5,8 第三人:3,6,9 其顺序由1 2 3 4 5 6 7 8 9 10 变成了 10 7 4 1 8 5 2 9 6 3 即: 10 3 第一次 10 7 4 1 8 5 2 9 6

HDU 4259 Double Dealing(置换群啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4259 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,

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 Problem Description Take a deck of n unique cards. Deal the entire deck out to

HDOJ 4259 Double Dealing(置换群)

Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1893    Accepted Submission(s): 672 Problem Description Take a deck of n unique cards. Deal the entire deck out to k players in

hdu 4529 Double Dealing (置换群)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; __int64 gcd(__int64 a,__int64 b) { if(b==0) return a; return gcd(b,a%b); } int main() { int n,k,i,j,vis[810],m,num[810],x; __int64 res,cot; while(~scanf(

HDU 1908 Double Queue&lt;Set&gt;

Problem Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of th

hdu 1908 Double Queue

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using mod

POJ 3481 &amp; HDU 1908 Double Queue (map运用)

题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by

hdu 1908 double queues

http://acm.hdu.edu.cn/showproblem.php?pid=1908 看到有两个优先级,然后题目中又有queue...就想到了优先队列... 但是优先队列的cmp函数没搞懂,因为比较的是结构体,好像要重载< 什么的. 然而并不会. 其实用map就可以做... map在插入的时候可以自动按关键字排序,简直好评如潮! #include <algorithm> #include <cstdio> #include <iostream> #incl