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

此题有毒。直接贪心,自己电脑上测试数据都过,cf上莫名其妙的变了。。。

 //2016.01.21
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long using namespace std; int fun_len(ll a)
{
ll n = ;
while(a)
{
n++;
a/=;
}
return n;
} ll pow(ll a, ll b)
{
ll ans = ;
while(b)
{
if(b&)ans *= a;
a*=a;
b>>=;
}
} int main()
{
ll cnt, dig[];
ll n;
string k;
while(cin>>n>>k)
{
cnt = ;
ll tmp = n, base;
int len_of_n = , high;
while(tmp)
{
len_of_n++;
high = tmp%;
tmp/=;
}
memset(dig, , sizeof(dig));
tmp = , base = ;
for(int i = k.length()-; i >= ; i--)
{
if(k[i]=='')
{
int pos = i;
while(k[pos] == '')pos--;
int num_of_zero = i-pos;
if(tmp!= && ((k[pos]-'')*pow(, num_of_zero+fun_len(tmp))+tmp>=n))dig[cnt++] = tmp;
else if(tmp!=){
tmp = (k[pos]-'')*pow(, num_of_zero+fun_len(tmp))+tmp;
base = pow(, fun_len(tmp));
i = pos;
if(i==){dig[cnt++] = tmp; break;}
continue;
}
if(num_of_zero<len_of_n-){
tmp = (k[pos]-'')*pow(, num_of_zero);
base = pow(, num_of_zero+);
}
else{
tmp = (k[pos]-'')*pow(, len_of_n-);
int zero = num_of_zero-len_of_n+;
if(tmp>=n){
tmp/=;
zero++;
}
for(int j = ; j < zero; j++)dig[cnt++] = ;
base*=;
}
if(pos == ){dig[cnt++] = tmp; break;}
i = pos-;
if(k[i] == ''){i++;continue;}
}
if(tmp+base*(k[i]-'')>=n)
{
dig[cnt++] = tmp;
tmp = k[i]-'';
base = ;
}else{
tmp += base*(k[i]-'');
base*=;
}
if(i==)dig[cnt++] = tmp;
}
ll ans = ;
base = ;
for(int i = ; i < cnt; i++)
{
ans += base*dig[i];
base*=n;
}
cout<<ans<<endl;
} return ;
}

CodeForces758D的更多相关文章

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

随机推荐

  1. ZenCoding 个人理解和总结

    我的理解:ZenCoding是一个html简写的语法,可以最快速的生成html. 不少IDE应该都支持,我用的intellij idea是支持的. ZenCoding表示和CSS/JS有相通之处,比如 ...

  2. python之路: 基础篇

    )或>>> name = )    #按照占位符的顺序):]        #下标识从0开始的 wulaoer >>> print name[:]        # ...

  3. Salesforce开发者学习笔记之二:Salesforce开发平台应用场景

    Salesforce作为一个全方位的CRM系统可以应用于企业中的各个不同部门以取代手工的,耗时的以及低效的业务流程,例如 基于报表的数据管理和分析 基于电子邮件的协同合作 本地的文件共享 各种手工操作 ...

  4. css颜色渐变在不同浏览器的设置

    在web开发中,难免会遇到浏览器之间的兼容问题,关于Css设置颜色渐变下面有解决的办法,直接上代码: 适用于谷歌浏览器: background: -webkit-gradient(linear, 0 ...

  5. 处理JSON格式的数据

    JSON格式的数据是最常用的数据格式,处理方法的选择就显得比较重要了.我常用的一种是用对象来接收,然后保存在数组中,需要时直接从数组中取值.下面列出一个小例子. .h文件中: #import < ...

  6. 由浅入深Mysql优化

    选Mysql优化作为我的第一篇博文,实在是因为这个东西很有意思,也是能体现后端开发人员设计细节及逻辑分析的一个知识点. 那么来吧: 作为Mysql优化,很多人大概能跟着感觉说出如下   :  (1)常 ...

  7. jstree使用小结(一)

    项目中用到tree结构,使用了jstree做个笔记如下: 1. 官网: http://www.jstree.com/    有时候打不开,那就只能等打得开的时候再看了...O(∩_∩)O [PS: 一 ...

  8. Sublime text追踪函数插件

    Sublime Text2/3怎样在Ubuntu中配置CTags插件 | 浏览:1278 | 更新:2014-03-05 10:34 1 2 3 4 5 6 7 分步阅读 本文详解在Ubuntu Li ...

  9. SQL之left join、right join、inner join

    创建表(Create table): CREATE TABLE A ( Id INT PRIMARY KEY, Name VARCHAR(20) NOT NULL); CREATE TABLE B ( ...

  10. java 之 Spring

    1.Spring 介绍 2.Spring 下载 3.Spring 导入 4.Spring 配置