前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 1010 Radix

PAT 1010 Radix

作者头像
ShenduCC
发布2018-04-26 17:34:00
6170
发布2018-04-26 17:34:00
举报
文章被收录于专栏:算法修养算法修养

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:

代码语言:javascript
复制
6 110 1 10

Sample Output 1:

代码语言:javascript
复制
2

Sample Input 2:

代码语言:javascript
复制
1 ab 1 2

Sample Output 2:

Impossible

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

代码语言:javascript
复制
#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\n");
    else
        printf("%lld\n",ans);
    return 0;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1010. Radix (25)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档