Codeforces 758D Ability To Convert 【贪心】

来源:互联网 发布:故事版绘制软件 编辑:程序博客网 时间:2024/06/06 02:23

题目链接:http://codeforces.com/contest/758/problem/D


题意:

有一个 n 进制下的数 k,问这个数在10进制下最小是几。

题解:

虽然 k 很大,但是 k 的位数很小,所以我们可以每次贪心的去从末尾取尽可能多的数,注意特判 0 的情况


代码:

#include <cstdio>#include <string>#include <iostream>using namespace std;// Version 3. By DenyTianly// Time: 2017-02-02// Verdict: Accepted typedef long long LL;LL n, base = 1LL, ans;string k;inline void quick_IO() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); }int main() {quick_IO();cin >> n >> k;int now = k.size()-1;while( now >= 0 ) {for ( int i = 0; i <= now; i ++ ) {if(k[i] == '0' && i != now ) continue;LL tot = 0LL;for ( int j = i; j <= now; j ++ ) {tot = tot*10+k[j]-'0';if( tot >= n ) break;}if( tot < n ) {ans += tot*base;base *= n;now = i-1;}}}cout << ans << endl;return 0;}


0 0
原创粉丝点击