티스토리 뷰

Programming/Algorithm

[중상] 서로소

dd_dev 2016. 10. 31. 21:58

서로소 : 유클리드 알고리즘(http://uwangg.tistory.com/24)

문제 : http://koitp.org/problem/RELATIVLEY_PRIME/read/



1. 다틀림 ㅋㅋㅋㅋ 다시 풀어보자

소스코드


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

//class Solution {
class SW02_seoroso{
	
	public static void main(String[] args) 
			throws FileNotFoundException 
	{

		File file =new File("D:\\dev\\worksspace\\Solution\\src\\SW02_seoroso");
		Scanner sc = new Scanner(file);
//		Scanner sc = new Scanner(System.in);
		
//		long start = System.currentTimeMillis();
		
		while (sc.hasNext()) {
			int N = sc.nextInt();
			
			int[] nArr = new int[N];
			for (int i = 0; i < N; i++) {
				nArr[i] = sc.nextInt();
			}
			
			int[][] dp = new int[N][N];
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					dp[i][j] = maxNanu(nArr[i],nArr[j]);
				}
			}
			
			double answer = 0;
			for (int i = 0; i < N-1; i++) {
				for (int j = i+1; j < N; j++) {
					if(dp[i][j] == 1){
						answer += Math.pow(2, N-j-1);
					}
				}
			}
			
			System.out.println((int)(answer%10000003));
					
		}
		
//		long end = System.currentTimeMillis();
//		System.out.println("time "+ (end-start)/1000.0);
	}

	private static int maxNanu(int i, int j) {
		
		if(j == 0){
			return i;
		}else{
			return maxNanu(j,i%j);
		}
	}
	
}




/*
3
2 3 4

3
*/


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함