https://www.acmicpc.net/problem/2758
package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
// ๋ก๋
public class BJ2758 {
static int t;
static int n;
static int m;
static long[][] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(br.readLine());
StringTokenizer st;
for(int i = 0; i < t; i++) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
dp = new long[n+1][m+1];
solution();
}
}
private static void solution() {
for(int i = 1; i <= m; i++) {
dp[1][i] = 1;
}
for(int i = 2; i <= n; i++) {
for(int j = 2; j <= m; j++) {
if(j%2 == 0) {
dp[i][j] = dp[i][j-1] + dp[i-1][j/2];
}
else {
dp[i][j] = dp[i][j-1];
}
}
}
long cnt = 0;
int start = (int)Math.pow(2, n-1); // n๋ฒ์งธ ๊ณ ๋ฅด๋ ์ ์ค ๊ฐ์ฅ ์์ ๊ฐ
for(int i = start; i <= m; i++) {
cnt += dp[n][i];
}
System.out.println(cnt);
}
}
package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
// ๋ก๋
/* Back Tracking์ผ๋ก ๊ตฌํํ๋ฉด ๋ฐฑ์ค์์๋ ์๊ฐ ์ด๊ณผ ๋ฐ์
DP๋ก ํด๊ฒฐํ์์ */
public class BJ2758_2 {
static int t;
static int n;
static int m;
static int cnt;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(br.readLine());
StringTokenizer st;
for(int i = 0; i < t; i++) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
cnt = 0;
backTracking(0, 0);
System.out.println(cnt);
}
}
private static void backTracking(int depth, int idx) {
if(depth == n) {
cnt++;
return;
}
if(idx * 2 <= m) { // isPromising() ์กฐ๊ฑด
for(int i = idx*2; i <= m; i++) {
if(depth == 0 && i == 0) continue;
backTracking(depth + 1, i);
}
}
else return;
}
}
์์ด๋์ด
ํ๋ ธ์ต๋๋ค