传送门: https://codeforces.ml/contest/1673/problem/C

参考题解:https://www.cnblogs.com/yaqu-qxyq/p/16215170.html

题意:

给t个正整数n,(n<=4e4)求n由若干个回文数相加得到有多少种组合。

思路:

首先求出4e4以内的回文数,发现只有500个。

有dp转移方程:
\(dp_{i,j}=dp_{i-hw_j,j}+dp_{i,j-1}\)
代表对于正整数i只由前j种回文数组成的情况总数。(就是完全背包)

#include<bits/stdc++.h> 
const int N = 4e4 + 10;
const int mo = 1e9 + 7;
#define int long long
std::vector<int>ve;//储存回文数
bool hw(int x) {//求回文
	int xx = x;
	int y = 0;
	while (xx) {
		y *= 10;
		y += xx % 10;
		xx /= 10;
	}
	return y == x;
}
long long dp[N][510];
void init() {
	for (int i = 0; i < 5e4 + 10; i++) {
		if (hw(i))ve.push_back(i);
	}
	for (int i = 1; i <= 5e2; i++)dp[0][i] = 1;
	for (int i = 1; i <= 4e4; i++) {
		for (int j = 1; j <= 500; j++) {
			if (i >= ve[j])dp[i][j] = dp[i][j - 1] + dp[i - ve[j]][j];
			else dp[i][j] = dp[i][j - 1];
			dp[i][j] %= mo;
		}
	}
}
signed main() {
	init();
	int t; std::cin >> t;
	while (t--) {
		int n; std::cin >> n;
		std::cout << dp[n][499] % mo << std::endl;
	}
}
posted on 2022-07-14 16:14  wtn135687  阅读(30)  评论(0)    收藏  举报