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. sublime text 主题推荐

    Soda Spacegray Flatland Tomorrow Base 16 Solarized Predawn itg.flat 其他所有的配色方案和主题.

  2. CDOJ 1270 Playfair

    模拟题,代码写得比较乱... #include<cstdio> #include<cstring> #include<cmath> #include<queu ...

  3. (中等) POJ 2948 Martian Mining,DP。

    Description The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site ...

  4. OC语言的特性(二)-Block

    本篇文章的主要内容 了解何谓block. 了解block的使用方法. Block 是iOS在4.0版本之后新增的程序语法. 在iOS SDK 4.0之后,Block几乎出现在所有新版的API之中,换句 ...

  5. Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"

    面向对象:曾经使用过SVN的同学. (因为Git 它 可以说是双重的SVN (本地一个服务器,远程一个服务器)),提交代码要有两次步骤,先提交到本地服务器,再把本地服务器在提交到远程服务器. 所以连S ...

  6. 关于JAVA中hashCode

    hash code 即散列码根据对象计算出的一个整型值,散列码是没有规律的. 如果两个对象相等(equal() 返回true),则hashCode一定相等,如果两个对象hashCode相等,则对象不一 ...

  7. Ubuntu下搭建C++开发环境

    Ubuntu使用eclipse搭建c/c++编译环境----CDT插件 Ubuntu(Linux)使用Eclipse搭建C/C++编译环境          这两天,给自己电脑弄了双系统,除了原来的W ...

  8. ajax常用写法

    $.ajax( url,{ type:"post", data:{},}) .done(function() { alert("success"); }) .f ...

  9. 用简单的http抓包来实现微信公众网页如何模拟登录

    一.准备工具: 系统:XP 浏览器:IE8 抓包工具:HttpWatch(它可以查看url请求的数据包) 二.抓包思路: 浏览器上的任何获取数据的方式都符合http协议的请求,只要发送符合要求的数据就 ...

  10. UVa 10299 - Relatives

    题目大意:Euler's Totient的应用. 几乎和UVa 10179 - Irreducable Basic Fractions一样,于是偷了个懒,直接用10179题的代码,结果WA了,感觉一样 ...