CodeForces758D
D. Ability To Convert
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的更多相关文章
- 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 ...
随机推荐
- phpstorm 正则匹配删除注释行(替换注释行为空行)
使用phpstorm 来编写php 和javascript 代码,感觉还是不错的,用得也很舒服. 遇到了一个需求,有时候在阅读框架源代码的时候 , 想过滤(删除)掉源代码中的注释行,如果手动逐行删除显 ...
- JavaScript(三)---- 控制流程语句
常用的控制流程语句有判断语句.分支语句.循环语句.基本用法都和java中的一致,switch有几点特殊. 1.判断语句 格式: if(判断条件){ 符合条件执行的代 ...
- 安卓 Android题目大全
安卓001个人事务管理系统(单端) 安卓002手机订餐系统 安卓003无线点菜 安卓004酒店房间预定系统 安卓005个人相册管理系统(单端) 安卓006计算器(单端) 安卓007英语学习(单端) ...
- iBATIS 3 试用手记 - The FUTURE - ITeye技术网站
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- linux 驱动入门5
慢慢的开始转驱动,目前比较有时间,一定要把驱动学会.哎.人生慢慢路,一回头.已经工作了八九年了.努力.在买套房.改退休了.学驱动.个人认为首先要熟悉驱动框架.慢慢来.心急吃不了热豆腐. 看网上都说的设 ...
- 《数据结构与算法分析:C语言描述》读书笔记------练习1.1 求第K大的数
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保 ...
- Xcode--Alcatraz来管理Xcode插件
简介 Alcatraz是一个帮你管理Xcode插件.模版以及颜色配置的工具.它可以直接集成到Xcode的图形界面中,让你感觉就像在使用Xcode自带的功能一样. 安装和删除 使用如下的命令行来安装Al ...
- ZOJ 3331 Process the Tasks
双塔DP. #include<cstdio> #include<cstring> #include<queue> #include<string> #i ...
- Linux ALSA声卡驱动之三:PCM设备的创建
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! 1. PCM是什么 模数转换 模拟信号经过pcm(脉冲编码调制)后为pcm数据: PCM是 ...
- 阿里开源Mysql分布式中间件:Cobar
目前在从事数据库中间件的开发和维护工作,我们使用的数据库中间件就是由cobar改造而来,所以对于cobar的一些说明一看就明白了: 下面是看到的一个很不错的分析文档 这里整理了下方便自己学习使用. C ...