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

注意点:1.left<=right;
    2.数据范围用long long
 #include<bits/stdc++.h>
using namespace std; long long digit[]; const long long inf = (1LL<<)-; void init(){ for(char c='';c<='';c++)
digit[c] = c-''; for(char c='a';c<='z';c++)
digit[c] = c-'a'+;
} long long convertNum10(string str1,long long radix,long long t){
long long ans=; long long len = str1.size(); for(long long i=;i<len;i++){
char c=str1[i];
ans=ans*radix+digit[c]; if(ans<||ans>t) return -;
} return ans; } long long cmp(string str2,long long radix,long long t){
long long n2=convertNum10(str2,radix,t);
if(n2<)return ;
else if(n2==t) return ;
else if(n2>t) return ;
else return -; } long long binary_search(string &str2,long long low,long long high,long long t){
long long left=low,right=high;
long long mid; while(left<=right){
mid=(left+right)/; long long flag=cmp(str2,mid,t); if(flag<)left =mid+;
else if(flag>)right = mid-;
else return mid;
} return -; } long long findLargest(string &str){
long long ans=-;
long long len=str.size(); for(long long i=;i<len;i++){
if(digit[str[i]]>ans)
ans=digit[str[i]];
} return ans+; } int main(){
string str1,str2;
long long tag,radix; cin>>str1>>str2>>tag>>radix; init(); if(tag==)
swap(str1,str2); long long t=convertNum10(str1,radix,inf); long long low = findLargest(str2); long long high=max(low,t)+; long long ans=binary_search(str2,low,high,t); if(ans==-)cout<<"Impossible\n";
else cout<<ans<<endl; }

1010 Radix (25 分)的更多相关文章

  1. 1010 Radix (25 分),PTA

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...

  2. PAT Advanced 1010 Radix(25) [⼆分法]

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

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

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

  4. 1010 Radix (25分)

    改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...

  5. 【PAT甲级】1010 Radix (25 分)(二分)

    题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...

  6. 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 ...

  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)

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

  9. 1010. Radix (25)(未完成)

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

随机推荐

  1. Jplayer用法

    引用js:jquery.jplayer.min.js; body里面必须有这个: <div id="jplayer"></div> jplayer停止方法 ...

  2. 设置 Quartus II 的仿真时间大于 1us

    Quartus II 仿真的默认时长是 1us. 设置时钟时看到 End time 想修改时长,把默认的 1us 改成 10us. 然后提示 End time 不合法.(只能设置为 0 到 1us) ...

  3. POJ 2254 Globetrotter (计算几何 - 球面最短距离)

    题目链接:POJ 2254 Description As a member of an ACM programming team you'll soon find yourself always tr ...

  4. PAT甲级——A1144 TheMissingNumber【20】

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

  5. Java 实例 - 方法重载

    先来看下方法重载(Overloading)的定义:如果有两个方法的方法名相同,但参数不一致,哪么可以说一个方法是另一个方法的重载. 具体说明如下: 方法名相同 方法的参数类型,个数顺序至少有一项不同 ...

  6. Django 模型层关系映射

    一.一对一映射 1.什么是一对一 A表中的一条记录只能与B表中的一条记录相关联如:一夫一妻制 2.语法 允许在关联的两个类的任何一个类中 增加: 属性 = models.OneToOneField(E ...

  7. 【纯净软件】三款照片EXIF信息删除软件 Clear Exif、JPEG & PNG Stripper、Easy Exif Delete 非专业横向对比

    商业软件:需支付费用后方可使用. 共享软件:需支付费用,但可以先免费试用(有使用期限.功能限制). 免费软件:无需支付费用,无使用期限,无功能限制. 纯净软件:无广告.无联网行为的免费软件. 自由软件 ...

  8. mySQL学习入门教程——2.创建表

    二.创建表 一.创建数据表的SQL语句模型(弱类型)CREATE TABLE [IF NOT EXISTS] 表名称(字段名1 列的类型[属性][索引],字段名2 列的类型[属性][索引],-字段名n ...

  9. Linux使用yum install 安装程序时,提示“另外一个程序锁定了 yum;等待它退出……”

    Linux使用yum install 安装程序时,提示“另外一个程序锁定了 yum:等待它退出……” 原因: yum命令一次只能安装一个软件,所以当你下载安装第二个软件包时,系统进程锁会锁定yum,这 ...

  10. python wave 库 读取 BytesIO 对象的注意事项

    程序中遇到需要使用临时文件时,常使用内存中的 io.BytesIO() 代替实体二进制文件,以避免磁盘IO,同时免去了考虑文件名的麻烦. file = io.BytesIO() file.write( ...