Codeforces Round #785 (Div. 2) C. Palindrome Basis
You are given a positive integer nn. Let's call some positive integer aa without leading zeroes palindromic if it remains the same after reversing the order of its digits. Find the number of distinct ways to express nn as a sum of positive palindromic integers. Two ways are considered different if the frequency of at least one palindromic integer is different in them. For example, 5=4+15=4+1 and 5=3+1+15=3+1+1 are considered different but 5=3+1+15=3+1+1 and 5=1+3+15=1+3+1 are considered the same.
Formally, you need to find the number of distinct multisets of positive palindromic integers the sum of which is equal to nn.
Since the answer can be quite large, print it modulo 10^9+7.
Input
The first line of input contains a single integer tt (1≤t≤1041≤t≤104) denoting the number of testcases.
Each testcase contains a single line of input containing a single integer nn (1≤n≤4⋅1041≤n≤4⋅104) — the required sum of palindromic integers.
Output
For each testcase, print a single integer denoting the required answer modulo 10^9+7.
Example input
2
5
12
Example output
7
74
题意:把n个整数由没有前导零的回文数组成的种数
动态规划
把所有在数据范围内的回文数记录下来,在对这些数进行完全背包计数
时间复杂度 \(O(n^2)\)
C++ 代码
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 40010,mod = 1e9+7;
int n,cnt = 0,ans = 0;
int a[N];
LL dp[N];
bool check (int x) {
int y = 0;
for (int i = x;i;i /= 10) y = y*10+i%10;
return x == y;
}
int main () {
for (int i = 1;i <= 40000;i++) {
if (check (i)) a[++cnt] = i;
}
dp[0] = 1;
for (int i = 1;i <= cnt;i++) {
for (int j = a[i];j <= 40000;j++) {
dp[j] = (dp[j]+dp[j-a[i]])%mod;
}
}
cin >> n;
while (n--) {
int x;
cin >> x;
cout << dp[x] << endl;
}
return 0;
}

浙公网安备 33010602011771号