알고리즘
백준 2705번 팰린드롬 파티션 C++
영춘권의달인
2022. 2. 13. 12:08
1. 자기 자신도 팰린드롬 파티션이기때문에 dp배열을 1로 초기화한다.
2. 0 ~ i - 1 까지 i 에서 뺀 값이 짝수이면 2로 나눠서 양쪽으로 배치해서 팰린드롬 파티션으로 만들 수 있기 때문에
dp[(i - 뺀 값) / 2] 를 dp[i]에 더해준다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<ll> dp(1001, 1);
for (int i = 2; i <= 1000; i++) {
for (int j = 0; j < i; j++) {
if ((i - j) % 2 != 0) continue;
dp[i] += dp[(i - j) / 2];
}
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << dp[n] << "\n";
}
};