PS [29일차]

Daniel·2022년 8월 18일
0

PS

목록 보기
29/32
post-thumbnail

문제번호

1010

package Main;

import java.util.*;

public class Main {

	public static void main(String[] args) {

		Scanner in = new Scanner (System.in);
		
		StringBuilder sb = new StringBuilder();
		
		int testcase = in.nextInt();
		
		for (int i = 0 ; i < testcase ; i++) {
			int pre = in.nextInt();
			int post = in.nextInt();
			
			if (pre == 1) {
				sb.append(post+"\n");
			}
			else if (pre == post) {
				sb.append(1+"\n");	
			}
			else {
				// post = 4, pre = 2
				// post = 5, pre = 3
				if (post/2 <= pre) {
					pre = post - pre;
				}
				// calculate
				long result = 1;
				int toPre = 1;
				long tempPre = 1;
				long tempPost = 1;
				while(toPre <= pre) {
					if (post % toPre == 0) {
						result *= post / toPre;
					}
					else {
						int max = findMaxDivisor(toPre, post);
						tempPre *= toPre / max;
						tempPost *= post / max;
					}
					toPre++;
					post--;
				}
				sb.append(result * tempPost / tempPre+"\n");
			}
					
		}
		System.out.println(sb);
	}
	static int findMaxDivisor (int toPre, int post) {
		int max = 0;
		for (int maxDivisor = 1; maxDivisor <= toPre / 2; maxDivisor++) {
			if (toPre % maxDivisor == 0 && post % maxDivisor == 0) {
				max = maxDivisor;
			}
		}
		return max;
	}

}

문제

경우의 수를 구하는 문제

풀이

  • 경우의 수를 알아야한다.
profile
폐쇄

0개의 댓글