题目: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 = 19; ull base[maxbit], n, k; void preDeal() { base[0] = 1; for(int i = 1; i < maxbit; i++){ base[i] = 10*base[i-1]; } } void ull2str(ull x,string& s) { int st[maxbit],top = 0; while(x){ st[top++] = x%10+‘0‘; x /= 10; } reverse(st,st+top); s.assign(st,st+top); } void work() { priority_queue<string,vector<string>,greater<string> > q; int sz = maxbit-1; while(base[sz]>n) sz--; string str; for(int i = 0; i <= sz; i++){ ull cur = base[i]; ull r = cur%k; if(r != 0){ 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 0; }
时间: 2024-11-08 23:54:36