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.

就f(i,j)表示将前i个数字划分为j位的最小值,状态转移方程看代码。

要判断是否会爆long long,我取对数判的。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define INF 1000000000000000000ll
#define EPS 0.00000001
typedef long long ll;
ll n;
char a[70];
ll f[70][70];
int m;
int main()
{
// freopen("d.in","r",stdin);
scanf("%I64d%s",&n,a+1);
m=strlen(a+1);
for(int i=1;i<=m;++i)
for(int j=1;j<=i;++j)
f[i][j]=INF;
for(int i=1;i<=m;++i)
for(int j=1;j<=i;++j)
{
ll now=0,base=1;
for(int k=1;i-k>=j-1;++k)
{
now+=((ll)(a[i-k+1]-'0')*base);
if(base>=n || now>=n)
break;
base*=10ll;
if(j==1 && k!=i)
continue;
if(!(a[i-k+1]=='0' && k!=1))
{
if(log(f[i-k][j-1])+log(n)-log(INF-now)<=EPS)
f[i][j]=min(f[i][j],f[i-k][j-1]*n+now);
}
}
}
printf("%I64d\n",*min_element(f[m]+1,f[m]+m+1));
return 0;
}

【动态规划】Codeforces Round #392 (Div. 2) 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. Codeforces Round #392 (div.2) E:Broken Tree

    orz一开始想不画图做这个题(然后脑袋就炸了,思维能力有待提高) 我的做法是动态规划+贪心+构造 首先把题目给的树变成一个可行的情况,同时weight最小 这个可以通过动态规划解决 dp[x]表示以x ...

  4. Codeforces Round #392 (Div. 2) F. Geometrical Progression

    原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...

  5. Virtual Codeforces Round #392 (Div. 2)

    下午闲来无事开了一场Virtual participation 2h就过了3道水题...又跪了..这只是Div. 2啊!!! 感觉这次直接就是跪在了读题上,T1,T2读题太慢,T3还把题读错了 要是让 ...

  6. Codeforces Round #392 (Div. 2) - C

    题目链接:http://codeforces.com/contest/758/problem/C 题意:给定N*M矩阵的教室,每个位置都有一个学生,Sergei坐在[X,Y],然后老师会问K个问题,对 ...

  7. Codeforces Round #392 (Div. 2) - B

    题目链接:http://codeforces.com/contest/758/problem/B 题意:给定n个点灯的情况,灯只有四种颜色RBGY,然后如果某个灯坏了则用'!'表示,现在要求将坏的灯( ...

  8. Codeforces Round #392 (Div. 2) - A

    题目链接:http://codeforces.com/contest/758/problem/A 题意:给定N个城市的福利,国王现在想让每个城市的福利都一致.问最少需要花多少钱使得N个城市的福利值都一 ...

  9. Codeforces Round #392 (Div. 2)

    D题,给出n,k,k是n进制数,但是大于十进制时,它的表示方法仍为十进制那种,比如16进制下的15,我们可以看成就是15,或者1|5,也就是1×16+5 = 21,让你求出能表达的最小十进制数 从后面 ...

随机推荐

  1. 异常message:There is no database named cloudera_manager_metastore_canary_test_db_hive_hivemetastore

    NoSuchObjectException(message:There is no database named cloudera_manager_metastore_canary_test_db_h ...

  2. iOS 单元测试(Unit Test 和 UI Test)

    之前一直搞过~~最近试了一下下,完美~~ 附上一篇文章,不同的伙伴可以看看: http://www.jianshu.com/p/009844a0b9edUnitTest(简单的单元测试使用) http ...

  3. javascript继承有5种实现方式

    1.对象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(this.user ...

  4. C语言编译各过程

    1.预处理 此阶段主要完成#符号后面的各项内容到源文件的替换,往往一些莫名其妙的错误都是出现在头文件中的,要在工程中注意积累一些错误知识. (1).#ifdef等内容,完成条件编译内容的替换 (2). ...

  5. 【CF1027D】Mouse Hunt(拓扑排序,环)

    题意:给定n个房间,有一只老鼠可能从其中的任意一个出现, 在第i个房间设置捕鼠夹的代价是a[i],若老鼠当前在i号房间则下一秒会移动到b[i]号, 问一定能抓住老鼠的最小的总代价 n<=2e5, ...

  6. 【洛谷 P3805】 【模板】manacher算法

    题目链接 manacher算法:在线性时间内求一个字符串中所有/最长回文串的算法. 先来考虑一下暴力的算法,枚举每个中点,向两边扩展,时间复杂度\(O(n^2)\). 来分析下此算法的缺点. 1.因为 ...

  7. 【BZOJ 3907】网格(Catalan数)

    题目链接 这个题推导公式跟\(Catalan\)数是一样的,可得解为\(C_{n+m}^n-C_{n+m}^{n+1}\) 然后套组合数公式\(C_n^m=\frac{n!}{m!(n-m)!}\) ...

  8. RabbitMQ消息队列(三): 发布/订阅

    1. 订阅/发布: 前面worker示例中的每个任务都是只发送给某一个worker,如果我们多个worker都需要接收处理同一个任务,此时就要使用 订阅/发布功能,比如,日志模块产生日志并发送到队列中 ...

  9. arm处理器中a5 a8 a9,v6 v7,arm7 arm9 arm11都是依据什么来分类的【转】

    转自:http://blog.csdn.net/maochengtao/article/details/9951131ARM处理器发展这么多年,有很多架构,很多不同的内核 架构有armv1 v2 v3 ...

  10. XML解析代码

    import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.LinkedLi ...