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 ...
随机推荐
- Python+Selenium使用Page Object实现页面自动化测试
Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...
- CodeForces 610A Pasha and Stick
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...
- Ubuntu上CUDA和CUDNN的安装
Ubuntu上CUDA的安装 下载路径: CUDA下载 `sudo dpkg -i cuda-repo-ubuntu1504-7-5-local_7.5-18_amd64.deb` `sudo apt ...
- Blog`s CSS
#div_digg { position: fixed; bottom: 10px; width: 50px; right: 50px; filter: alpha(opacity=20); opac ...
- 前台html与后台php通信(上传文件)
这部分为导入txt文本文件,存放在服务器然后返回txt文本的内容到前台进行相应操作 前台html代码 <div id="coordinate_div"> ...
- 6种炫酷的CSS3按钮边框动画特效
6种炫酷的CSS3按钮边框动画特效Button border animate 用鼠标滑过下面的按钮看看效果! Draw Draw Meet Center Spin Spin Circle Spin T ...
- Javascript Fromdata 与jQuery 实现Ajax文件上传
<!DOCTYPE html> <html> <head> <title>ajax</title> <script type=&quo ...
- linux分区-df
转自:http://baike.baidu.com/link?url=tyonI3NCB3F-ytIQz72PY-8uAaUQgfFFXbyKAea1e2NiB_t5AsE0MLOLc2LcqOiS ...
- 一种比较简单的在USB U盘中访问nandflash的方法
u8 nandflash_write_buffer[NAND_SERECT_FULL_SIZE]; static int currentBlock = -1; static int currentPa ...
- MQTT研究
http://www.jianshu.com/collection/1c742515f8d8 http://blog.csdn.net/gaojq_ios/article/details/481597 ...