codeforces Gym 100338E Numbers (贪心,实现)

时间:2023-03-08 17:56:13
codeforces Gym 100338E 	Numbers (贪心,实现)

题目:http://codeforces.com/gym/100338/attachments

贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxbit = ;
ull base[maxbit], n, k; void preDeal()
{
base[] = ;
for(int i = ; i < maxbit; i++){
base[i] = *base[i-];
}
} void ull2str(ull x,string& s)
{
int st[maxbit],top = ;
while(x){
st[top++] = x%+'';
x /= ;
}
reverse(st,st+top);
s.assign(st,st+top);
} void work()
{
priority_queue<string,vector<string>,greater<string> > q;
int sz = maxbit-;
while(base[sz]>n) sz--;
string str;
for(int i = ; i <= sz; i++){
ull cur = base[i];
ull r = cur%k;
if(r != ){
cur += k-r;
}
if(cur <= n){
ull2str(cur,str);
q.push(str);
}
}
printf("%s\n",q.top().c_str());
} int main()
{
freopen("numbers.in","r",stdin);
freopen("numbers.out","w",stdout);
preDeal();
while(scanf("%I64u%I64u",&n,&k),n){
work();
}
return ;
}