1010 Radix (25 分)

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

算法说明:

已知一个数和其基数,求另一个数的基数使得这两个数相等。数字表示使用[0-9a-z],很容易看出基数的范围是[2-36],如果已知数很大,基数是会超出36的,可能会很大很大,用long long 来存储这个基数,如果你用暴力遍历查找基数,时间会超时,可以用二分查找。

#include <iostream>
#include <cstring>
using namespace std;
#define Max 3
// 转成十进制
long long toDecimal(char *N,long long radix){
long long decimal=0;
for(int i=0;i<strlen(N);i++){
if(N[i]<58)
decimal=decimal*radix+(N[i]-48);
else
decimal=decimal*radix+(N[i]-87); // a-z ,10-35 呵呵
}
return decimal;
}
//找出串中最大值
int maxValueStr(char *N){
int max=0;
for(int i=0;i<strlen(N);i++)
if(N[i]<58&&N[i]-48>max)
max=N[i]-48;
else if(N[i]-87>max)
max=N[i]-87;
if(max>1)
return max;
else
return 1;
}
int compare(char *N,long long radix,long long target){
long long decimal=0;
for(int i=0;i<strlen(N);i++){
if(N[i]<58)
decimal=decimal*radix+(N[i]-48);
else
decimal=decimal*radix+(N[i]-87);
if(decimal>target||decimal<0)
return 1;
}
if(decimal>target)
return 1;
else if(decimal<target)
return -1;
else
return 0;
}
long long binarySearch(long long target,char *N,long long low,long long high){
long long mid=low; while(low<=high){
if(compare(N,mid,target)==1)
high=mid-1;
else if(compare(N,mid,target)==-1)
low=mid+1;
else
return mid;
mid=(low+high)/2;
}
return -1;
}
int main(int argc, char* argv[])
{
char *N[Max]; long long radix,target;
int tag;
for(int i=0;i<Max;i++)
N[i]=new char[10](); cin >> N[1] >> N[2] >> tag >> radix; target=toDecimal(N[tag],radix); //目标数转成十进制比较
tag=(tag==1)? 2:1;
long long max=(long long)maxValueStr(N[tag])+1; // 出现最大值f 进制为16 +1 if(target<=max){
long long result=binarySearch(target,N[tag],max,36);
if(result==-1)
cout<<"Impossible";
else
cout<<result;
}else{
long long result=binarySearch(target,N[tag],max,target+1);
if(result==-1)
cout<<"Impossible";
else
cout<<result;
}
return 0;
}

2020考研打卡第十三天,星辰之变,骄阳岂是终点。

我要争一口气,不是想证明我了不起,我是要告诉大家,我失去的东西一定要拿回来。

PAT-1010 Radix的更多相关文章

  1. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

  2. 已经菜到不行了 PAT 1010. Radix (25)

    https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...

  3. PAT 1010 Radix 进制转换+二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  4. PAT 1010 Radix (二分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  5. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  6. PAT甲级1010. Radix

    PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...

  7. PAT 解题报告 1010. Radix (25)

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  8. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  9. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  10. PAT甲组 1010 Radix (二分)

    1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...

随机推荐

  1. git 命令行下浏览器tig使用记录

    git 命令行下浏览器tig使用记录 tig 是一款优化 git 命令行的工具,使 git 命令行更加的便捷人性化 .如果用习惯了,会上瘾. 以下是一些使用记录: 安装成功后,在 Repo 文件夹下, ...

  2. IIS 安全设置

    这近网站中毒,以下把IIS安全设置记录一下,以便查阅. 1.对于不需要执行的目录,将处理程序映射中的编辑功能权限中的脚本去掉,这样即使上传了木马文件在此目录,也是无法执行的. 删除IIS默认的匿名用户 ...

  3. 4星|《门口的野蛮人2》:美国宝万之争专业户KKR公司的疯狂借债收购史

    门口的野蛮人2:KKR与资本暴利的崛起(珍藏版) 英文版是1992年出的.主要内容是1977-1998年之间KKR在美国的杠杆收购简史.从KKR创立开始,讲到1990年KKR差点倒闭.国内A股市场上前 ...

  4. PyQt5--EventSender

    # -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing Comment: This example is ...

  5. Java中Map根据键值(key)或者值(value)进行排序实现

    我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...

  6. 'No Transport' Error w/ jQuery ajax call in IE

    I need to use foursquare API to search venues. Of course it is cross-domain. It has no any problems ...

  7. cookie的详解

    cookie是如何出生的 由于HTTP协议是无状态的,而服务器端的业务必须是要有状态的.Cookie诞生的最初目的是为了存储web中的状态信息,以方便服务器端使用.比如判断用户是否是第一次访问网站.目 ...

  8. Python csv.md

    csv csv模块可以用于处理从电子表格和数据库导出的数据到带有字段和记录格式的文本文件,通常称为逗号分隔值(csv)格式,因为逗号通常用于分隔记录中的字段. Reading csv.reader(c ...

  9. 3.HBase In Action 第一章-HBase简介(1.1.1 大数据你好呀)

    Let's take a closer look at the term Big Data. To be honest, it's become something of a loaded term, ...

  10. P1231 教辅的组成

    传送门:https://www.luogu.org/problemnew/show/P1231 这是一道很不错的网络流入门题,关键在于如何建图. 首先,我们将练习册和源点连一条边权为1的边,然后若书 ...