HDU5392 Infoplane in Tina TownLCM

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5392 Infoplane in Tina TownLCM相关的知识,希望对你有一定的参考价值。

Infoplane in Tina Town
Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2713 Accepted Submission(s): 730

Problem Description
There is a big stone with smooth surface in Tina Town. When people go towards it, the stone surface will be lighted and show its usage. This stone was a legacy and also the center of Tina Town’s calculation and control system. also, it can display events in Tina Town and contents that pedestrians are interested in, and it can be used as public computer. It makes people’s life more convenient (especially for who forget to take a device).

Tina and Town were playing a game on this stone. First, a permutation of numbers from 1 to n were displayed on the stone. Town exchanged some numbers randomly and Town recorded this process by macros. Town asked Tine,”Do you know how many times it need to turn these numbers into the original permutation by executing this macro? Tina didn’t know the answer so she asked you to find out the answer for her.

Since the answer may be very large, you only need to output the answer modulo 3∗230+1=3221225473 (a prime).

Input
The first line is an integer T representing the number of test cases. T≤5

For each test case, the first line is an integer n representing the length of permutation. n≤3∗106

The second line contains n integers representing a permutation A1…An. It is guaranteed that numbers are different each other and all Ai satisfies ( 1≤Ai≤n ).

Output
For each test case, print a number ans representing the answer.

Sample Input
2
3
1 3 2
6
2 3 4 5 6 1

Sample Output
2
6

Source
BestCoder Round #51 (div.2)

问题链接HDU5392 Infoplane in Tina Town
问题简述:(略)
问题分析:LCM问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU5392 Infoplane in Tina Town */

#include <bits/stdc++.h>

using namespace std;

const long long MOD = 3221225473;
const int N = 3e6 + 1;
int a[N], vis[N], num[N];

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);

        memset(vis, 0, sizeof(int) * (n + 1));
        memset(num, 0, sizeof(int) * (n + 1));
        for (int i = 1; i <= n; i++) {
            if (vis[i] == 0) {
                // 计算循环长度
                int cnt = 0, k = i;
                while (vis[k] == 0)
                    vis[k] = 1, cnt++, k = a[k];
                for (int j = 2; j * j <= cnt; j++) {
                    int sum = 0;
                    if (cnt % j == 0)
                        while (cnt % j == 0) cnt /= j, sum++;
                    num[j] = max(num[j], sum);
                }
                if (cnt > 1) num[cnt] = max(num[cnt], 1);
            }
        }

        long long ans = 1;
        for (int i = 2; i <= n; i++)
            while (num[i]--) ans = ans * i % MOD;

        printf("%lld\\n", ans);
    }

    return 0;
}

以上是关于HDU5392 Infoplane in Tina TownLCM的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5391 Zball in Tina Town威尔逊定理

hdu5391Zball in Tina Town

HDU5391Zball in Tina Town

HDU.5394.Trie in Tina Town(回文树)

BC - Zball in Tina Town (质数 + 找规律)

01背包HDU 1171 Big Event in HDU