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 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.

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.

——————————————————————————————————

给出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) 收藏的更多相关文章

  1. 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 ...

  2. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  3. C语言基础总结 分类: iOS学习 c语言基础 2015-06-11 10:08 23人阅读 评论(0) 收藏

    //欲练此功必先自宫!!!     //第一天:C语言的基础     //进制     //2进制, 10进制, 8进制, 16进制     //注:8进制数前加0, 16进制数前加0x        ...

  4. winform timespan 两个时间的间隔(差) 分类: WinForm 2014-04-15 10:14 419人阅读 评论(0) 收藏

    TimeSpan 结构  表示一个时间间隔. 先举一个小例子:(计算两个日期相差的天数) 代码如下: DateTime dt = DateTime.Now.ToShortDateString(yyyy ...

  5. Financial Management 分类: POJ 2015-06-11 10:51 12人阅读 评论(0) 收藏

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 164431   Accepted: ...

  6. DateTime日期格式获取 分类: C# 2014-04-15 10:36 233人阅读 评论(0) 收藏

    c#.net 获取时间年月日时分秒格式 //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now. ...

  7. 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...

  8. 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏

    Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...

  9. 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏

    完美素数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...

随机推荐

  1. mark TODO:完善拦截规则;日志分析;web仪表盘展示;终极目标动态配置规则

  2. hBuilder培训资源视频教程汇总

    DCloud对开发者的学习支持分3个层面:官方文档.三方专业培训.网友经验分享 DCloud的精力主要在做产品,配套的文档也会一直完善好.但专业的培训还不是DCloud能做好的,在HTML5中国产业联 ...

  3. Linq模型ObjectContext下查看Sql语句。

    ObjectContext 并没有提供 LINQ to SQL DataContext.Log 这样的功能,要查看实际生成的 T-SQL 语句,要么借助 SQL Server Sql Profiler ...

  4. TCP 3-Way Handshake (SYN,SYN-ACK,ACK)

    http://www.inetdaemon.com/tutorials/internet/tcp/3-way_handshake.shtml

  5. Appium简介及原理

    1.Appium简介 Appium是一个开源.跨平台的,适用于原生或混合移动应用(hybrid mobile apps)的自动化测试平台.Appium使用WebDriver(JSON wire pro ...

  6. Lifecycle of an ASP.NET Web API Message

    ASP.NET Web API, as we know now, is a framework that helps build Services over HTTP. Web API was int ...

  7. jenkins显示html样式问题的几种解决方案总结

    前言 jenkins上使用HTML Publisher plugin插件生成的html报告样式会丢失,需要设置下才能正常显示. 一.样式丢失 1.官方文档的解释如下,参考地址https://stack ...

  8. c# 数据集调试工具插件

    DataSetSpySetup,调试期查看dataset数据集的记录内容, Debug DataSet

  9. 路由的分发include实现

    在主程序里面的URL.py 中 from django.conf.urls import url, include urlpatterns = [ url(r'^cmdb/', include('ap ...

  10. D3D-GetBackBuffer &GetFrontBufferData 抓屏&D3D抓取GPU数据

    HRESULT GetBackBuffer( [in]          UINT                iSwapChain, [in]          UINT              ...