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. appium通过WiFi连接真机进行测试

    http://www.th7.cn/Program/Android/201507/514602.shtml appium通过WiFi连接真机进行测试   2015-07-24 19:43:07CSDN ...

  2. 使用axis2开发webservices并打包到tomcat

    1. 写service类 package com.datatrans.demo; public class HelloServiceNew { public String sayHelloNew(){ ...

  3. Hibernate---基础配置之日志信息slf 及搭建日志环境

    slf日志接口, 实现有slf4j nodep, log4j hibernate里我们一般用 log4j,所以删除之前创建的hibernate 包里的 slf4j-nop包, 加入log4j-1.2. ...

  4. andorid之摄像头驱动流程--MTK平台

    原文地址:andorid之摄像头驱动流程--MTK平台 作者:守候心田 camera成像原理: 景物通过镜头生产光学图像投射到sensor表面上,然后转为模拟电信号,经过数模变成数字图像信号,在经过D ...

  5. (简单) POJ 2406 Power Strings,扩展KMP。

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  6. ios 添加PCH文件

  7. [转]解决Maven报错"Plugin execution not covered by lifecycle configuration"

    [转]解决Maven报错"Plugin execution not covered by lifecycle configuration" 导入Myabtis源码后,POM文件会报 ...

  8. iOS 消息推送原理及实现总结

    在实现消息推送之前先提及几个于推送相关概念,如下图:1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服务 ...

  9. STM32-USB那点事

    STM32 USB那点事1 USB那点事2 - Custom HID例子程序解疑 USB那点事3 -使用端口2作为custom HID的传输 USB那点事5之USB通信出错 USB那点事6传输要素 S ...

  10. ajax 基础2

    连接数据库实现分页功能 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Defa ...