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
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).
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 ).
本来是一道水题,求出所有循环节的长度,然后求个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 + ];
int len_in = ;
int pos_in = ; inline void Read() {
if(len_in < MAX_SINGLE_DATA) {
int len = ;
while(len_in--)
buff[len++] = buff[pos_in++];
len_in = len + fread(buff + len, , MAX_LEN - len, stdin);
pos_in = ;
}
} inline int Getchar() {
Read();
if(len_in == ) return -;
int res = buff[pos_in];
if(++pos_in == MAX_LEN) pos_in = ;
len_in--;
return res;
} char buff_out[MAX_LEN + ];
int len_out = ;
inline void Flush() {
fwrite(buff_out, , len_out, stdout);
len_out = ;
} 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=;
while(!(ch>=''&&ch<='')) ch=getchar();
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return res;
} template<class T>
inline void Out(T a) {
static int arr[];
int p = ;
do{
arr[p++] = a%;
a /= ;
}while(a);
while(p--) {
putchar(arr[p]+'');
}
} #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 = ;
while (m) {
if (m & ) ret = ret * n % mod;
n = n * n % mod;
m >>= ;
}
return ret;
} #endif //ICPC_QUICK_POWER_HPP int a[];
bool vis[];
map<int, int> ms;
int prime[];
bool ok[];
int gao = ;
void init(){
rep2(i,,)ok[i] = ;
rep2(i,,){
if(ok[i]){
prime[gao++] = i;
for(int j = i*i;j<;j+=i)ok[j] = ;
}
}
}
class hdu5392 {
public:
void solve() {
int t;
init();
t = Scan();//Scan(t);//in>>t;
ll mod = ;
while (t--) {
int n;
ms.clear();
n = Scan();//Scan(n);//in>>n;
rep2(i, , n) {
vis[i] = ;
a[i] = Scan();//Scan(a[i]);//in>>a[i];
}
rep2(i, , n) {
if (!vis[i]) {
int len = ;
int x = a[i];
vis[i] = ;
while (!vis[x]) {
vis[x] = ;
x = a[x];
len++;
}
for (int k = ; k<gao && prime[k] * prime[k] <= len; k++) {
int j = prime[k];
if (len % j == ) {
int num = ;
while (len % j == ) {
num++;
len /= j;
}
if (!ms.count(j))ms[j] = num;
else ms[j] = max(ms[j], num);
}
}
if (len != ) {
if (!ms.count(len))ms[len] = ;
}
}
}
ll ans = ;
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 ;
}
代码君
hdu5392 Infoplane in Tina Town(LCM)的更多相关文章
- [hdu5392 Infoplane in Tina Town]置换的最小循环长度,最小公倍数取模,输入挂
题意:给一个置换,求最小循环长度对p取模的结果 思路:一个置换可以写成若干循环的乘积,最小循环长度为每个循环长度的最小公倍数.求最小公倍数对p取模的结果可以对每个数因式分解,将最小公倍数表示成质数幂的 ...
- HDU 5392 Infoplane in Tina Town
Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- hdu 5392 Infoplane in Tina Town(数学)
Problem Description There is a big stone with smooth surface in Tina Town. When people go towards it ...
- hdoj 5392 Infoplane in Tina Town
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392 #include<stdio.h> #include<cstring> ...
- HDU-5391 Zball in Tina Town
(n-1)!/n 就是如果n为素数,就等于n-1else为0. 求素数表: Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others) Memo ...
- (hdu)5391 Zball in Tina Town
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...
- hdu5391 Zball in Tina Town(威尔逊定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Zball in Tina Town Time Limit: 3000/1500 ...
- hdu 5391 Zball in Tina Town(打表找规律)
问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候, ...
- C#版 - HDUoj 5391 - Zball in Tina Town(素数) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. HDUoj 5 ...
随机推荐
- 6 个轻量级并且灵活的 PHP CMS 系统
Anchor CMS Just like its introduction says, Anchor has a very simple UI. The installation takes lite ...
- 大整数算法[09] Comba乘法(原理)
★ 引子 原本打算一篇文章讲完,后来发现篇幅会很大,所以拆成两部分,先讲原理,再讲实现.实现的话相对复杂,要用到内联汇编,要考虑不同平台等等. 在大整数计算中,乘法是非常重要的,因为 ...
- 关于接口使用getType的方法的问题
C#代码如下: public interface IConfigInfo { } public class A : IConfigInfo { } public c ...
- Android 中HttpURLConnection与HttpClient的简单使用
1:HttpHelper.java public class HttpHelper { //1:标准的Java接口 public static String getStringFromNet1(Str ...
- C51 库函数
C-51软件包的库包含标准的应用程序,每个函数都在相应的头文件(.h)中有原型声明.如果使用库函数,必须在源程序中用预编译指令定义与该函数相关的头文件(包含了该函数的原型声明).例如:#include ...
- WPF自定义控件与样式(15)-终结篇
原文:WPF自定义控件与样式(15)-终结篇 系列文章目录 WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与 ...
- ChangeServiceConfig2 function
ChangeServiceConfig2 function Changes the optional configuration parameters of a service. Syntax C ...
- AcitonBar 自定义布局
Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar. 自定义Activity主题和ActionBar样式 在新建的 ...
- @Valid springMVC bean校验不起作用及如何统一处理校验
SpringMVC 使用JSR-303进行校验 @Valid 使用注解 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-vali ...
- PHP上传文件出现文件名被截掉第一个字符的问题
最近用PHP做了一个简单的上传功能,结果出现了一个意想不到的问题.我上传的文件,在获取$_FILES的时候发现文件名的第一个字符被截掉了,因为最开始上传的一直是数字或者字母为名称的文档,也没有太在意这 ...