Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

분할 알고리즘 예시(1) 본문

algorithm

분할 알고리즘 예시(1)

코드와이 2021. 2. 16. 17:36

 

package week0215_0219;

import java.util.Scanner;

public class divide {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		
		//log(2) n 
		System.out.println(exp(x,y));
				
	}
	
	public static long exp(long x, long y) {
		
		if( y == 1) return x;
		
		long r = exp(x, y/2);
		long result = r*r;
		
		if ( y % 2 == 1) {
			result *= x;
		}
		return result;
	}
}

n = 2^30일 때 log(2) n 은 30

n이 크게 증가할수록 분할 알고리즘이 효율성이다.

'algorithm' 카테고리의 다른 글

에라토스테네스의 체  (0) 2021.03.01
[Back Tracking] N-Queen 예제 + 설명  (0) 2021.02.18
CompleteBinaryTree  (0) 2021.02.10
LinkedList  (0) 2021.02.10
부분집합  (0) 2021.02.05