Sequence with Digits CodeForces - 1355A

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 xx without leading zeroes. For examples refer to notes.

Your task is calculate aKaK for given a1a1 and KK .

Input

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

Each test case consists of a single line containing two integers a1a1 and KK (1≤a1≤10181≤a1≤1018 , 1≤K≤10161≤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

Output

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

题意:

给出a1和k,an=a(n-1)+(a(n-1)每个位数中的最小值乘上a(n-1)每个位数中的最大值),求ak;

思路:

简单的暴力模拟,但是我们需要注意到一点就可以节约时间;

就是如果在不断的公式递推中得到ai的某一个位数为零的话,往后都为这一个值,方可直接退出,进而节省时间。

代码如下:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int t;
long long n,k;
long long a[50];
long long judge(long long n)//对于当前的数进而得出各个位数的最大值和最小值的乘积。
{
    memset(a,0,sizeof(a));
    int t=0;
    int mi=10;
    int ma=0;
    while(n!=0)
    {
        int k=n%10;
        mi=min(mi,k);
        ma=max(ma,k);
        a[t++]=k;
        n/=10;
    }
    return mi*ma;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(int i=1; i<k; i++)
        {
            if(judge(n)==0)//如果返回为零,这证明这个数的某一个位的数为0,所以直接跳出循环,以后结果就是当前结果了。
                break;
            n+=judge(n);
        }
        cout<<n<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值