zoukankan      html  css  js  c++  java
  • codeforces 758D

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

    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.

    这题的题意是第一行输入一个进制,第二行输入该进制下的数,输出将该数转化为十进制的最小值。

    ( 有零的地方一直都处理不好- -,后来看了别人的代码才学会。

    思路是用字符串读入,从后往前加,如果大于输入的进制了,就跳出再判断含有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 }
    View Code

     

  • 相关阅读:
    SSH 密钥类型的的选择
    VMware 默认的虚拟交换机关系与VMnet设置
    路由器端口映射不生效的解决方法
    优先使用对象组合,而不是类继承
    权限管理系统之软件注册模块
    如何正确安装phpDocumentor
    IE下cookie跨域问题
    Linux下Apache无法解析.php文件
    用 memcache 来存储 session
    windows7 下 phpunit 安装
  • 原文地址:https://www.cnblogs.com/zmin/p/7507129.html
Copyright © 2011-2022 走看看