codeforce A - Sequence with Digits

鏈接
已知遞推公式,告訴a1,和k,求ak
在這裏插入圖片描述
每個數字各位中最大的數字,和最小的數字相乘。
比如513,minDigit(513)=1,maxDigit(513)=5
數據量
在這裏插入圖片描述
暴力long long最大9×10189\times10^{18}!!!

取出一個數字的每一位取餘10再除10.

long long Max(long long x){
	while(x){
	ll a=-1;
	 a=max(a,x%10);//最低位
	 x/=10;//抹掉最低位
	 if(a==9) break;	
	}
	return a;
}
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

typedef long long ll;

ll Max(ll x){
	ll a=-1;
	while(x){
		a=max(a,x%10);
		x/=10;
		if(a==9) break;//剪枝
	}
	return a;
}

ll Min(ll x){
	ll a=11;
	while(x){
		a=min(a,x%10);
		x/=10;
		if(a==0) break;//剪枝
	}
	return a;
}

int main(){
	ll t,n,k;
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(int i=1;i<k;i++){
			if(Min(n)==0) break;//剪枝
			n+=Max(n)*Min(n);
		}
		cout<<n<<endl;
	
		
		
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章