【CF758D】 Ability To Convert

题目

题意翻译
题目描述

亚历山大正在学习如何把十进制数字转换成其他进制,但是他不懂英文字母,所以他只是把数值按照十进制数字的方式写出来。这意味着他会用 10 代替英文字母 A。这样,他就会把十进制的 475 转换成十六进制的 11311(475=1·162+13·161+11·16^0)。亚历山大平静的生活着,直到有一天他试着把这些数字转换回十进制数字。

亚历山大记着他总是用较小的数字工作,所以他需要找到在 n 进制的基础下,用他的转换系统得出数字 k 的最小十进制数。 输入输出格式 输入格式:

第一行是一个正整数 n ( 2<=n<=10^9),第二行有一个正整数 k ,满足数字 k中包含不超过 60 个数值。第二行整数中的没每一个数字都严格小于 n。

亚历山大保证答案存在且不超过 10^18,数字 k 不含前导 0. 输出格式:

输出一个数字 x —— 问题的答案。

题目描述
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn’t know English letters, so he writes any number only as a decimal number, it means that instead of the letter A A he will write the number 10 10 . Thus, by converting the number 475 475 from decimal to hexadecimal system, he gets 11311 11311 ( 475=1·16{2}+13·16{1}+11·16^{0} 475=1⋅16
2
+13⋅16
1
+11⋅16
0
). Alexander lived calmly until he tried to convert the number back to the decimal number system.

Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n n he will get the number k k .

输入输出格式
输入格式:
The first line contains the integer n n ( 2<=n<=10^{9} 2<=n<=10
9
). The second line contains the integer k k ( 0<=k<10^{60} 0<=k<10
60
), it is guaranteed that the number k k contains no more than 60 60 symbols. All digits in the second line are strictly less than n n .

Alexander guarantees that the answer exists and does not exceed 10^{18} 10
18
.

The number k k doesn’t contain leading zeros.

输出格式:
Print the number x x ( 0<=x<=10^{18} 0<=x<=10
18
) — the answer to the problem.

输入输出样例
输入样例#1: 复制
13
12
输出样例#1: 复制
12
输入样例#2: 复制
16
11311
输出样例#2: 复制
475
输入样例#3: 复制
20
999
输出样例#3: 复制
3789
输入样例#4: 复制
17
2016
输出样例#4: 复制
594
说明
In the first example 12 12 could be obtained by converting two numbers to the system with base 13 13 : 12=12·13^{0} 12=12⋅13
0
or 15=1·13{1}+2·13{0} 15=1⋅13
1
+2⋅13
0
.

思路

首先会情不自禁向dp去想,但是仔细想想,贪心好像也有一定道理,因为想要转化得到的十进制数最小,因为转进制数的方式是这样的,如样例所示:5=1⋅131+2⋅130

因此转换成n进制的数,所以当最小是当然有每一位最大,这样这个n进制的数的位数可以尽可能短qwq,所以应该没有什么问题,然后就试着写了一份贪心,然后就AC了

代码

#include<iostream>
#include<cstring>
using namespace std;
int n;
string s;
int main(){
    cin>>n;    cin>>s;
    long long ans=0,fcheng=1;
    for(register int i=s.size()-1;i>=0;){
        long long now=0,cheng=1,wei=0,flag=1;
        for(register int j=i;j>=0;){
            if(now+(s[j]-'0')*cheng<n&&now+(s[j]-'0')*cheng>=0){
                now+=(s[j]-'0')*cheng,cheng*=10,j--,wei++;
                if(j<0){
                    ans+=fcheng*now;
                    cout<<ans<<endl;
                    return 0;
                }
            }
            else if(now+(s[j]-'0')*cheng>=n||now+(s[j]-'0')*cheng<0){   
                while(s[j+1]=='0'&&wei!=1)j++,cheng/=10,wei--;     //当超出时,循环向回选,注意选的位数不能退回0位
                i=j,flag=0;
            }
            if(flag==0)break;
        }
        ans+=now*fcheng,fcheng*=n;
    }
    cout<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值