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

 

本来是一道水题,求出所有循环节的长度,然后求个LCM就好了,唯一的坑点就是输入的量实在是大。。。输入外挂跑了7s,改成队友给的fread的输入外挂变成了1.5s

  1 /**
  2  * code generated by JHelper
  3  * More info: https://github.com/AlexeyDmitriev/JHelper
  4  * @author xyiyy @https://github.com/xyiyy
  5  */
  6
  7 #include <iostream>
  8 #include <fstream>
  9
 10 //#####################
 11 //Author:fraud
 12 //Blog: http://www.cnblogs.com/fraud/
 13 //#####################
 14 //#pragma comment(linker, "/STACK:102400000,102400000")
 15 #include <iostream>
 16 #include <sstream>
 17 #include <ios>
 18 #include <iomanip>
 19 #include <functional>
 20 #include <algorithm>
 21 #include <vector>
 22 #include <string>
 23 #include <list>
 24 #include <queue>
 25 #include <deque>
 26 #include <stack>
 27 #include <set>
 28 #include <map>
 29 #include <cstdio>
 30 #include <cstdlib>
 31 #include <cmath>
 32 #include <cstring>
 33 #include <climits>
 34 #include <cctype>
 35
 36 using namespace std;
 37 #define rep2(X, L, R) for(int X=L;X<=R;X++)
 38 typedef long long ll;
 39
 40 //
 41 // Created by xyiyy on 2015/8/7.
 42 //
 43
 44 #ifndef ICPC_SCANNER_HPP
 45 #define ICPC_SCANNER_HPP
 46
 47 #define MAX_LEN 20000000
 48 #define MAX_SINGLE_DATA 100
 49 #define getchar Getchar
 50 #define putchar Putchar
 51
 52 char buff[MAX_LEN + 1];
 53 int len_in = 0;
 54 int pos_in = 0;
 55
 56 inline void Read() {
 57     if(len_in < MAX_SINGLE_DATA) {
 58         int len = 0;
 59         while(len_in--)
 60             buff[len++] = buff[pos_in++];
 61         len_in = len + fread(buff + len, 1, MAX_LEN - len, stdin);
 62         pos_in = 0;
 63     }
 64 }
 65
 66 inline int Getchar() {
 67     Read();
 68     if(len_in == 0) return -1;
 69     int res = buff[pos_in];
 70     if(++pos_in == MAX_LEN) pos_in = 0;
 71     len_in--;
 72     return res;
 73 }
 74
 75 char buff_out[MAX_LEN + 1];
 76 int len_out = 0;
 77 inline void Flush() {
 78     fwrite(buff_out, 1, len_out, stdout);
 79     len_out = 0;
 80 }
 81
 82 inline void Putchar(char c) {
 83     buff_out[len_out++] = c;
 84     if(len_out + MAX_SINGLE_DATA >= MAX_LEN)
 85         Flush();
 86 }
 87
 88 inline int Scan() {
 89     int res, ch=0;
 90     while(!(ch>=‘0‘&&ch<=‘9‘)) ch=getchar();
 91     res=ch-‘0‘;
 92     while((ch=getchar())>=‘0‘&&ch<=‘9‘)
 93         res=res*10+ch-‘0‘;
 94     return res;
 95 }
 96
 97 template<class T>
 98 inline void Out(T a) {
 99     static int arr[20];
100     int p = 0;
101     do{
102         arr[p++] = a%10;
103         a /= 10;
104     }while(a);
105     while(p--) {
106         putchar(arr[p]+‘0‘);
107     }
108 }
109
110 #endif //ICPC_SCANNER_HPP
111
112 //
113 // Created by xyiyy on 2015/8/5.
114 //
115
116 #ifndef ICPC_QUICK_POWER_HPP
117 #define ICPC_QUICK_POWER_HPP
118 typedef long long ll;
119
120 ll quick_power(ll n, ll m, ll mod) {
121     ll ret = 1;
122     while (m) {
123         if (m & 1) ret = ret * n % mod;
124         n = n * n % mod;
125         m >>= 1;
126     }
127     return ret;
128 }
129
130 #endif //ICPC_QUICK_POWER_HPP
131
132 int a[3000010];
133 bool vis[3000010];
134 map<int, int> ms;
135 int prime[3010];
136 bool ok[3010];
137 int gao = 0;
138 void init(){
139     rep2(i,2,3009)ok[i] = 1;
140     rep2(i,2,3009){
141         if(ok[i]){
142             prime[gao++] = i;
143             for(int j = i*i;j<3010;j+=i)ok[j] = 0;
144         }
145     }
146 }
147 class hdu5392 {
148 public:
149     void solve() {
150         int t;
151         init();
152         t = Scan();//Scan(t);//in>>t;
153         ll mod = 3221225473;
154         while (t--) {
155             int n;
156             ms.clear();
157             n = Scan();//Scan(n);//in>>n;
158             rep2(i, 1, n) {
159                 vis[i] = 0;
160                 a[i] = Scan();//Scan(a[i]);//in>>a[i];
161             }
162             rep2(i, 1, n) {
163                 if (!vis[i]) {
164                     int len = 1;
165                     int x = a[i];
166                     vis[i] = 1;
167                     while (!vis[x]) {
168                         vis[x] = 1;
169                         x = a[x];
170                         len++;
171                     }
172                     for (int k = 0; k<gao && prime[k] * prime[k] <= len; k++) {
173                         int j = prime[k];
174                         if (len % j == 0) {
175                             int num = 0;
176                             while (len % j == 0) {
177                                 num++;
178                                 len /= j;
179                             }
180                             if (!ms.count(j))ms[j] = num;
181                             else ms[j] = max(ms[j], num);
182                         }
183                     }
184                     if (len != 1) {
185                         if (!ms.count(len))ms[len] = 1;
186                     }
187                 }
188             }
189             ll ans = 1;
190             for (auto x : ms) {
191                 ans = ans * quick_power(x.first, x.second, mod) % mod;
192             }
193             Out(ans);
194             putchar(‘\n‘);//out<<ans<<endl;
195         }
196
197     }
198 };
199
200 int main() {
201     //std::ios::sync_with_stdio(false);
202     //std::cin.tie(0);
203     hdu5392 solver;
204     //std::istream &in(std::cin);
205     //std::ostream &out(std::cout);
206     solver.solve();
207     Flush();
208     return 0;
209 }

代码君

时间: 2024-10-12 22:38:16

hdu5392 Infoplane in Tina Town(LCM)的相关文章

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

[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

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 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 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,

Zball in Tina Town(判素数)

Zball in Tina Town Accepts: 397 Submissions: 2463 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem Description Tina Town is a friendly place. People there care about each other. Tina has a ball called zball.

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

Zball in Tina Town Accepts: 541 Submissions: 2463 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候,它会变大22倍.在第nn天的时候,