您的位置:首页 > 运维架构

hdu5392 Infoplane in Tina Town(LCM)

2015-08-16 00:24 435 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud

Infoplane in Tina Town

[b]Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 518 Accepted Submission(s): 74


[/b]

[align=left]Problem Description[/align]
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).

[align=left]Input[/align]
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 ).

[align=left]Output[/align]
For each test case, print a number ans representing the answer.

[align=left]Sample Input[/align]

2

3

1 3 2

6

2 3 4 5 6 1

[align=left]Sample Output[/align]

2

6

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

/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper * @author xyiyy @https://github.com/xyiyy
*/

#include <iostream>
#include <fstream>

//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/ //#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>

using namespace std;
#define rep2(X, L, R) for(int X=L;X<=R;X++)
typedef long long ll;

//
// Created by xyiyy on 2015/8/7.
//

#ifndef ICPC_SCANNER_HPP
#define ICPC_SCANNER_HPP

#define MAX_LEN 20000000
#define MAX_SINGLE_DATA 100
#define getchar Getchar
#define putchar Putchar

char buff[MAX_LEN + 1];
int len_in = 0;
int pos_in = 0;

inline void Read() {
if(len_in < MAX_SINGLE_DATA) {
int len = 0;
while(len_in--)
buff[len++] = buff[pos_in++];
len_in = len + fread(buff + len, 1, MAX_LEN - len, stdin);
pos_in = 0;
}
}

inline int Getchar() {
Read();
if(len_in == 0) return -1;
int res = buff[pos_in];
if(++pos_in == MAX_LEN) pos_in = 0;
len_in--;
return res;
}

char buff_out[MAX_LEN + 1];
int len_out = 0;
inline void Flush() {
fwrite(buff_out, 1, len_out, stdout);
len_out = 0;
}

inline void Putchar(char c) {
buff_out[len_out++] = c;
if(len_out + MAX_SINGLE_DATA >= MAX_LEN)
Flush();
}

inline int Scan() {
int res, ch=0;
while(!(ch>='0'&&ch<='9')) ch=getchar();
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return res;
}

template<class T>
inline void Out(T a) {
static int arr[20];
int p = 0;
do{
arr[p++] = a%10;
a /= 10;
}while(a);
while(p--) {
putchar(arr[p]+'0');
}
}

#endif //ICPC_SCANNER_HPP

//
// Created by xyiyy on 2015/8/5.
//

#ifndef ICPC_QUICK_POWER_HPP
#define ICPC_QUICK_POWER_HPP
typedef long long ll;

ll quick_power(ll n, ll m, ll mod) {
ll ret = 1;
while (m) {
if (m & 1) ret = ret * n % mod;
n = n * n % mod;
m >>= 1;
}
return ret;
}

#endif //ICPC_QUICK_POWER_HPP

int a[3000010];
bool vis[3000010];
map<int, int> ms;
int prime[3010];
bool ok[3010];
int gao = 0;
void init(){
rep2(i,2,3009)ok[i] = 1;
rep2(i,2,3009){
if(ok[i]){
prime[gao++] = i;
for(int j = i*i;j<3010;j+=i)ok[j] = 0;
}
}
}
class hdu5392 {
public:
void solve() {
int t;
init();
t = Scan();//Scan(t);//in>>t;
ll mod = 3221225473;
while (t--) {
int n;
ms.clear();
n = Scan();//Scan(n);//in>>n;
rep2(i, 1, n) {
vis[i] = 0;
a[i] = Scan();//Scan(a[i]);//in>>a[i];
}
rep2(i, 1, n) {
if (!vis[i]) {
int len = 1;
int x = a[i];
vis[i] = 1;
while (!vis[x]) {
vis[x] = 1;
x = a[x];
len++;
}
for (int k = 0; k<gao && prime[k] * prime[k] <= len; k++) {
int j = prime[k];
if (len % j == 0) {
int num = 0;
while (len % j == 0) {
num++;
len /= j;
}
if (!ms.count(j))ms[j] = num;
else ms[j] = max(ms[j], num);
}
}
if (len != 1) {
if (!ms.count(len))ms[len] = 1;
}
}
}
ll ans = 1;
for (auto x : ms) {
ans = ans * quick_power(x.first, x.second, mod) % mod;
}
Out(ans);
putchar('\n');//out<<ans<<endl;
}

}
};

int main() {
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
hdu5392 solver;
//std::istream &in(std::cin);
//std::ostream &out(std::cout);
solver.solve();
Flush();
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: