hdu5392--Infoplane in Tina Town(置换群+质因子分解求最小公倍数)

题目链接:点击打开链接

题目大意:给出一种操作a[1],a[2],,,,a[n],代表每交换一次,1位置的数到a[1]位置,2位置的数到a[2]位置,,,

问最终交换多少次可以恢复初始的情况。

题目给出一个置换,要求置换的次数,也就是所有轮换个数的最小公倍数。首先求出所有轮换的个数,然后求最小公倍数的时候不能用gcd,因为Mod的取余太大,所以用质因子分解,统计每个质因子出现的最多次数,计算最终的值。

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <time.h>
using namespace std ;
#pragma comment(linker, "/STACK:102400000,102400000")
#define LL __int64
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define maxn 3000010
const LL Mod = 3221225473 ;
int a[maxn] , sum[maxn] , vis[maxn] ;
int main() {
    int t , n , i , j , num , temp , cnt ;
    LL ans ;
    scanf("%d", &t) ;
    while( t-- ) {
        scanf("%d", &n) ;
        for(i = 1 ; i <= n ; i++) {
            scanf("%d", &a[i]) ;
            sum[i] = vis[i] = 0 ;
        }
        for(i = 1 ; i <= n ; i++) {
            if( vis[i] ) continue ;
            vis[i] = 1 ;
            num = 1 ;
            j = a[i] ;
            while( !vis[j] ) {
                num++ ;
                vis[j] = 1 ;
                j = a[j] ;
            }
            for(j = 2 , temp = num ; j*j <= num ; j++) {
                if( temp%j ) continue ;
                cnt = 0 ;
                while( temp%j == 0 ) {
                    temp /= j ;
                    cnt++ ;
                }
                sum[j] = max(sum[j],cnt) ;
                if( temp == 1 ) break ;
            }
            if( temp > 1 ) sum[temp] = max(sum[temp],1) ;
        }
        for(i = 2 , ans = 1 ; i <= n ; i++) {
            for(j = 0 ; j < sum[i] ; j++)
                ans = ans*i%Mod ;
        }
        printf("%I64d\n", ans) ;
    }
    return 0 ;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

时间: 2024-11-07 11:46:19

hdu5392--Infoplane in Tina Town(置换群+质因子分解求最小公倍数)的相关文章

hdu5392 Infoplane in Tina Town(LCM)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 518    Accepted Submission(s): 74 Problem Description There i

[hdu5392 Infoplane in Tina Town]置换的最小循环长度,最小公倍数取模,输入挂

题意:给一个置换,求最小循环长度对p取模的结果 思路:一个置换可以写成若干循环的乘积,最小循环长度为每个循环长度的最小公倍数.求最小公倍数对p取模的结果可以对每个数因式分解,将最小公倍数表示成质数幂的乘积形式,然后用快速幂取模,而不能一边求LCM一边取模. 由于这题数据量太大,需要用到输入挂,原理是把文件里面的东西用fread一次性读到内存. 输入挂模板: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 namespace IO { const s

HDU 5392 Infoplane in Tina Town

Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 805    Accepted Submission(s): 168 Problem Description There is a big stone with smooth surface in Tina Town. When peop

HDOJ 5392 Infoplane in Tina Town LCM

找循环节,分解质因数,求LCM Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1627    Accepted Submission(s): 380 Problem Description There is a big stone with smooth surface in Tin

hdu 5392 Infoplane in Tina Town (质因子分解求gcd)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5392 题意:至今没弄懂题意.按admin的意思猜的:求出每个循环的长度,然后求出这些长度的最小公倍数.结果%3221225473. 分析:首先求出每个循环的长度len,由于结果很大,用gcd求最小公倍数的时候不能直接模3221225473(模下gcd是不正确的......),可以将所有的长度len分解质因子,记录每种质因子的最大数量,,最后快速幂求结果. 代码: #include <iostream>

hdu 5392 Infoplane in Tina Town(数学)

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. als

hdoj 5392 Infoplane in Tina Town

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392 1 #include<stdio.h> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<set> 6 using namespace std; 7 const int MAXN = 3*1e6+10; 8 const unsigned int MOD

HDU 2601 An easy problem(暴力枚举/质因子分解)

An easy problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7963    Accepted Submission(s): 1920 Problem Description When Teddy was a child , he was always thinking about some simple math p

hdu 5391 Zball in Tina Town

点击此处即可传送 hdu 5391 唉,我以为带7的基本上都是素数,所以一直拿1007算,结果....唉,一把辛酸泪啊,算了,不说了,上正事: Problem Description Tina Town is a friendly place. People there care about each other. Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day,