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<stdio.h>
#include<string.h>
#include<stdlib.h>
long long binarySearch();
int cmp( long long k);
char a[],b[],c[];
long long ans[];
long long low,high,len,valuea;
int main()
{
int tag;
long long radixa,temp,ret;
int i;
scanf("%s %s %d %lld",a,b,&tag,&radixa);
if( tag==)
{
strcpy(c,a);
strcpy(a,b);
strcpy(b,a);
}
for( i=; a[i]!='\0'; i++)
{
if( a[i]>='' && a[i]<='')
temp = a[i]-'';
else if( a[i]>'a' && a[i]<'z')
temp = a[i]-'a'+;
valuea = valuea*radixa + temp;
} for( i=; b[i]!='\0'; i++)
{
if( b[i]>='' && b[i]<='')
temp = b[i]-'';
else if( b[i]>'a' && b[i]<'z')
temp = b[i]-'a'+;
ans[i]=temp;
if( low<temp)
low = temp;
}
low++;
len = strlen(b);
if( low>valuea)
high = low+;
else high=valuea+;
ret = binarySearch();
if( ret==-)
printf("Impossible\n");
else printf("%lld\n",ret);
return ;
}
long long binarySearch()
{
long long l=low,h=high,mid;
while( l<=h )
{
mid = (l+h)/;
if(cmp(mid)==)
return mid;
else if(cmp(mid)<)
l= mid+;
else h=mid-;
}
return -;
} int cmp( long long k)
{
long long valueb=;
int i;
for( i=; i<len; i++)
valueb = k*valueb+ans[i];
if( valueb< || valueb>valuea)
return ;
else if( valueb<valuea)
return -;
else if ( valuea==valueb)
return ;
}

1010. Radix (25)(出错较多待改进)的更多相关文章

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

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

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

  3. pat 甲级 1010. Radix (25)

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

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

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

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

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

  6. 1010. Radix (25)

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

  7. PAT (Advanced Level) 1010. Radix (25)

    撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...

  8. 1010. Radix (25) pat

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

  9. 1010 Radix (25)(25 point(s))

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

随机推荐

  1. HDU 2828 Lamp 二分图的最大匹配 模型题

    http://acm.hdu.edu.cn/showproblem.php?pid=2828 给定n个灯,m个开关,使得每栈灯亮,前提是控制这栈灯的开关的状态是其中一个.(题目应该都看得懂) 其实我想 ...

  2. RHEL 6.5----rsync+inotify数据同步服务

    Rsync特性: 可以镜像保存整个目录树和文件系统: 可以保持原文件的权限.时间.软硬链接等: 安装简单. 传输特点: 速度快:rsync首次同步会复制同步全部内容,以后只传输修改过的文件: 压缩传输 ...

  3. IO(下)

    7. 标准输入.输出流 7.1 标准输入流 源数据源是标准输入设备(键盘.鼠标.触摸屏)等输入设备.在java中用http://System.in 得到一个 InputStream 字节输入流. 需求 ...

  4. ag-grid-vue 本地删除,不重新刷新数据

    // 这是本地删除,不重新刷新数据 that.gridListOptions.api.updateRowData({ remove: [that.submitTransmitData] });

  5. 让WPS10显示为offic97效果

    让WPS10显示为offic97效果2019/1/26 22:02 OS:win7 64位使用的WPS_10.1.0.5603_setup.1460689247.exe 衣不如旧,人不如新.最开始接触 ...

  6. Linux常用终端快捷键

    UNIX程序员对键盘以及快捷键的设置都遵循一个标准:"手移动最少的距离,作更多的操作." 所有的类UNIX的终端上都有一些快捷键Ctrl+n = 下,Ctrl+b = 左,Ctrl ...

  7. upload 上传按钮组件 iview

    <!-- * @description 导入Excel * @fileName importExcel.vue * @author 彭成刚 * @date // :: * @version V1 ...

  8. Microsoft Windows Server 部署

    Microsoft Windows Server 部署 多重引导 计算机可以被设置多重引导,即在一台计算机上安装多个操作系统..在安装多重引导的操作系统时,还要注意版本的类型,一般应先安装版本低的,再 ...

  9. python编码的初识

    用途: ​ 密码本:二进制 与 文字的对应关系 ASCII: ​ 最早的密码本:二进制与 英文字母,数字,特殊字符的对应关系 格式: 01100001 a 01100010 b 字节数: ​ 英文1个 ...

  10. JavaEE-06 Servlet基础

    学习要点 Servlet生命周期 Servlet API的常用接口和类 Servlet的部署和配置 Servlet处理用户请求 Servlet JSP回顾 JSP技术开发web应用在服务器端生成网页的 ...