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<map>
using namespace std; map<char,long long> mm; long long getlow(char n[])
{
long long Max = -;
for(long long i = ; i < strlen(n); i++)
{
if(Max < mm[n[i]])
Max = mm[n[i]];
}
return Max+;
} long long getx(char n[],long long radix)
{
long long sum = ;
for(long long i = ; i < strlen(n);i++)
{
sum = sum * radix + mm[n[i]];
}
return sum;
} long long compare(char n[],long long x,long long radix)
{
long long sum = ;
for(long long i = ; i < strlen(n);i++)
{
sum = sum * radix + mm[n[i]];
if(sum > x ) return ;
} if(sum == x) return ;
if(sum < x) return -;
} long long getresult(long long low,long long high,char n[],long long x)
{
//mid 要从low 开始,因为 当两个数相等的时候,它的进制数的最小值是low
long long mid = low ;
while(low <= high)
{
long long b = compare(n,x,mid);
if(b == ) return mid;
else if(b == )
{
high = mid -; }
else if(b == -)
{
low = mid+;
} mid = (low + high)/;
} return -;
} int main()
{
long long i;
for(i = '' ; i <= '';i++)
mm[i] = i - ''; for(i = 'a' ; i <='z' ; i++)
mm[i] = + i - 'a'; char n1[];
char n2[];
char ctem[];
long long tag;
long long radix;
scanf("%s%s%lld%lld",n1,n2,&tag,&radix);
if(tag == )
{
strcpy(ctem,n2);
strcpy(n2,n1);
strcpy(n1,ctem);
} long long x = getx(n1,radix); long long low = getlow(n2);
//上限是 x+1 : 10 9999 2 10
//low是 N2某一位上的数,而x 是N1,
//low 某一位上的数就比N1 大,
//那么N2无论什么进制,N1 和 N2肯定是不相等的。
//所以不用判断 low 和 x 的 大小
long long high = x + ;
long long result = getresult(low,high,n2,x);
if(result == -) printf("Impossible\n");
else
{
printf("%lld\n",result);
} 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 ...
- 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 ...
- 1010. Radix (25)(出错较多待改进)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
随机推荐
- Webservice简单概念
一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...
- Nginx - Events Module
The Events module comes with directives that allow you to configure network mechanisms. Some of the ...
- Slickflow.NET 开源工作流引擎基础介绍(六)--模块化架构设计和实践
前言:在集成Slickflow.NET 引擎组件过程中,引擎组件需要将用户,角色等资源数据读取进来,供引擎内部调用:而企业客户都是有自己的组织架构模型,在引入模块化架构设计后,引擎组件的集成性更加友好 ...
- 把mvc4彻底搞定(一)
1 .net web开发方式 webform开发 asp.net mvc开发 2 请求模式 webform方式:客户端向服务端请求一个aspx页面,服务端生成一个页面对象类,调用这个页面对象类的PR方 ...
- 分享我的2014年3月unity3d面试题与参考答案
今年3月份面试unity3d游戏程序,先做的面试题,然后人事问了我几个之前游戏开发的问题,问我英语怎么样等等,介绍了公司情况和对unity3d程序的要求.之后就回来了,总共面了不到1个半小时吧.笔试答 ...
- HDOJ2025查找最大元素
查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 解析LRC歌词文件readlrc
package com.jikexueyuan.readlrc.main; import com.jikexueyuan.readlrc.utils.Utils; import java.io.Fil ...
- request对象实现请求转发
request对象实现请求转发,请求转发指一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理.request对象提供了一个getRequestDispatcher方法,该方法返 ...
- Java之绘制二次曲线
前面已经介绍过绘制方法.这里不再赘述. 画一下草图来看看:灰色表示X值,红色表示Y值. 在这里要值得注意的是:当我们把它们的坐标在纸上画出来时,就会觉得“有问题”了,你看A(60,20,120,100 ...
- TTTAttributedLabel 如何将多个字符串高亮显示
TTTAttributedLabel进行多个字符串的高亮显示. 需要对每个字符串进行匹配,从而得到所有需要高亮的NSRange,然后利用NSMutableAttributedString对每个NSRa ...