组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)

CARDS

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1448   Accepted: 773

Description

Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the same label) and a shuffle machine. We assume that N is an odd integer. 
The shuffle machine accepts the set of cards arranged in an arbitrary order and performs the following operation of double shuffle : for all positions i, 1 <= i <= N, if the card at the position i is j and the card at the position j is k, then after the completion of the operation of double shuffle, position i will hold the card k.

Alice and Bob play a game. Alice first writes down all the numbers from 1 to N in some random order: a1, a2, ..., aN. Then she arranges the cards so that the position ai holds the card numbered ai+1, for every 1 <= i <= N-1, while the position aN holds the card numbered a1.

This way, cards are put in some order x1, x2, ..., xN, where xi is the card at the ith position.

Now she sequentially performs S double shuffles using the shuffle machine described above. After that, the cards are arranged in some final order p1, p2, ..., pN which Alice reveals to Bob, together with the number S. Bob‘s task is to guess the order x1, x2, ..., xN in which Alice originally put the cards just before giving them to the shuffle machine.

Input

The first line of the input contains two integers separated by a single blank character : the odd integer N, 1 <= N <= 1000, the number of cards, and the integer S, 1 <= S <= 1000, the number of double shuffle operations. 
The following N lines describe the final order of cards after all the double shuffles have been performed such that for each i, 1 <= i <= N, the (i+1)st line of the input file contains pi (the card at the position i after all double shuffles).

Output

The output should contain N lines which describe the order of cards just before they were given to the shuffle machine. 
For each i, 1 <= i <= N, the ith line of the output file should contain xi (the card at the position i before the double shuffles).

Sample Input

7 4
6
3
1
2
4
7
5

Sample Output

4
7
5
6
1
2
3

Source

CEOI 1998



Mean:

剀剀和凡凡有N张牌(依次标号为1,2,……,N)和一台洗牌机。假设N是奇数。洗牌机的功能是进行如下的操作:对所有位置I(1≤I≤N),如果位置I上的牌是J,而且位置J上的牌是K,那么通过洗牌机后位置I上的牌将是K。

剀剀首先写下一个1~N的排列ai,在位置ai处放上数值ai+1的牌,得到的顺序x1, x2, ..., xN作为初始顺序。他把这种顺序排列的牌放入洗牌机洗牌S次,得到牌的顺序为p1, p2, ..., pN。

现在,剀剀把牌的最后顺序和洗牌次数告诉凡凡,要凡凡猜出牌的最初顺序x1, x2, ..., xN。

analyse:

刚开始搞置换群,看得云里雾里的,还好看到了潘震皓的《置换群快速幂运算 + 研究与探讨》,讲的很清楚,而且很符合ACM的出题习惯。

很显然,这是一题典型的置换群问题,一副扑克就是一个置换,而对于每次的操作,我们可以看作置换的平方运算,题目说n为奇数,这就保证了在进行置换平方运算的过程中不会出现分裂,那么我们就可以使用置换群的快速幂来做了。

进行2*x次运算就可,当然其中有一个剪枝,将O(n^2+logs)的时间复杂度变为了O(n+logs)了,十分经典。

Time complexity:O(n+logs)

Source code:

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-09-11-20.34
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1010
#define LL long long
using namespace std;
int a[N],b[N],c[N],n,m;
int work()
{
    int j;
    int cnt=0;
    while(1)
    {
        for(int i=1;i<=n;i++)
           b[i]=c[c[i]];
        cnt++;
        for(j=1;j<=n;j++)
          if(b[j]!=a[j])
            break;
        if(j>n)break;
        for(int i=1;i<=n;i++)
           c[i]=b[i];
    }
    return cnt;
}
int main()
{
//    freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
//    freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            c[i]=a[i];
            b[i]=a[i];
        }
        int cnt=work();
        m%=cnt;
        m=cnt-m;
        while(m--)
        {
            for(int i=1;i<=n;i++)
              b[i]=a[a[i]];
            for(int i=1;i<=n;i++)
               a[i]=b[i];
        }
        for(int i=1;i<=n;i++)
          printf("%d\n",b[i]);
    }
    return 0;
}

  

时间: 2024-12-22 13:40:27

组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)的相关文章

[Ceoi98]Cards洗牌机

Description 剀剀和凡凡有N张牌(依次标号为1,2,--,N)和一台洗牌机.假设N是奇数.洗牌机的功能是进行如下的操作:对所有位置I(1≤I≤N),如果位置I上的牌是J,而且位置J上的牌是K,那么通过洗牌机后位置I上的牌将是K. 剀剀首先写下一个1~N的排列ai,在位置ai处放上数值ai+1的牌,得到的顺序x1, x2, ..., xN作为初始顺序.他把这种顺序排列的牌放入洗牌机洗牌S次,得到牌的顺序为p1, p2, ...,pN.现在,剀剀把牌的最后顺序和洗牌次数告诉凡凡,要凡凡猜出

[CareerCup] 18.2 Shuffle Cards 洗牌

18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of the 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. 这道题让我们实现一个洗牌的算法,实际上洗

poj 3128 Leonardo&#39;s Notebook (置换群的整幂运算)

题意:给你一个置换P,问是否存在一个置换M,使M^2=P 思路:资料参考 <置换群快速幂运算研究与探讨> https://wenku.baidu.com/view/0bff6b1c6bd97f192279e9fb.html 结论一: 一个长度为 l 的循环 T,l 是 k 的倍数,则 T^k 是 k 个循环的乘积,每个循环分别是循环 T 中下标 i mod k=0,1,2- 的元素按顺序的连接. 结论二:一个长度为 l 的循环 T,gcd(l,k)=1,则 T^k 是一个循环,与循环 T 不一

关于java洗牌发牌小程序

package play.card; public class Card { public String num; public String suit; public Card(String num,String suit) { this.num = num; this.suit = suit; } public Card() { super(); } @Override public String toString() { String str = suit+" "+ num; r

POJ 1845 Sumdiv【同余模运算+递归求等比数列和+快速幂运算】

快速幂运算在第一次训练时候就已经遇到过,这里不赘述 同余模运算也很简单,这里也不说了,无非是(a+b)%m (a*b)%m 把m弄到里面变成(a%m+b%m)%m   (a%m*b%m)%m 今天学的最重要的还是递归二分求等比数列 题目大意是给出A和B,求A^B的约数和 解这个题,首先,对A进行素因子分解得到 (PI(pi^ai))^B 然后我们有约数和公式: 对A=PI(p1^k1) A的所有因子之和为S = (1+p1+p1^2+p1^3+...p1^k1) * (1+p2+p2^2+p2^

[ACM] POJ 3070 Fibonacci (矩阵幂运算)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9517   Accepted: 6767 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence

[ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In

[ACM] hdu 3923 Invoker (Poyla计数,快速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In

2541 幂运算

2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 从m开始,我们只需要6次运算就可以计算出m31: m2=m×m,m4=m2×m2,m8=m4×m4,m16=m8×m8,m32=m16×m16,m31=m32÷m. 请你找出从m开始,计算mn的最少运算次数.在运算的每一步,都应该是m的正整数次方,换句话说,类似m-3是不允许出现的. 输入描述 Input Description 输入为一