HDU5392

今晚BC的一道题,用JAVA写的,但正解并不是这个

这里主要是拿了一个读入优化的模板,存一下,或许以后能用到呢。

 还没有进行太多的测试,以后有机会再测吧。

这是这个题。

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.*;
 4
 5 public class Main {
 6     public static int fa[] = new int[3000005];
 7     public static int Find(int x){
 8         int rt = x;
 9         while(rt!=fa[rt])rt = fa[rt];
10         while(x!=rt){
11             int t = fa[x];
12             fa[x] = rt;
13             x = t;
14         }
15         return rt;
16     }
17     public static BigInteger gcd(BigInteger a,BigInteger b){
18         return b.compareTo(new BigInteger("0"))==1?gcd(b,a.mod(b)):a;
19     }
20     public static void main(String[] args) {
21
22         /*******************////输入输出
23         InputStream inputStream = System.in;
24         OutputStream outputStream = System.out;
25         InputReader in = new InputReader(inputStream);
26         PrintWriter out = new PrintWriter(outputStream);
27         /*******************/
28
29         int T = in.nextInt();
30         for(int kase = 1;kase<=T;++kase){
31             int n = in.nextInt();
32             int a[] = new int[3000005];
33             int siz[] = new int[3000005];
34             for(int i = 1;i<=n;++i)a[i] = in.nextInt();
35             for(int i = 1;i<=n;++i){fa[i] = i;siz[i] = 1;}
36             for(int i = 1;i<=n;++i){
37                 int fx = Find(i),fy = Find(a[i]);
38                 if(fx==fy)continue;
39                 fa[fy] = fx;
40                 siz[fx] = siz[fy]+siz[fx];
41                 siz[fy] = 0;
42             }
43             BigInteger ans = BigInteger.ONE,x;
44             for(int i = 1;i<=n;++i)if(siz[i]!=0){
45                 x = BigInteger.valueOf(siz[i]);
46                 ans = ans.multiply(x).divide(gcd(ans,x));
47             }
48             System.out.println(ans.mod(BigInteger.valueOf(3221225473L)));
49         }
50     }
51     ///读入优化类
52     static class InputReader {
53         public BufferedReader reader;
54         public StringTokenizer tokenizer;
55         public InputReader(InputStream stream) {
56             reader = new BufferedReader(new InputStreamReader(stream), 32768);
57             tokenizer = null;
58         }
59         public String next() {
60             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
61                 try {
62                     tokenizer = new StringTokenizer(reader.readLine());
63                 }
64                 catch (IOException e) {
65                     throw new RuntimeException(e);
66                 }
67             }
68             return tokenizer.nextToken();
69         }
70
71         public int nextInt(){
72             return Integer.parseInt(next());
73         }
74
75     }
76 }

这是JAVA写的话,大概的轮廓

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.*;
 4
 5 public class Main {
 6     public static void main(String[] args) {
 7
 8         /*******************////输入输出
 9         InputStream inputStream = System.in;
10         OutputStream outputStream = System.out;
11         InputReader in = new InputReader(inputStream);
12         PrintWriter out = new PrintWriter(outputStream);
13         /*******************/
14
15
16         /**
17
18             代码主体
19             输入 in.nextInt();//注意输入类型
20             输出 out.println();
21
22         **/
23
24
25
26
27     ///读入优化类
28     static class InputReader {
29         public BufferedReader reader;
30         public StringTokenizer tokenizer;
31         public InputReader(InputStream stream) {
32             reader = new BufferedReader(new InputStreamReader(stream), 32768);
33             tokenizer = null;
34         }
35         public String next() {
36             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
37                 try {
38                     tokenizer = new StringTokenizer(reader.readLine());
39                 }
40                 catch (IOException e) {
41                     throw new RuntimeException(e);
42                 }
43             }
44             return tokenizer.nextToken();
45         }
46         public int nextInt(){
47             return Integer.parseInt(next());
48         }
49     }
50 }
时间: 2024-10-17 08:47:17

HDU5392的相关文章

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