CodeForces 1355 A. Sequence with Digits#
目录#
1. 题目描述#
1.1. Limit#
Time Limit: 2 seconds
Memory Limit: 256 megabytes
1.2. Problem Description#
Let's define the following recurrence:
Here and are the minimal and maximal digits in the decimal representation of without leading zeroes. For examples refer to notes.
Your task is calculate for given and .
1.3. Input#
The first line contains one integer () — the number of independent test cases.
Each test case consists of a single line containing two integers and (, ) separated by a space.
1.4. Output#
For each test case print one integer on a separate line.
1.5. Sample Input#
8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7
1.6. Sample Output#
42
487
519
528
544
564
588
628
1.7. Note#
1.8. Source#
CodeForces 1355 A. Sequence with Digits
2. 解读#
直接根据公式进行计算
但是考虑到数据范围 (, ),不进行剪枝肯定会超时。
我们可以注意到一个规律,就是当 时,,也就是说,当我们在数字中找到了一个 ,那么就不再需要进行计算,直接返回 即可。
3. 代码#
#include <algorithm>
#include <iostream>
using namespace std;
long long n, k;
// 计算结果
long long calculate(long long x)
{
long long minN = 10, maxX = 0;
while (x) {
// 若找到0,则返回
if (x % 10 == 0) {
return 0;
} else {
minN = min(x % 10, minN);
maxX = max(x % 10, maxX);
}
x /= 10;
}
return minN * maxX;
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
scanf("%lld %lld", &n, &k);
while (--k) {
if (calculate(n) == 0) {
break;
} else {
n += calculate(n);
}
}
printf("%lld\n", n);
}
}
联系邮箱:curren_wong@163.com
CSDN:https://me.csdn.net/qq_41729780
知乎:https://zhuanlan.zhihu.com/c_1225417532351741952
公众号:复杂网络与机器学习
欢迎关注/转载,有问题欢迎通过邮箱交流。