标签:
不理解或者需要交流的同学可以粉我新浪微博@雷锹,私信哟!!!
题目所给的矩阵是一种特殊矩阵,掌握矩阵运算的规律就好,这种大数据最好划整为零
今天老师问我这道题用了什么算法,其实没用很高深的算法,只是在掌握运算规律的前提下不断去减少它的运算量
有兴趣的同志可以研究一下我的求最小公倍数部分
package com.jueshai2014; import java.math.BigInteger; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class _5 { @SuppressWarnings("unused") public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int row[] = new int[n + 1]; int pos [] = new int [n + 1]; int count [] = new int [n + 1]; HashSet<Integer> hashset = new HashSet<Integer>(); for (int i = 0; i < n; i++) { int x = scan.nextInt(); int y = scan.nextInt(); pos[x] = y; row[x] = y; } long time1 = System.currentTimeMillis(); for (int i = 1; i <= n; i++) { hashset.add(exec(row, pos, i)); } Iterator<Integer> it= hashset.iterator(); BigInteger sum = new BigInteger(it.next()+""); while(it.hasNext()){ BigInteger next = new BigInteger(it.next()+""); BigInteger max,min; max= sum.max(next); min = sum.min(next); for(int i = 1; i <= min.intValue(); i++){ BigInteger temp = new BigInteger(i+""); if(max.multiply(temp).mod(min).equals(BigInteger.ZERO)){ sum = max.multiply(temp);break; } } } System.out.println(sum); long time3 = System.currentTimeMillis(); System.out.println("运行时间:"+ (time3-time1)); } private static int exec(int[] row, int[] pos, int i) { int temp = 1; while (true) { row[i] = pos[row[i]]; temp++; if (row[i] == i) break; } if(temp == 2) return 1; return temp; } }
标签:
原文地址:http://blog.csdn.net/u013993712/article/details/51329471