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个整数由没有前导零的回文数组成的种数
动态规划
把所有在数据范围内的回文数记录下来,在对这些数进行完全背包计数
时间复杂度
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现