D. Ability To Convert
http://codeforces.com/contest/758/problem/D
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进制下给你一个数,要你转换成最小的十进制数;
思路:贪心;
从后往前低位贪心选取的数尽量大。
要注意数字中有0的时候,如果不能和后面加前面不为0的数组在一起,将后面先组起来,然后在判断当前0时候能和前面不为0的组,不能单独成一位。
1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long LL;
4 char a[100];
5 LL quick(LL n,LL m);
6 LL ac[100];
7 int main(void)
8 {
9 LL n;
10 scanf("%lld",&n);
11 scanf("%s",a);
12 int l = strlen(a);
13 LL sum = 0;
14 LL ak = 1;
15 if(a[0] == '0')
16 printf("0\n");
17 else
18 {int cn = 0;
19 for(int i = l-1; i >= 0;)
20 {
21 if(a[i]!='0')
22 {
23 LL app = sum + (LL)(a[i]-'0')*ak;
24 if(app >= n)
25 {
26 ac[cn++] = sum;
27 ak = 1;
28 sum = 0;
29 }
30 else
31 {
32 sum = app;
33 ak*=10; i--;
34 }
35 }
36 else if(a[i] == '0')
37 {
38 LL app = sum;
39 int j;
40 for( j = i-1; j >= 0; j--)
41 {
42 if(a[j]!='0')
43 break;
44 }
45 int s;
46 for( s = i; s >= j; s--)
47 {if(ak >= n)break;
48 app = app+ak*(LL)(a[s]-'0');
49 ak*=10;
50 }
51 if(app < n&& s == j-1)
52 {
53 sum = app;
54 i = j-1;
55 }
56 else
57 {
58 ac[cn++] = sum;
59 if(sum == 0)
60 i--;
61 sum = 0;
62 ak = 1;
63 }
64 }
65 }
66 ac[cn++] = sum;
67 sum = 1;
68 LL ask = 0;
69 for(int i = cn-1;i >= 0;i--)
70 {
71 ask *= n;
72 ask += ac[i];
73 }
74 printf("%lld\n",ask);}
75 return 0;
76 }
D. Ability To Convert的更多相关文章
- Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)
D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...
- Codeforces Round #392 (Div. 2)-D. Ability To Convert
D - Ability To Convert 题目大意:给你一个数字 n 接下来再输入一个数字 w(<10^60),表示w这个数字是 n 进制的, 并且超过十进制也用数字表示,这样就有多种组合了 ...
- Codeforces758D Ability To Convert 2017-01-20 10:29 231人阅读 评论(0) 收藏
D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【动态规划】Codeforces Round #392 (Div. 2) D. Ability To Convert
D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...
- CodeForces 758 D Ability To Convert
Ability To Convert 题意:给你一个n进制的60位的数,但是由于Alexander只会写0->9,所以他就会用10来表示十而不是A(假设进制>10); 题解:模拟就好了,先 ...
- codeforces 758D Ability To Convert【DP】
在N进制下给你一个数,要你转换成最小的十进制数; 状态转移方程:从前向后 dp[j]表示j位前数列的最小十进制数 dp[j]=min(dp[j],dp[i]*n+x) 程序: #include < ...
- CF758 D. Ability To Convert 细节处理字符串
link 题意:给定进制数n及一串数字,问在此进制下这串数能看成最小的数(10进制)是多少(如HEX下 1|13|11 = 475) 思路:此题要仔细思考细节.首先要想使数最小那么必定有个想法是使低位 ...
- Codeforces 758D Ability To Convert(区间DP)
题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...
- 【codeforces 758D】Ability To Convert
[题目链接]:http://codeforces.com/contest/758/problem/D [题意] 给你一个n进制的数k; 问你它可能的最小的十进制数是多少; [题解] 从右往左; 获取数 ...
随机推荐
- [C++] vptr, where are you?
Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...
- day06 模板层
day06 模板层 今日内容 常用语法 模板语法传值 模板语法之过滤器 模板语法之标签 自定义过滤器.标签.inclusion_tag(BBS作业用一次) 模板的继承(django前后端结合 那么使用 ...
- [云原生]Docker - 镜像
目录 Docker镜像 获取镜像 列出本地镜像 创建镜像 方法一:修改已有镜像 方法二:通过Dockerfile构建镜像 方法三:从本地文件系统导入 上传镜像 保存和载入镜像 移除本地镜像 镜像的实现 ...
- DBeaver客户端工具连接Hive
目录 介绍 下载安装 相关配置 1.填写主机名 2.配置驱动 简单使用 主题设置 字体背景色 介绍 在hive命令行beeline中写一些很长的查询语句不是很方便,急需一个hive的客户端界面工具 D ...
- Shell脚本字符串截取方法总结
Shell脚本8种字符串截取方法总结转自:https://www.cnblogs.com/ralphdc/p/8032335.html Linux 的字符串截取很有用.有八种方法.假设有变量 var= ...
- Simulating final class in C++
Ever wondered how can you design a class in C++ which can't be inherited. Java and C# programming la ...
- shell脚本统计多个CPU利用率
本节主要内容:top命令统计CPU的利用率 一,问题分析 MySQL在Linux下是多线程的,而且只能将多个线程分布到一个CPU上.因此,使用小型服务器,或者PC SERVER,多个CPU利用率并不高 ...
- 注册页面的servlet
package cn.itcast.travel.web.servlet;import cn.itcast.travel.domain.ResultInfo;import cn.itcast.trav ...
- 3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)
1.引入maven坐标 <!--spring-data-elasticsearch--><dependency> <groupId>org.springframew ...
- [BUUCTF]PWN——jarvisoj_level3
jarvisoj_level3 附件 步骤 例行检查,32位,nx保护 运行一下程序 32位ida载入,shift+f12没有看到程序里有可以直接利用的后面函数,根据运行时的字符串找到了程序的关键函数 ...