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 }

 

posted on 2015-08-16 00:29  round_0  阅读(275)  评论(0编辑  收藏  举报

导航