HDU 4985 Little Pony and Permutation(数学 置换群)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985

置换群:http://baike.baidu.com/view/1879054.htm?fr=aladdin

Little Pony and Permutation

Problem Description

As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in the decomposition of permutations.
A permutation of a set S = {1, 2, ..., n} is a bijection from S to itself. In the great magician —— Cauchy‘s two-line notation, one lists the elements of set S in the first row, and then for each element, writes its image under the permutation below it in
the second row. For instance, a permutation of set {1, 2, 3, 4, 5} σ can be written as:

Here σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3, and σ(5) = 1.

Twilight Sparkle is going to decompose the permutation into some disjoint cycles. For instance, the above permutation can be rewritten as:

Help Twilight Sparkle find the lexicographic smallest solution. (Only considering numbers).

Input

Input contains multiple test cases (less than 10). For each test case, the first line contains one number n (1<=n<=10^5). The second line contains n numbers which the i-th of them(start from 1) is σ(i).

Output

For each case, output the corresponding result.

Sample Input

5
2 5 4 3 1
3
1 2 3

Sample Output

(1 2 5)(3 4)
(1)(2)(3)

Source

BestCoder Round #7

题意:

题意较难理解,其实就是一个置换群!

看代码就会理解题意!

代码如下:

/*#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 100005;

int n, a[N], vis[N];

int main()
{
    while (~scanf("%d", &n))
    {
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for (int i = 1; i <= n; i++)
        {
            if (vis[i])
                continue;
            int t = i;
            printf("(%d", t);
            vis[t] = 1;
            t = a[t];
            while (vis[t] == 0)
            {
                vis[t] = 1;
                printf(" %d", t);
                t = a[t];
            }
            printf(")");
        }
        printf("\n");
    }
    return 0;
}*/

#include <cstdio>
#include <cstring>
int main()
{
    int n;
    int a[100017], f[100017];
    while(~scanf("%d",&n))
    {
        memset(f,0,sizeof(f));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i = 1; i <= n; i++)
        {
            if(f[i])
                continue;
            int t = i;
            f[t] = 1;
            printf("(%d",t);
            while(f[a[t]] == 0)
            {
                printf(" %d",a[t]);
                f[a[t]] = 1;
                t = a[t];
            }
            printf(")");
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-29 03:13:08

HDU 4985 Little Pony and Permutation(数学 置换群)的相关文章

HDU 4985 Little Pony and Permutation(置换)

HDU 4985 Little Pony and Permutation 题目链接 题意:给定一个置换,输出分解成的循环 水题,直接模拟即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N = 100005; int n, a[N], vis[N]; int main() { whi

[BestCoder Round #7] hdu 4985 Little Pony and Permutation (找循环节)

Little Pony and Permutation Problem Description As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight

【HDOJ】4985 Little Pony and Permutation

水题. 1 #include <cstdio> 2 3 #define MAXN 100005 4 5 int buf[MAXN], n; 6 7 int main() { 8 int i, j, k; 9 10 while (scanf("%d", &n) != EOF) { 11 for (i=1; i<=n; ++i) 12 scanf("%d", &buf[i]); 13 for (i=1; i<=n; ++i) {

hdu 5621 KK&#39;s Point(数学,推理题)

题解: 在圆上点三个点时,除圆上三个交点外,圆内没有交点:在圆上点四个点时,除圆上四个交点外,圆内出现了一个交点,因此,在N个点中每四个点便可以在圆内产生一个交点,因此N个点在圆内形成的点的个数为CN4,总的交点数就是CN4+N 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstrin

hdu 5344 MZL&#39;s xor(数学之异或)

Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all (Ai+Aj)(1≤i,j≤n) The xor of an array B is defined as B1 xor B2...xor Bn Input Multiple test cases, the first line contains an inte

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】

难度上,,,确实,,,不算难 问题是有个矩阵运算的优化 题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵,然后算C^(N*N) 相当于 ABABABABABABAB...=(AB)^(N*N) 不如 A(BA)^(N*N-1)B 因为BA乘得K*K的矩阵,K是比较小的 #include <cstdio> #include <cstdlib> #include <cstr

hdu 4803 Poor Warehouse Keeper(贪心+数学)

题目链接:hdu 4803 Poor Warehouse Keeper 题目大意:有以个屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,x+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的为取整后的整数,小数部分忽略.给定一个目标x,y,初始状态为1,1,求最少需要多少次可以目标状态,不可以达到的话输出-1. 解题思路:如果是加一次总价的话,单价就在变大:如果是加一次数量的话,单价是不变的.总而言之,单价是只会往上

hdu 4985(模拟)

Little Pony and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 639    Accepted Submission(s): 342 Problem Description As a unicorn, the ability of using magic is the distinguishing

HDU 4986 Little Pony and Alohomora Part I(递推+犹拉常数)

HDU 4986 Little Pony and Alohomora Part I 题目链接 题意:一些钥匙随机放在箱子里,现在问打开次数期望 思路:每种方式相当于一个置换的循环个数,那么考虑f[i]为i个箱子的情况,f[i + 1]要么就是放在最后多一个循环,要么就是插入中间循环个数不变,对应的转移为f[i + 1] = (f[i] + 1) / i + f[i] * (i - 1) / i 化简得到f[i] = f[i - 1] + 1 / i 这个式子i越大,越趋近lni + C,这个C为