日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Sequence with Digits CodeForces - 1355A(暴力+数学)

發布時間:2023/12/4 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sequence with Digits CodeForces - 1355A(暴力+数学) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

定義: an+1=an+minDigit(an)×maxDigit(an)。

給定 a1 和 k,求 ak ?

題目:

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

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

分析:

顯然當 an 中包含了 0 時,之后的數都等于 an。那么我們就要判斷是否一定會出現包含 0 的情況,并且復雜度是可以接受的。想到只要出現0那就美滋滋,畢竟后面怎么加都是0,就不用看了。而0在1000次以內一定會出現,這個其實是有官方證明的,但是我更傾向于打表然后找找規律。
??首先 minDigit(x)×maxDigit(x)≤81。那么令 t=x mod 1000,那么 t<1000,那么在經過遞增過后,一定會出現 t+m≥1000,由于 m≤81,那么 t+m<1100,也就是一千零幾,那么他的百位一定是 0。所以我們是可以直接循環去解決。

AC代碼

#include <cstring> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; ll dfs(ll x) {ll m1=10,m2=0;while(x>0){ll y=x%10;x/=10;m1=min(m1,y);m2=max(m2,y);}return m1*m2; } int main() {int t;scanf("%d",&t);while(t--){ll a,k;scanf("%lld%lld",&a,&k);k--;while(k--){ll y=dfs(a);if(y==0)break;a+=y;}printf("%lld\n",a);} }

總結

以上是生活随笔為你收集整理的Sequence with Digits CodeForces - 1355A(暴力+数学)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。