티스토리 뷰
문제 : https://www.acmicpc.net/problem/2758
시간을 줄이는대로 줄여봤지만, 시간초과로 실패
조금더 생각해 봐야할 듯
소스코드
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class Baekjoon2758 {
public static void main(String[] args) throws FileNotFoundException {
long start = System.currentTimeMillis();
File file =new File("D:\\dev\\worksspace\\Solution\\src\\sample.txt");
Scanner sc = new Scanner(file);
int test_case_num = 0;
int max_test_case = Integer.parseInt(sc.nextLine());
while (sc.hasNext()) {
if(test_case_num >= max_test_case) break;
int n = sc.nextInt();
int m = sc.nextInt();
test_case_num++;
solutionMap = new HashMap();
powMap = new HashMap();
BigDecimal result = solveProb(n, m);
System.out.println(result);
//System.out.println(solutionMap.toString());
}
sc.close();
long end = System.currentTimeMillis();
System.out.println( "실행 시간 : " + ( end - start )/1000.0 );
}
private static BigDecimal solveProb(int n, int m) {
BigDecimal result = BigDecimal.ZERO;
for (int i = 1; i < m; i++) {
Stack q = new Stack();
q.add(i);
result = result.add(fnSolution(i, n-1, m, q));
if(result.compareTo(BigDecimal.ZERO) == 0) break;
}
return result;
}
static Map solutionMap = new HashMap();
static Map powMap = new HashMap();
private static BigDecimal fnSolution(int i, int n, int m, Stack q) {
BigDecimal result = BigDecimal.ZERO;
/* i : selected value
* n : remain count
* m
* */
if(i*Math.pow(2, n) <= m){
//다 선택했으면
if(n == 0){
result = result.add(BigDecimal.ONE);
//테스트케이스를 보기위한 sysout
//System.out.println(q.toString());
}else{
for (int j = i*2; j <= m; j++) {
//q.add(j);
BigDecimal solutionInt = solutionMap.get(j+"/"+(n-1));
if(solutionInt == null){
result = result.add(fnSolution(j, n-1, m, q));
}else{
result = result.add(solutionInt);
}
//q.remove(q.size()-1);
}
solutionMap.put(i+"/"+n, result);
}
}
return result;
}
}
'Programming > Algorithm' 카테고리의 다른 글
[DP] 백준 알고리즘 1126번 같은 탑 (1322) | 2016.10.27 |
---|---|
[DP] 백준 알고리즘 1514번 자물쇠 (252) | 2016.10.26 |
[DP] 백준 알고리즘 4781번 사탕가게 (1073) | 2016.10.25 |
[DP] 백준 알고리즘 2011번 암호코드 (984) | 2016.10.24 |
[DP] 백준 알고리즘 1017번 소수 쌍 (972) | 2016.09.19 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Bitnami
- 고운발크림
- s9+
- AWS npm
- 존맛탱
- 10cm
- 수미네 반찬
- 생활코딩
- 부동산거래계약신고필증
- 프리티어
- ES6
- 자금조달계획서
- 웰빙헬스
- 중국어공부
- Axis2
- 노브랜드
- 프렌즈
- 부동신 계약시 주의사항
- AWS nodejs
- 중국어정리
- 뒤꿈치 건조함
- Git
- 마시내 탕수육
- 중국어강의
- 로꼬
- 알고리즘
- 혁오
- 서머너즈워
- GraphQL
- 크러쉬
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함