Sequence with Digits

朱雀 2023-03-14 08:14 349阅读 0赞

Let’s define the following recurrence : an+1=an+minDigit(an)⋅maxDigit(an).an+1=an+minDigit(an)⋅maxDigit(an).Here minDigit(x)minDigit(x) and maxDigit(x)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 aKaK on a separate line.
Example
input
8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7
ootput
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

  1. #include<stdio.h>
  2. int fmax(int n)
  3. {
  4. int res=n%10;
  5. while(n)
  6. {
  7. if(res<n%10)
  8. {
  9. res=n%10;
  10. }
  11. n=n/10;
  12. }
  13. return res;
  14. }
  15. int fmin(int n)
  16. {
  17. int res=n%10;
  18. while(n)
  19. {
  20. if(res>n%10)
  21. {
  22. res=n%10;
  23. }
  24. n=n/10;
  25. }
  26. return res;
  27. }
  28. int fmn(int n)
  29. {
  30. return n+fmax(n)*fmin(n);
  31. }
  32. int main()
  33. {
  34. int t=0;
  35. scanf("%d",&t);
  36. while(t--)
  37. {
  38. int a1,k=0;
  39. scanf("%d%d",&a1,&k);
  40. for(int i=1;i<k;i++)
  41. {
  42. a1=fmn(a1);
  43. }
  44. printf("%d\n",a1);
  45. }
  46. }

发表评论

表情:
评论列表 (有 0 条评论,349人围观)

还没有评论,来说两句吧...

相关阅读