E. Good Triples
E. Good Triples
Given a non-negative integer number (). Let's say a triple of non-negative integers is good if , and , where is the sum of digits of number .
For example, if , then the pair is good, because , and .
Your task is to find the number of good triples for the given number . The order of the numbers in a triple matters. For example, the triples and are two different triples.
Input
The first line of input contains a single integer () — the number of test cases. Descriptions of test cases follow.
The first and only line of the test case contains one integer ().
Output
For each test case output one integer, the number of good triples for the given integer . Order of integers in a triple matters.
Example
input
12
11
0
1
2
3
4
5
3141
999
2718
9999999
10000000
output
9
1
3
6
10
15
21
1350
166375
29160
1522435234375
3
Note
In the first example, the good triples are , , , , , , , , .
In the second example, there is only one good triple .
解题思路
比 G 题难,G 都做出来了这题还不会。
如果没观察出 有进位则必然有 那就真别想做出来了。假设 在十进制下有 个数位,表示成 ,同理将 ,, 用 个数位来表示,不足用前导零补。如果每一个数位都不产生进位,即对于 ,都有 ,那么 就等价于对于每个 都有 。否则如果某一位产生了进位,那么 ,两位数肯定大于一位数,因此必然有 。
为此我们只需单独考虑 ,, 的每一位选哪些数即可,即求 的非负整数解的数量。可以用隔板法,不过隔板法求的是正整数解的数量,只需让 ,, 都加一个偏移量即可,即 ,,,那么等式就变成 ,因此解的数量就是 。因此最终答案就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve() {
int x;
scanf("%d", &x);
LL ret = 1;
while (x) {
int t = x % 10;
x /= 10;
ret *= (t + 2ll) * (t + 1) >> 1;
}
printf("%lld\n", ret);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round 913 (Div. 3) Editorial:https://codeforces.com/blog/entry/123012
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17883699.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效