CodeForces758D

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上莫名其妙的变了。。。

  1 //2016.01.21
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <algorithm>
  5 #include <cstring>
  6 #define ll long long
  7 
  8 using namespace std;
  9 
 10 int fun_len(ll a)
 11 {
 12     ll n = 0;
 13     while(a)
 14     {
 15         n++;
 16         a/=10;
 17     }
 18     return n;
 19 }
 20 
 21 ll pow(ll a, ll b)
 22 {
 23     ll ans = 1;
 24     while(b)
 25     {
 26         if(b&1)ans *= a;
 27         a*=a;
 28         b>>=1;
 29     }
 30 }
 31 
 32 int main()
 33 {
 34     ll cnt, dig[65];
 35     ll n;
 36     string k;
 37     while(cin>>n>>k)
 38     {
 39         cnt = 0;
 40         ll tmp = n, base;
 41         int len_of_n = 0, high;
 42         while(tmp)
 43         {
 44             len_of_n++;
 45             high = tmp%10;
 46             tmp/=10;
 47         }
 48         memset(dig, 0, sizeof(dig));
 49         tmp = 0, base = 1;
 50         for(int i = k.length()-1; i >= 0; i--)
 51         {
 52             if(k[i]=='0')
 53             {
 54                 int pos = i;
 55                 while(k[pos] == '0')pos--;
 56                 int num_of_zero = i-pos;
 57                 if(tmp!=0 && ((k[pos]-'0')*pow(10, num_of_zero+fun_len(tmp))+tmp>=n))dig[cnt++] = tmp;
 58                 else if(tmp!=0){
 59                     tmp = (k[pos]-'0')*pow(10, num_of_zero+fun_len(tmp))+tmp;
 60                     base = pow(10, fun_len(tmp));
 61                     i = pos;
 62                     if(i==0){dig[cnt++] = tmp; break;}
 63                     continue;
 64                 }
 65                 if(num_of_zero<len_of_n-1){
 66                     tmp = (k[pos]-'0')*pow(10, num_of_zero);
 67                     base = pow(10, num_of_zero+1);
 68                 }
 69                 else{
 70                     tmp = (k[pos]-'0')*pow(10, len_of_n-1);
 71                     int zero = num_of_zero-len_of_n+1;
 72                     if(tmp>=n){
 73                         tmp/=10;
 74                         zero++;
 75                     }
 76                     for(int j = 0; j < zero; j++)dig[cnt++] = 0;
 77                     base*=10;
 78                 }
 79                 if(pos == 0){dig[cnt++] = tmp; break;}
 80                 i = pos-1;
 81                 if(k[i] == '0'){i++;continue;}
 82             }
 83             if(tmp+base*(k[i]-'0')>=n)
 84             {
 85                 dig[cnt++] = tmp;
 86                 tmp = k[i]-'0';
 87                 base = 10;
 88             }else{
 89                 tmp += base*(k[i]-'0');
 90                 base*=10;
 91             } 
 92             if(i==0)dig[cnt++] = tmp;
 93         }
 94         ll ans = 0;
 95         base = 1;
 96         for(int i = 0; i < cnt; i++)
 97         {
 98             ans += base*dig[i];
 99             base*=n;
100         }
101         cout<<ans<<endl;
102     }
103 
104     return 0;
105 }

 

转载于:https://www.cnblogs.com/Penn000/p/6337860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值