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. .net与java建立WebService再互相调用

    A: .net建立WebService,在java中调用. 1.在vs中新建web 简单修改一下Service.cs的[WebMethod]代码: using System; using System ...

  2. Hadoop运行jar包报错java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1

    错误信息: java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1    at org.apache.hadoop.mapre ...

  3. 学习java的第十六天

    一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...

  4. abundant

    In ecology [生态学], local abundance is the relative representation of a species in a particular ecosys ...

  5. 零基础学习java------day6----数组

    0. 内容概览 补充:main方法中的数组 1. 数组的概述 概念: 用来存储一组相同数据类型的集合(或者叫容器) 注意事项: 1. 数组中的元素类型必须一致 2. 数组本身是引用数据类型,但是里面的 ...

  6. 【leetcode】1217. Minimum Cost to Move Chips to The Same Position

    We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...

  7. C++构造函数和析构函数初步认识(2)

    构造函数的三个作用1.构造对象2.对象初始化3.类型转换  //Test1.h #include<iostream> using namespace std; //构造对象 //初始化对象 ...

  8. GO 总章

    GO 学习资源 go 代理 GO 语言结构 GO 数字运算 GO 时间处理 GO 定时器 GO 异常处理 go recover让崩溃的程序继续执行 GO Exit Fatal panic GO 通过进 ...

  9. 银联acp手机支付总结

    总结: 1.手机调用后台服务端接口,获取银联返回的流水号tn 银联支付是请求后台,后台向银联下单,返回交易流水号,然后返回给用户,用户通过这个交易流水号,向银联发送请求,获取订单信息,然后再填写银行卡 ...

  10. Flask + Nginx + uwsgi 部署过程

    一.安装Flask 1.itsdangerous tar xvf itsdangerous-0.23.tar.gz cd itsdangerous-0.23/ python setup.py inst ...