Codeforces758D Ability To Convert 2017-01-20 10:29 231人阅读 评论(0) 收藏
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.
——————————————————————————————————
给出n进制和一个对应的n进制的数,问最小换成多大的十进制数。
贪心处理,先倒序,从最小位处理每次尽量取较多的位,注意前导零,题目不允许出现前导零。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string> using namespace std; long long mypow(long long a,long long b)
{
long long ans=1;
for(int i=0; i<b; i++)
{
ans*=a;
}
return ans;
} int main()
{
char s[100];
long long n,a; while(~scanf("%I64d %s",&n,s))
{
int k=strlen(s);
for(int i=0; i<k/2; i++)
{
swap(s[i],s[k-1-i]);
}
long long m=n;
int t=0;
while(m>0)
{
m/=10;
t++;
}
long long kk=0,as=0,pw=0,tot=0,w[1000],ans=0;
for(int i=0; i<k; i++)
{
as+=(s[i]-'0')*mypow(10,kk);
kk++;
if(kk==t)
{
while(s[i]=='0'&&kk>1)
{
i--;
kk--; }
if(as<n)
w[tot++]=as;
else
{
w[tot++]=as-mypow(10,kk-1)*(s[i]-'0');
kk--;
i--;
while(s[i]=='0'&&kk>1)
{
i--;
kk--; }
} as=0;
kk=0;
}
}
w[tot++]=as;
for(int i=0;i<tot;i++)
{
ans+=w[i]*mypow(n,pw);
pw++;
}
printf("%I64d\n",ans);
}
return 0;
}
Codeforces758D Ability To Convert 2017-01-20 10:29 231人阅读 评论(0) 收藏的更多相关文章
- Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏
参考APIDEMO及http://developer.android.com/guide/components/loaders.html#app 1.Introduced in Android 3.0 ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...
- C语言基础总结 分类: iOS学习 c语言基础 2015-06-11 10:08 23人阅读 评论(0) 收藏
//欲练此功必先自宫!!! //第一天:C语言的基础 //进制 //2进制, 10进制, 8进制, 16进制 //注:8进制数前加0, 16进制数前加0x ...
- winform timespan 两个时间的间隔(差) 分类: WinForm 2014-04-15 10:14 419人阅读 评论(0) 收藏
TimeSpan 结构 表示一个时间间隔. 先举一个小例子:(计算两个日期相差的天数) 代码如下: DateTime dt = DateTime.Now.ToShortDateString(yyyy ...
- Financial Management 分类: POJ 2015-06-11 10:51 12人阅读 评论(0) 收藏
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 164431 Accepted: ...
- DateTime日期格式获取 分类: C# 2014-04-15 10:36 233人阅读 评论(0) 收藏
c#.net 获取时间年月日时分秒格式 //获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now. ...
- 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏
以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...
- 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...
- 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏
完美素数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...
随机推荐
- Linux内核 TCP/IP参数调优
http://www.360doc.com/content/14/0606/16/3300331_384326124.shtml
- 全是干货!UI设计的30条黄金准则!
http://www.wex5.com/portfolio-items/js-1/ 全是干货!UI设计的30条黄金准则! 总的来说,好的UI界面有几个特征:简洁.便利.目标明确.人性化.字面上看这 ...
- Asp.net MVC 出现“Failed to map the path '/'.”错误
Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a ...
- cesium初始化参数
var viewer = new Cesium.Viewer('cesiumContainer',{ animation:false, //动画控制不显示 //baseLayerPicker:fals ...
- CSS 第1练 搜索
1.搜索 效果: <!DOCTYPE HTML> <html> <head> <meta charset="gbk" /> < ...
- TMS Grid
TMS Grid http://edn.embarcadero.com/article/42553
- localStorage,sessionStorage
h5的本地存储. 现代浏览器普遍开始支持H5本地存储,localStorage.sessionStorage.可以用来代替cookie的一部分存储功能,他比cookie存储量更大.比较实用. 两者用法 ...
- 更改Eclipse里的Classpath Variables M2_REPO
M2_REPO这个classpath variable 是不能改变的. 为什么 Eclipse 里的 Classpath Variables M2_REPO 无法修改(non modifiable) ...
- idea 插件
https://plugins.jetbrains.com/plugin/4509-statistic
- Website蝴蝶结构
[Website蝴蝶结构] 网页的其正向链接连结在一起表现为一种蝴蝶结结构. 1.蝴蝶结中部(SCC, Strongly Connected Componnet) 这种网页彼此相连. 2.蝴蝶结左部( ...