码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces Gym 100338E Numbers

时间:2015-08-21 19:26:23      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

题目: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;
}

 

codeforces Gym 100338E Numbers

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4748635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!