IT 정보/알고리즘(백준, BOJ)

[백준-BOJ] 2410

Dalyoung 2021. 2. 6. 22:16
728x90
반응형

www.acmicpc.net/problem/2410

 

2410번: 2의 멱수의 합

첫째 줄에 경우의 수를 출력한다. 답이 커질 수 있으므로 1,000,000,000으로 나눈 나머지를 출력한다.

www.acmicpc.net

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
 
public class Main {
    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.doit();
    }
     
     
     
    public void doit() throws IOException{
      //  System.setIn(new FileInputStream("input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int K = Integer.toBinaryString(N).length();
        int c[] = new int[K+1];
        long dp[] = new long[N+1];
        dp[0] = 1;
        c[1] = 1;
        int D = 1000000000;
        for(int i = 2; i <= K; i++){
        	c[i] = 2 * c[i - 1];
        }
        
//        System.out.println(Arrays.toString(c));
        
        for(int i = 1; i <= K; i++){
        	for(int j = c[i]; j <= N; j++){
        		
        		dp[j] = dp[j] % D + dp[j - c[i]] % D;
        		if(dp[j] > D){
        			dp[j] %= D;
        		}
        	}
        }
//        System.out.println(Arrays.toString(dp));
        System.out.println(dp[N]);
        br.close();
    }
}
728x90
반응형

'IT 정보 > 알고리즘(백준, BOJ)' 카테고리의 다른 글

[백준-BOJ] 2569  (0) 2021.02.07
[백준-BOJ] 2490  (0) 2021.02.07
[백준-BOJ] 2357  (0) 2021.02.06
[백준-BOJ] 2352  (0) 2021.02.06
[백준-BOJ] 2302  (0) 2021.02.06