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).
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 ).
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }