Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)
1 second
256 megabytes
standard input
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.
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 k contains 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.
Print the number x (0 ≤ x ≤ 1018) — the answer to the problem.
13
12
12
16
11311
475
20
999
3789
17
2016
594
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我没打,其实是很水的一场,不过这道题解法想到too simple,就是细节特别讨厌,会导致WA声一片。。。
题意:给你N进制与对应的数字,问转化成10进制最小是多少?
解析:一眼贪心,倒着每次尽可能取较多的数,其实证明也是很简单的(略=懒的写),不过要注意前导0问题,细节题啊~最讨厌这种带细节题的场了。DP也可以,可能DP在细节上处理的不会很烦,我这里给出贪心AC代码。
#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std; long long n,ans;
int l;
string k; long long powx(long long x,int y)
{
if(y==0)return 1;
long long s=powx(x,y/2);
if(y%2)return s*s*x; else return s*s;
} int main()
{
scanf("%I64d",&n);
long long t=n;
while(t>0)
{
t/=10LL;
l++;
}
cin>>k; int p=0,v=0,last=k.size()-1;
long long sum=0;
for(int i=k.size()-1;i>=0;i--)
{
long long t=sum;
sum+=powx(10,v)*(k[i]-48);
if(sum<n && v<l)v++;
else
{
int kkk=i;
if(k[i+1]=='0')
{
int ss=0;
while(k[++i]=='0')
{
ss++;
if(i==last)break;
}
i--;
}
ans+=powx(n,p)*t;
sum=k[i]-48;
v=1;
p++;
last=i;
}
}
ans+=powx(n,p)*sum;
printf("%I64d\n",ans);
return 0;
}
最新文章
- JavaScript:声明变量名的语法规则
- xampp 端口冲突
- Cookie 知识点再整理
- ERP联系记录管理(十七)
- 超级终端和SecureCRT进行Console口的连接
- oracle中的function 、procedure、packages、package bodies比较
- linux ar 命令的使用说明那个和例子[转]
- svn加入新的文件夹
- 转:apache 的mod-status
- 1787: [Ahoi2008]Meet 紧急集合
- 房上的猫:switch选择结构,与选择结构总结
- hdu-5786(补图最短路)
- CSS3_线性渐变_径向渐变----背景
- C# 结构(struct)的特点
- 阈值分割与XLD轮廓拼接——第4讲
- UnityShader之屏幕特效基础
- Linux 磁盘与文件系统(EXT2)简介
- Sprint2-2.0
- 当Shell遇上了NodeJS
- PowerDesigner16 生成的备注脚本,在sql server 2008 中报“对象名 &#39;sysproperties&#39; 无效”的错误的解决办法