D. Ability To Convert
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·162 + 13·161 + 11·160). 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 he will get the number k.

Input

The first line contains the integer n (2 ≤ n ≤ 109). The second line contains the integer k (0 ≤ k < 1060), it is guaranteed that the number kcontains no more than 60 symbols. All digits in the second line are strictly less than n.

Alexander guarantees that the answer exists and does not exceed 1018.

The number k doesn't contain leading zeros.

Output

Print the number x (0 ≤ x ≤ 1018) — the answer to the problem.

Examples
input
13
12
output
12
input
16
11311
output
475
input
20
999
output
3789
input
17
2016
output
594
Note

In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·130 or 15 = 1·131 + 2·130.

这题的题意是第一行输入一个进制,第二行输入该进制下的数,输出将该数转化为十进制的最小值。

( 有零的地方一直都处理不好- -,后来看了别人的代码才学会。

思路是用字符串读入,从后往前加,如果大于输入的进制了,就跳出再判断含有0的情况,直到循环到起始。

 1 #include <iostream>
2 #define ll long long
3 using namespace std;
4 string s;
5 int main()
6 {
7 ll b;
8 cin>>b>>s;
9 ll ans = 0;
10 ll p = 1;
11 ll len = s.length() - 1;
12 while(len >= 0) {
13 ll sum = 0;
14 ll t = 1;
15 ll i;
16 for( i = len; i >= 0; i--) {
17 if(max((s[i] - '0'),1) * t + sum >= b)
18 break;
19 sum += (s[i] - '0') * t;
20 t *= 10;
21 // cout<<sum<<endl;
22 }
23 while(i < len - 1) { //每位至少有一个数,所以每次大的循环至少会让最后一位数
24 if(s[i+1] == '0') { //加入ans中,因此遍历到倒数第二位之前就行了
25 i++;
26 }
27 else break;
28 }
29 ans += sum * p;
30 // cout<<ans<<endl;
31 p *= b;
32 len = i;
33 }
34 cout << ans << endl;
35 return 0;
36 }

codeforces 758D的更多相关文章

  1. Codeforces 758D Ability To Convert(区间DP)

    题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...

  2. 【codeforces 758D】Ability To Convert

    [题目链接]:http://codeforces.com/contest/758/problem/D [题意] 给你一个n进制的数k; 问你它可能的最小的十进制数是多少; [题解] 从右往左; 获取数 ...

  3. Codeforces 758D:Ability To Convert(思维+模拟)

    http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...

  4. codeforces 758D Ability To Convert【DP】

    在N进制下给你一个数,要你转换成最小的十进制数; 状态转移方程:从前向后 dp[j]表示j位前数列的最小十进制数 dp[j]=min(dp[j],dp[i]*n+x) 程序: #include < ...

  5. Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

    D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...

  6. Codeforces Round #392(div 2) 758D (贪心)

    orz 最近被水题卡+FST,各种掉rating 题目大意 一个数s它是n进制的,但是每一位不是用'A','B'....表示的,而是用10,11等等表示的,将它还原成十进制 这种表示方法显然会产生多解 ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. ElasticSearch极简入门总结

    一,目录 安装es 项目添加maven依赖 es客户端组件注入到spring容器中 es与mysql表结构对比 索引的删除创建 文档的crud es能快速搜索的核心-倒排索引 基于倒排索引的精确搜索. ...

  2. 如何创建一个Java项目

    目录 新建项目 项目信息配置 创建Java类 编译和运行 新建项目 首先双击eclipse进入到eclipse页面. 菜单"File"下的"New"里" ...

  3. Bitter.Core系列八:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore 之 事务

    Bitter.Core 编写事务相当简单,Bitter.Core 尽可能的将代码编写量降为最低,例外一方方面保证客户主观能控制代码.Bitter.Core 事务提交,支持Builkcopy事务,原生事 ...

  4. Mac下IDEA激活Jrebel

    第一步:在idea中下载jrebel,过程省略 第二步:配置反向代理工具 Windows 版:http://blog.lanyus.com/archives/317.html MAC 版: 安装hom ...

  5. hive报错:Failed with exception java.io.IOException: rename for src path:

    在hive中,会有这样一种情形: 1.创建一个分区外部表A(比如A表有5个字段),并且向A表里指定的分区(比如20160928这个分区)里插入数据 2.发现A表缺少一些字段,因为存在元数据不实时更新的 ...

  6. InnoDB 事务隔离级探索

    https://mp.weixin.qq.com/s/gWYL2Th9Go5LDhkyGB_rYQ

  7. file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes 禁止输出 屏蔽 stdout 和 stderr 输入输出重定向 重定向文件描述符

    movie.mpeg.001 movie.mpeg.002 movie.mpeg.003 ... movie.mpeg.099   $cat movie.mpeg.0*>movie.mpeg   ...

  8. call by value reference name python既不是按值传递也不是按引用传递 python复制原理 创建新对象 与 改变原对象

    按名调用 Algol 按值调用 Java https://docs.python.org/3.6/faq/programming.html#how-do-i-write-a-function-with ...

  9. JAXB学习(一):概述

    pre.XML { background-color: rgba(255, 204, 204, 1); padding-left: 25px } JAXB是 Java Architecture for ...

  10. 本地代码上传GitHub

    0. 登录 git config --global user.name "GitHub用户名" git config --global user.email "GitHu ...