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. 42-Remove Nth Node From End of List

    Remove Nth Node From End of List My Submissions QuestionEditorial Solution Total Accepted: 106592 To ...

  2. ubuntu终端颜色快速配置

    ubuntu终端颜色快速配置 根据以下step步骤设置即可 step1:备份:cp ~/.bashrc ~/.bashrc.backup step2:打开文件:vim ~/.bashrc step3: ...

  3. Linux搭建yum仓库

    1.安装nginx 2.为nginx搭建共享目录 3.安装createrepo,创建存储库 4.客户端测试 1.安装nginx yum list |grep nginx #查看是否有可用的nginx包 ...

  4. c#GridView

    分页: 1.先把属性AllowPaging设置为true, 2.pagesize为每一页的行数,PageSize="15". 3.OnPageIndexChanging=" ...

  5. Hive(六)【分区表、分桶表】

    目录 一.分区表 1.本质 2.创建分区表 3.加载数据到分区表 4.查看分区 5.增加分区 6.删除分区 7.二级分区 8.分区表和元数据对应得三种方式 9.动态分区 二.分桶表 1.创建分桶表 2 ...

  6. Oracle——生成Awr报告

    Oracle--生成Awr报告 AWR的概念 Oracle数据库是一个使用量很多的数据库,关于Oracle数据库的性能.Oracle10g以后,Oracle提供了一个性能检测的工具:AWR(Autom ...

  7. 如何在linux 上配置NTP 时间同步?

    故障现象: 有些应用场景,对时间同步的要求严格,需要用到NTP同步,如何在linux上配置NTP时间同步? 解决方案: 在linux 上配置NTP 时间同步,具休操作步骤,整理如下: 1.安装软件包( ...

  8. idea maven 项目 遇到 "Module not specified" 解决方法

    1. 原因:我这边出现的原因是 其他同事在提交代码是 将  这个文件夹也提交了,idea 会加载 .idea 里的配置(即 他的配置),而我的 maven 配置不同,导致出错. 2. 解决方法:删除这 ...

  9. Maven项目打包成war包并启动war包运行

    1 项目打包 1.1 右键点击所需要打包的项目,点击如图所示 Maven clean,这里 Maven 会清除掉之前对这个项目所有的打包信息. 1.2进行完 Maven clean 操作后,在ecli ...

  10. Linux脚本教程

    Linux_Shell_脚本参数接收键盘输入 #!/bin/bash #提示"请输入姓名"并等待30秒,把用户的输入保存入变量name中 read -t 30 -p "请 ...