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. Chrome下font-size小于12px的解决办法

    自从Chorme取消了-webkit-text-size-adjust,这个问题又变得令人烦恼起来. 好在我们可以利用-webkit-transform这个私有属性. .box{ -webkit-tr ...

  2. RHEL 6.5----haproxy实现负载均衡

    主机名 IP master 192.168.30.130 node-1 192.168.30.131 node-2 192.168.30.132 在master上安装 [root@master ~]# ...

  3. Java 中 i++和++i的区别

    public class Test{ public static void main(String [] args){ int i = 1; int s = ++i; int x= i++; Syst ...

  4. 导Excel数据表

    需要把EXcel转换格式:

  5. 【经验总结】北邮OJ

    90. 字符串转换 时间限制 1000 ms 内存限制 65536 KB 题目描述 我们将仅由若干个同一小写字母构成的字符串称之为简单串,例如"aaaa"是一个简单串,而" ...

  6. 【Jenkins】Jenkins配置从节点,实现远程主机调用功能

    一.需求 使用Jenkins进行持续集成部署过程中,需要用到远端主机的处理功能.如部署到远程主机.文件备份等功能 二.思路 1.当远端主机为Linux系统时使用Publish Over SSH Plu ...

  7. COGS 2111. [NOIP2015普及]扫雷游戏

    ★   输入文件:2015mine.in   输出文件:2015mine.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 扫雷游戏是一款十分经典的单机小游戏.在 n 行 ...

  8. 洛谷 P1455 搭配购买

    题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经 ...

  9. faster rcnn的改进方向

    http://blog.csdn.net/linolzhang/article/details/74159463 http://blog.csdn.net/linolzhang/article/det ...

  10. CWnd::Updata的作用

    CWnd::Updata的作用 CWnd::UpdateData 调用此成员函数以在对话框中初始化数据,或者取回和验证对话框数据. BOOL UpdateData(BOOL bSaveAndValid ...