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,那么我们称这个数为完美 ...
随机推荐
- HDU 2199 Can you solve this equation?(二分精度)
HDU 2199 Can you solve this equation? Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == ...
- Tensorflow笔记——神经网络图像识别(一)前反向传播,神经网络八股
第一讲:人工智能概述 第三讲:Tensorflow框架 前向传播: 反向传播: 总的代码: #coding:utf-8 #1.导入模块,生成模拟数据集 import t ...
- HDU 1717 小数化分数2(最大公约数)
小数化分数2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- UI“三重天”之selenium--封装(二)
基础示例代码: /** * @author Richered **/ package com.sample; import org.openqa.selenium.By; import org.ope ...
- Keras函数式 API
用Keras定义网络模型有两种方式, Sequential 顺序模型 Keras 函数式 API模型 之前我们介绍了Sequential顺序模型,今天我们来接触一下 Keras 的函数式API模型. ...
- http和https(转)
一.HTTP协议 最近看了一些网络通信方面的书籍,研究了一下 HTTP 和 TCP/IP,有了一些新的收获和理解,在这里做个归纳和总结. (1)什么是HTTP协议 HTTP (HyperText Tr ...
- jQuery操作table tr td
1.鼠标移动行变色 $("#tab tr").hover(function(){ $(this).children("td").addClass("h ...
- (2/24) 快速上手一个webpack的demo
写在前面:该部分的安装都是基于windows系统的,且此处的webpack的版本为:3.6.0. 1.安装webpack 1.1 安装方法: 用win+R打开运行对话框,输入cmd进入命令行模式.然后 ...
- firemonkey Grid自定义
http://stackoverflow.com/questions/28893564/memory-leak-on-tstringgrids-ondrawcolumncell-event http: ...
- Git----远程仓库01
到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题了 可是用过集中式版本控制系统SVN的童鞋们会站出来说,这些功能在SVN里早就有了,没看出Git ...