題目鏈接:https://codeforces.ml/contest/1373/problem/E
題目大意:
定義一個f(x)爲,x的數位和。
給出n,k,求最小的x使得f(x) + f(x+1) + f(x+k) == n
題目思路:
非常好的構造思路(看了榜一巨巨的
雖然思路和我略像,但我實現不出來,看完之後恍然大悟
首先確定一個性質:
對於任何一個數字來說,想要數位和最大的情況下最小,肯定會組成x9999的形式
根據這個性質我們去暴力構造這個數字
枚舉所有的個位數字,所以答案一定爲x999x的形式
所以變的只是中間有多少個9
所以枚舉所有個位數字+枚舉中間的9
這樣就可以求出去除個位和中間的9的貢獻,此時需要加上共有部分,所以剩下的數位和一定可以整除k+1
整除k+1
剩下之後首先看那個數字是否大於8,大於8首先把8放上
之後按照貪心繼續9就可以了
此題的貪心性質:是在於要明白全選9會縮短數字長度,而在數字比較中長度是非常重要的
Code:
/*** keep hungry and calm CoolGuang!***/
#include <bits/stdc++.h>
#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF=2e18;
const int maxn=1e6+6;
const int mod=1000000007 ;
const double eps=1e-15;
inline bool read(ll &num)
{char in;bool IsN=false;
in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
int s = 0;
ll num[maxn],dp[maxn];
ll F[maxn],b[maxn],a[maxn];
string cmp(string a,string b){
if(a=="") return b;
if(b=="") return a;
if(a.size()<b.size()) return a;
if(b.size()<a.size()) return b;
if(a<b) return a;
return b;
}
int main()
{
int T;scanf("%d",&T);
while(T--){
read(n);read(m);
string ans = "";
for(int last = 0;last<=9;last++){
for(int pre=0;pre<=19;pre++){
string res;
res += last + '0';
for(int i=1;i<=pre;i++) res += '9';
ll temp = n,cot = 0;
for(int i=0;i<=m;i++){
temp -= (i+last)%10;
if(i+last > 9) cot++;
}
temp -= (m+1-cot)*9*pre;
temp -= cot;
if(temp < 0) continue;
if(temp%(m+1) != 0) continue;
temp/=m+1;
if(temp>8){
res += '8';
temp -= 8;
}
while(temp>0){
res += '0'+min(temp,9ll);
temp -= 9;
}
reverse(res.begin(),res.end());
ans =cmp(ans,res);
}
}
if(ans=="") printf("-1\n");
else cout<<ans<<endl;
}
return 0;
}
/**
**/