http://codeforces.com/contest/758/problem/D

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 k contains 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.

题意:在N进制下给你一个数,要你转换成最小的十进制数;

思路:贪心;

从后往前低位贪心选取的数尽量大。

要注意数字中有0的时候,如果不能和后面加前面不为0的数组在一起,将后面先组起来,然后在判断当前0时候能和前面不为0的组,不能单独成一位。

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long LL;
4 char a[100];
5 LL quick(LL n,LL m);
6 LL ac[100];
7 int main(void)
8 {
9 LL n;
10 scanf("%lld",&n);
11 scanf("%s",a);
12 int l = strlen(a);
13 LL sum = 0;
14 LL ak = 1;
15 if(a[0] == '0')
16 printf("0\n");
17 else
18 {int cn = 0;
19 for(int i = l-1; i >= 0;)
20 {
21 if(a[i]!='0')
22 {
23 LL app = sum + (LL)(a[i]-'0')*ak;
24 if(app >= n)
25 {
26 ac[cn++] = sum;
27 ak = 1;
28 sum = 0;
29 }
30 else
31 {
32 sum = app;
33 ak*=10; i--;
34 }
35 }
36 else if(a[i] == '0')
37 {
38 LL app = sum;
39 int j;
40 for( j = i-1; j >= 0; j--)
41 {
42 if(a[j]!='0')
43 break;
44 }
45 int s;
46 for( s = i; s >= j; s--)
47 {if(ak >= n)break;
48 app = app+ak*(LL)(a[s]-'0');
49 ak*=10;
50 }
51 if(app < n&& s == j-1)
52 {
53 sum = app;
54 i = j-1;
55 }
56 else
57 {
58 ac[cn++] = sum;
59 if(sum == 0)
60 i--;
61 sum = 0;
62 ak = 1;
63 }
64 }
65 }
66 ac[cn++] = sum;
67 sum = 1;
68 LL ask = 0;
69 for(int i = cn-1;i >= 0;i--)
70 {
71 ask *= n;
72 ask += ac[i];
73 }
74 printf("%lld\n",ask);}
75 return 0;
76 }

D. Ability To Convert的更多相关文章

  1. 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 ...

  2. Codeforces Round #392 (Div. 2)-D. Ability To Convert

    D - Ability To Convert 题目大意:给你一个数字 n 接下来再输入一个数字 w(<10^60),表示w这个数字是 n 进制的, 并且超过十进制也用数字表示,这样就有多种组合了 ...

  3. Codeforces758D Ability To Convert 2017-01-20 10:29 231人阅读 评论(0) 收藏

    D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. 【动态规划】Codeforces Round #392 (Div. 2) D. Ability To Convert

    D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. CodeForces 758 D Ability To Convert

    Ability To Convert 题意:给你一个n进制的60位的数,但是由于Alexander只会写0->9,所以他就会用10来表示十而不是A(假设进制>10); 题解:模拟就好了,先 ...

  6. codeforces 758D Ability To Convert【DP】

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

  7. CF758 D. Ability To Convert 细节处理字符串

    link 题意:给定进制数n及一串数字,问在此进制下这串数能看成最小的数(10进制)是多少(如HEX下 1|13|11 = 475) 思路:此题要仔细思考细节.首先要想使数最小那么必定有个想法是使低位 ...

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

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

  9. 【codeforces 758D】Ability To Convert

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

随机推荐

  1. RabbitMQ消息中介之Python使用

    本文介绍RabbitMQ在python下的基本使用 1. RabbitMQ安装,安装RabbitMQ需要预安装erlang语言,Windows直接下载双击安装即可 RabbitMQ下载地址:http: ...

  2. Python3调用C程序(超详解)

    Python3调用C程序(超详解) Python为什么要调用C? 1.要提高代码的运算速度,C比Python快50倍以上 2.对于C语言里很多传统类库,不想用Python重写,想对从内存到文件接口这样 ...

  3. 同一局域网,远程连接别人的Mysql数据库

    数据库:MySQL 工具: Navicat, 电脑A连接电脑B的数据库, 确保两部电脑都是在同一个局域网,都是连着同一个路由器,或者连接同一个WiFi, 如果不确定是否为同一个局域网,可以打开cmd, ...

  4. Redis | 第10章 二进制数组、慢查询日志和监视器《Redis设计与实现》

    目录 前言 1. 二进制位数组 1.1 位数组的表示 1.2 GETBIT 命令的实现 1.3 SETBIT 命令的实现 1.4 BITECOUNT 命令的实现 1.5 BITOP 命令的实现 2. ...

  5. C++中的排序

    下面网站解释比较好 http://www.cnblogs.com/heyonggang/archive/2013/11/03/3404371.html 1. qsort(C中的函数加上stdlib.h ...

  6. 生产调优3 HDFS-多目录配置

    目录 HDFS-多目录配置 NameNode多目录配置 1.修改hdfs-site.xml 2.格式化NameNode DataNode多目录配置(重要) 1.修改hdfs-site.xml 2.测试 ...

  7. 【Maven实战技巧】「插件使用专题」Maven-Archetype插件创建自定义maven项目骨架

    技术推荐 自定义Archetype Maven骨架/以当前项目为模板创建maven骨架,可以参考http://maven.apache.org/archetype/maven-archetype-pl ...

  8. pow()是如何实现的?

    如1.5 ** 2.5,如何计算?似乎是这样的: 1. cmath calculates pow(a,b) by performing exp(b * log(a)). stackoverflow 2 ...

  9. Spark(二)【sc.textfile的分区策略源码分析】

    sparkcontext.textFile()返回的是HadoopRDD! 关于HadoopRDD的官方介绍,使用的是旧版的hadoop api ctrl+F12搜索 HadoopRDD的getPar ...

  10. APK 反编译以及遇到的问题

    APK反编译: https://www.cnblogs.com/geeksongs/p/10864200.html 遇到的问题 https://www.jianshu.com/p/55bf5f688e ...