• PAT 1010 Radix


    1010. Radix (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

    Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

    Input Specification:

    Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
    N1 N2 tag radix
    Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

    Output Specification:

    For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.

    Sample Input 1:
    6 110 1 10
    
    Sample Output 1:
    2
    
    Sample Input 2:
    1 ab 1 2
    
    Sample Output 2:

    Impossible

    给你两个数,问你,一个数用什么进制转换之后可以等于另一个数,由于进制可以很大,可以二分求解

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    #include <string>
    #include <vector>
    #include <strstream>
    #include <map>
    
    using namespace std;
    typedef long long int LL;
    string str1,str2;
    int tag;LL radix;
    LL fun(string str,LL radix)
    {
        LL num1=0;
      int len1=str.length();
        for(int i=0;i<len1;i++)
        {
        num1*=radix;
            if(str[i]<='9'&&str[i]>='0')
                num1+=(LL)(str[i]-'0');
            else if(str[i]<='z'&&str[i]>='a')
                num1+=(LL)(str[i]-'a'+10);
        if(num1<0)
          return -1;
        }
        return num1;
    }
    int main()
    {
    
        cin>>str1>>str2;
        scanf("%d%lld",&tag,&radix);
       
        LL num1=0;LL num2=0;
        if(tag==2)
            swap(str1,str2);
      int len1=str1.length();
        int len2=str2.length();
        LL ans;
        num1=fun(str1,radix);
        int minn=1;
        for(int i=0;i<len2;i++)
        {
            if(str2[i]<='9'&&str2[i]>='0')
                minn=max(minn,str2[i]-'0');
            else if(str2[i]<='z'&&str2[i]>='a')
                minn=max(minn,str2[i]-'a'+10);
        }
      LL l=minn+1;
      LL r=num1+1;
      bool flag=false;
      while(l<=r)
      {
        LL mid=(l+r)/2;
        num2=fun(str2,mid);
        if(num2==-1||num2>num1)
          r=mid-1;
        else if(num2==num1)
        {
          ans=mid;
          flag=true;
          break;
        }
        else
          l=mid+1;
      }
      if(!flag)
            printf("Impossible
    ");
        else
            printf("%lld
    ",ans);
        return 0;
    
    }


  • 相关阅读:
    《追风筝的人》书摘与笔记
    《清单革命》读书笔记
    华为HMS Core音频服务,让有声世界更动听
    Unity平台 | 快速集成华为性能管理服务
    【Demo见真章】投稿赢HarmonyOS手机Beta公测名额
    【开发者必看】2020年移动应用趋势洞察白皮书
    华为帐号服务升级,助力应用快速验证用户身份
    简单的路径规划案例分享
    Cocos|快速集成华为AGC云存储服务
    Flutter平台 | 零代码快速集成AGC崩溃服务
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228619.html
Copyright © 2020-2023  润新知