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 N​1​​ and N​2​​, 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 <cstdio>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
typedef long long LL;
LL Map[256];
LL inf=pow(2,63)-1;void init(){for(char c='0';c<='9';c++){Map[c]=c-'0';}for(char c='a';c<='z';c++){Map[c]=c-'a'+10;}
}LL num10(char a[],LL radix,LL t){//将a转换为10进制,t为上界LL ans=0;int len=strlen(a);for(int i=0;i<len;i++){ans=ans*radix+Map[a[i]];if(ans<0||ans>t)return -1;}return ans;}int cmp(char N2[],LL radix,LL t){int len=strlen(N2);LL num=num10(N2,radix,t);if(num<0)return 1;//溢出,N2>t;if(t>num)return -1;//t较大,返回-1else if(t==num)return 0;//相等else return 1;//num较大
}LL binary(char N2[],LL left,LL right,LL t){LL mid;while(left<=right){mid=(left+right)/2;int flag=cmp(N2,mid,t);//N2转为10进制后与tj比较if(flag==0)return mid;else if(flag==-1)left=mid+1;else right=mid-1;}return -1;
}int find(char N2[]){int ans=-1,len=strlen(N2);for(int i=0;i<len;i++){if(Map[N2[i]]>ans){ans=Map[N2[i]];}}return ans+1;
}char N1[15],N2[15],temp[15];
int tag,radix;
int main(){init();scanf("%s %s %d %d",N1,N2,&tag,&radix);if(tag==2){swap(N1,N2);}
LL t=num10(N1,radix,inf);//将N1z转换为10进制
LL low=find(N2);//找到N2中最大的数位
LL high=max(low,t)+1;//上界
LL ans=binary(N2,low,high,t);
if(ans==-1)printf("Impossible\n");
else printf("%lld\n",ans);
return 0;}

1010 Radix (25 分) 超级坑恶魔坑相关推荐

  1. 【测试点分析】1010 Radix (25 分)_37行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...

  2. 1010 Radix (25 分)

    一.问题描述 输入两个数N1.N2,和一个标记tag.一个基数radix,其意义是:tag为1时,radix是N1的基数:tag为2时,radix是N2的基数.方便起见,以下假定tag为1,则目标就是 ...

  3. PAT Advanced—1010 Radix (25分)

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

  4. 1010 Radix (25 分)【难度: 难 / 知识点: 二分查找】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 注意: 答案可能不是[2,36] 而是一个非 ...

  5. pat 甲级 1010. Radix (25)

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

  6. [Java] 1010. Radix (25)-PAT甲级

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

  7. 【PAT - 甲级1010】Radix (25分)(二分,进制转化)

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

  8. PAT A1010 Radix (25 分)

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

  9. PAT 甲级 A1010 Radix (25 分)

    题目传送门 这个题用二分做,我自己写的二分呢太菜了,只能拿到19分,不放出来丢人了.后面看了yxc的代码,美妙绝伦哈. 慢慢来拜读一下. #include "bits/stdc++.h&qu ...

  10. PAT甲题题解-1010. Radix (25)-二分搜索

    题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...

最新文章

  1. 消息队列面试连环问:如何保证消息不丢失?处理重复消息?消息有序性?消息堆积处理?...
  2. Delphi XE 使用 MySQL 数据库一个奇怪的问题
  3. 深蓝学院《从零开始手写VIO》作业五
  4. CodeForces - 1457E New Game Plus!(贪心)
  5. phpize增加php模块
  6. js初化加载页面时ajax会调用两次的原因_在前端开发中,有哪些因素会导致页面卡顿
  7. win11如何自动启用禁用设置时区 windows11自动启用禁用设置时区的步骤方法
  8. Ubuntu安装maven
  9. vivado中文乱码更换中文等宽字体
  10. 使用Origin软件对数据进行指数拟合的方法
  11. 如何监控网页卡顿和崩溃?
  12. 月薪1万在中国是什么水平?答案揭露残酷的收入真相
  13. 06计算机优秀毕业论文-总结和展望
  14. oracle_java.exe,系统找不到C:\ProgramData\Oracle\Java\javapath\java.exe问题及解决方案
  15. 湖北工业大学计算机学院多少分录取,多少分可以上湖北工业大学?
  16. 开放云将使业务焕然一新
  17. 拼题A 7-7 计算存款利息
  18. 用 FC FOR nexenta 山寨SAN存储
  19. SetItemText
  20. Win勒索病毒害惨中国学生!微软:最新Win10很安全

热门文章

  1. Elasticsearch安装之安装Java环境
  2. linux screen 命令详解[转]
  3. CentOS下Red5安装
  4. 非域环境下安装并配置Project Server 2007(二)
  5. Microsoft Dynamic CRM 自定义重复检测功能
  6. 用css实现了一个精致的纵向导航菜单
  7. logitech鼠标接收器配对
  8. JavaSE基础篇之-Java 流(Stream)、文件(File)和IO
  9. android studio2.4,Android Studio 2.4 Preview 7 发布
  10. 多维动态数组c语言,C语言多维动态数组的实现