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
#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)(出错较多待改进)的更多相关文章
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- 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 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- 1010. Radix (25)(未完成)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 1010. Radix (25)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- 1010. Radix (25) pat
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 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 ...
随机推荐
- c#学习系列之关键字where
where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量. 1.接口约束. 例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就 ...
- 为RecyclerView打造通用Adapter 让RecyclerView更加好用
原文出处: 张鸿洋 (Granker,@鸿洋_ ) 一.概述 记得好久以前针对ListView类控件写过一篇打造万能的ListView GridView 适配器,如今RecyclerView异军突起, ...
- 浏览器的差距、ie6 ie7 ie8、符号、html css、BFC、
1.浏览器的差距 浏览器默认的字体是16px,谷歌的最小字体是12px,其他是10px 2.ie6.ie7.ie8. hack:就是针对不同的浏览器去不同的html,css样式,从而让各个浏览器能达到 ...
- JSP自定义标签开发步骤
自定义的标签库一.基本概念: 1.标签(Tag): 标签,通常也成为动作,是一组按照XML语法格式编写的代码片段,在JSP中,用来封装在页面中可重复利用的逻辑,通过标签可以使JSP网页变得简洁并且易于 ...
- git diff查看修改,出现^M换行问题
通过命令git diff查看修改,出现^M换行问题,如图: 解决: git config --global core.whitespace cr-at-eol 换行符的问题: 提交时转换为LF,检出时 ...
- Win2D 入门教程 VB 中文版
继续填坑!又一个c#教程变为vb! 这是我翻译的Win2D教程,链接保留了微软原版的. 如果文档有问题,可以在 https://github.com/Nukepayload2/Win2dDocVB发 ...
- Elasticsearch 插入地理索引文档一直为空
今天在获取插入索引数据的时候,一直提示插入不成功,尝试了很多方法,原来是因为在插入的时候应该先 插入Latitude后插入longitude修改后的代码如下 public boolean insert ...
- Java后端学习路线_备战
SpringCloud项目实战 Dubbo项目实战 项目实战应涵括哪些技术: 缓存.消息队列 WEB应用服务器(Weblogic.Jetty.JBoss.WebSphere) NoSQL(MongoD ...
- sqlserver 数据库主外键关联错误
话题引入: 在建立主外键关系时,系统提示表"table2"中的列与现有的主键或UNIQUE约束不匹配 原因: 数据库表中只有一个主键,这个主键可以是多个列共同组成.所以table2 ...
- 指针-AC自动机
大家都不喜欢指针,但是这个AC自动机仿佛不用不行…… 先引用我最喜欢的话:“AC自动机,不是自动AC的机器.” 如果写不好还可能一直WA AC自动机是KMP与Trie树的完美结合,适用于多字符串匹配, ...