首页 > 其他 > 详细

codeforces Gym 100338E Numbers

时间:2015-08-21 19:25:07      阅读:187      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!