codeforces 758D
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 kcontains 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.
这题的题意是第一行输入一个进制,第二行输入该进制下的数,输出将该数转化为十进制的最小值。
( 有零的地方一直都处理不好- -,后来看了别人的代码才学会。
思路是用字符串读入,从后往前加,如果大于输入的进制了,就跳出再判断含有0的情况,直到循环到起始。
1 #include <iostream>
2 #define ll long long
3 using namespace std;
4 string s;
5 int main()
6 {
7 ll b;
8 cin>>b>>s;
9 ll ans = 0;
10 ll p = 1;
11 ll len = s.length() - 1;
12 while(len >= 0) {
13 ll sum = 0;
14 ll t = 1;
15 ll i;
16 for( i = len; i >= 0; i--) {
17 if(max((s[i] - '0'),1) * t + sum >= b)
18 break;
19 sum += (s[i] - '0') * t;
20 t *= 10;
21 // cout<<sum<<endl;
22 }
23 while(i < len - 1) { //每位至少有一个数,所以每次大的循环至少会让最后一位数
24 if(s[i+1] == '0') { //加入ans中,因此遍历到倒数第二位之前就行了
25 i++;
26 }
27 else break;
28 }
29 ans += sum * p;
30 // cout<<ans<<endl;
31 p *= b;
32 len = i;
33 }
34 cout << ans << endl;
35 return 0;
36 }
codeforces 758D的更多相关文章
- 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; 问你它可能的最小的十进制数是多少; [题解] 从右往左; 获取数 ...
- Codeforces 758D:Ability To Convert(思维+模拟)
http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...
- codeforces 758D Ability To Convert【DP】
在N进制下给你一个数,要你转换成最小的十进制数; 状态转移方程:从前向后 dp[j]表示j位前数列的最小十进制数 dp[j]=min(dp[j],dp[i]*n+x) 程序: #include < ...
- 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) 758D (贪心)
orz 最近被水题卡+FST,各种掉rating 题目大意 一个数s它是n进制的,但是每一位不是用'A','B'....表示的,而是用10,11等等表示的,将它还原成十进制 这种表示方法显然会产生多解 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- [Usaco2002 Feb]Rebuilding Roads重建道路
题目描述 一场可怕的地震后,奶牛用N个牲口棚(1 <= N <= 150,编号1..N)重建了农民John的牧场.奶牛没有时间建设多余的道路,所以现在从一个牲口棚到另一个牲口棚的道路是唯一 ...
- 关于java并发场景下,HttpServletRequst中session丢失问题
使用场景: 在list数据进来之后使用安全数组 Lists.newCopyOnWriteArrayList() 进行了 parallelStream 并行处理,在接口中进行了登录者信息接口 ...
- Redis 实战 —— 07. 复制、处理故障、事务及性能优化
复制简介 P61 关系型数据库通常会使用一个主服务器 (master) 向多个从服务器 (slave) 发送更新,并使用从服务器来处理所有读请求. Redis 也采用了同样的方法实现自己的复制特性,并 ...
- 离线安装docker-ce
1.用一台可以连外网的虚拟机把docker-ce安装包下载下来,vim /tmp/docker-download.sh #!/bin/bash set -e mkdir -p /apps/docker ...
- STP 根桥、根端口、指定端口是如何选举的
学习HCIA过程中,对交换机的根桥.跟端口以及指定端口选举有些迷糊,也度娘了一番,总觉得一部分人解释的不够全面精细.通过仔细研究最终有了自己的理解,分享给大家,如果纰漏,欢迎指正. STP收敛过程: ...
- centos系统磁盘扩容
1.查看磁盘空间大小,使用df -h 命令. 2. 增加磁盘空间,例如下图使用VM虚拟机增加的方式.物理机直接安装挂载上去. 3. 使用fdisk /dev/sda, 创建新分区. 4.重启Linux ...
- What is the difference between Serialization and Marshaling?
How to serialize and deserialize JSON using C# - .NET | Microsoft Docs https://docs.microsoft.com/en ...
- java面向对象(二)构造函数和构造代码块
面向对象 类成员 1.成员变量 属性 数值类型的基本数据类型默认值是 0 成员变量在任何方法中都能访问,和声明先后没有关系 2.成员函数 方法 3.定义方式 class 类名{成员变量:成员函数} / ...
- 内存屏障 WriteBarrier 垃圾回收 屏障技术
https://baike.baidu.com/item/内存屏障 内存屏障,也称内存栅栏,内存栅障,屏障指令等, 是一类同步屏障指令,是CPU或编译器在对内存随机访问的操作中的一个同步点,使得此点之 ...
- LIS的优化
二分优化 在求一个最长不上升自序列中,显然其结尾元素越小,越有利于接其他元素,对答案的贡献也就可能会更高 那么我们可以用low[i]去存长度为i的LIS结尾元素的最小值 因此我们只要维护low数组 对 ...