Codeforces Round #643 (Div. 2) A. Sequence with Digits 瞎模拟

A. Sequence with Digits
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s define the following recurrence:
an+1=an+minDigit(an)⋅maxDigit(an).
Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.

Your task is calculate aK for given a1 and K.

Input
The first line contains one integer t (1≤t≤1000) — the number of independent test cases.

Each test case consists of a single line containing two integers a1 and K (1≤a1≤1018, 1≤K≤1016) separated by a space.

Output
For each test case print one integer aK on a separate line.

Example
inputCopy
8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7
outputCopy
42
487
519
528
544
564
588
628
Note
a1=487

a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519

a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528

a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544

a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564

a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588

a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628

给定一个数 N N N,定义操作 o p op op
op:
N = N + m i n ( N ) m a x ( N ) N=N+min(N的每一位数字)*max(N的每一位数字) N=N+min(N)max(N)
可以发现,多次操作后 N N N可能出现某一位出现 0 0 0,所以,min(N的每一位数字)*max(N的每一位数字)为0,即:出现0后无论怎么操作 N N N都不会改变
所以,执行 K K K次模拟,当 N N N出现0的时候break掉

//#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
 
 
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define int long long int
#define QAQ (0)
 
using namespace std;
 
#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
#endif
 
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
 
int n, m, Q, K;
 
int minD(int x) {
	int ret = 99;
	while(x) {
		ret = min(ret, x%10);
		x /= 10;
	}
	return ret;
}
 
int maxD(int x) {
	int ret = 0;
	while(x) {
		ret = max(ret, x%10);
		x /= 10;
	}
	return ret;
}
 
int calc() {
	while(--K) {
		int tmin = minD(n), tmax = maxD(n);
// show(n, tmin, tmax);
		if(!tmin) break;
		n = n + (tmin * tmax);
	}
	return n;
}
 
signed main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%lld ", &Q);
	while(Q--) {
		scanf("%lld %lld ", &n, &K);
		int ans = calc();
		printf("%lld\n", ans);
	}
 
 
 
#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}
 


全部评论

相关推荐

头像
05-12 09:14
点赞 评论 收藏
转发
头像
不愿透露姓名的神秘牛友
04-29 12:10
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务