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,那么我们称这个数为完美 ...
随机推荐
- JProfiler 8(一个很好的java性能监控工具) 下载和注册码
windows x64 zip下载地址:http://download-aws.ej-technologies.com/jprofiler/jprofiler_windows-x64_8_0_1.zi ...
- [C++ Primer] 第3章: 字符串, 向量和数组
标准库类型string string初始化 string s2(s1); string s2 = s1; string s3("value"); string s3 = " ...
- 注解反射原理(IOC框架)
IOC(Inversion of Control):控制反转.采用配置文件和注解的方式,将成员变量通过反射注入,舍弃new的方式,降低了耦合度. 反射:JAVA反射机制是在运行状态中,对于任意一个类, ...
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- WPF 实现指定UI控件截图
using System.Windows.Media.Imaging; using System.IO; private void SaveToImage(FrameworkElement ui, s ...
- Vim中异步语法检查ale配置
注意 在设置let g:ale_sign_error = '✗'和let g:ale_sign_warning = '⚡'这些时,可能vim不让你保存,提示fenc这个东西. 所以,为了保险起见,你最 ...
- 少走弯路,给Java 1~5 年程序员的建议
参考:https://www.jianshu.com/p/5681a1f0aad6 今天LZ是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每个阶段要学习的内容甚至是一些书籍.这一部分的内 ...
- 容器网络之 veth设备
创建命名空间 # ip netns add mhc # ip link show1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue ...
- [leetcode]141. Linked List Cycle判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 未能映射路径"/"
1.检查Server.MapPath 这里面需要像这样: ~/uploads/ 有~符号. 2.应用程序池出现问题,换一个应用程序池,或者重启程序池.