• 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;
    
    }


  • 相关阅读:
    C#.NET.JSON库
    C# .NET 私钥 RSA2,SHA256,签名
    fiddler 捕捉不到代码发出去的HTTP请求
    双网卡共享上网
    json 反序列化成键值对
    C# .NET 杀进程
    docker in centos error
    深入理解磁盘文件系统之inode
    CentOS下安装高版本GCC
    Mac 使用 launchctl 定时运行程序(转载)
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228619.html
Copyright © 2020-2023  润新知