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. LATEX公式语法

    see how any formula was written in any question or answer, including this one, right-click on the ex ...

  2. react native安装遇到的问题

    最近学习react native 是在为全栈工程师而努力,看网上把react native说的各种好,忍不住学习了一把.总体感觉还可以,特别是可以开发android和ios这点非常厉害,刚开始入门需要 ...

  3. A Child's History of England.7

    After the death of Ethelbert, Edwin, King of Northumbria [公元616年,隋朝末年], who was such a good king tha ...

  4. Java偏向锁浅析

    偏向锁的定义 顾名思义,偏向锁会偏向第一个访问锁的线程. 如果在接下来的运行过程中,该锁没有被其他线程访问,这持有偏向锁的线程将永远不需要同步 如果在运行过程中,遇到了其他线程抢占锁,则持有偏向锁的线 ...

  5. Android 高级UI组件(三)

    一.popupWindow 1.AlertDialog和PopupWindow最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManage ...

  6. Initialization of data members

    In C++, class variables are initialized in the same order as they appear in the class declaration. C ...

  7. java对象分配

    1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...

  8. JDBC(3):PreparedStatement对象介绍

    一,PreparedStatement介绍 PreperedStatement是Statement的子类,它的实例对象可以通过Connection.preparedStatement()方法获得,相对 ...

  9. Dubbo提供者的异步执行

    从前面"对提供者的异步调用"例子可以看出,消费者对提供者实现了异步调用,消费者线程的执行过程不再发生阻塞,但提供者对IO耗时操作仍采用的是同步调用,即IO操作仍会阻塞Dubbo的提 ...

  10. spring boot @EnableWebMvc禁用springMvc自动配置原理。

    说明: 在spring boot中如果定义了自己的java配置文件,并且在文件上使用了@EnableWebMvc 注解,那么sprig boot 的默认配置就会失效.如默认的静态文件配置路径:&quo ...