1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

题意:已知其中一个数的字码以及进制和另一个数的字码,推断另一个数的进制。
思路:首先有可能出现的最大进制不是36,可能会很大,所以直接暴力搜索的话很可能会卡时,所以需要二分搜索。这样我们只需确定另一个数字进制出现的可能范围[max_radix,min_radix]内进行二分搜索就行。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30+5
#define M_MAX 2001
typedef unsigned long long ll;
int tag,radix;
struct Number {
string number;
ll base;
}num[];
bool known , unknown;
ll sum1, sum2;
ll translate_to_10(string s,ll base) {//转化成十进制
ll sum = ;
for (int i = ; i < s.size();i++) {
if (s[i] >= ''&&s[i] <= '') {
sum = sum*base + s[i] - '';
}
else if (s[i] >= 'a'&&s[i] <= 'z') {
sum = sum*base + s[i] - 'a' + ;
}
}
return sum;
} bool C(ll x) {
ll sum = translate_to_10(num[unknown].number, x);
if (sum<)return true;//太大导致数值溢出
if (sum >= sum1)return true;
else return false;
} int main() {
while (cin>>num[].number>>num[].number>>tag>>radix) {
known = !(tag & ),unknown=!known;
num[known].base = radix;
sum1 = translate_to_10(num[known].number, num[known].base);
int strart_base = ; string s = num[unknown].number;
for (int i = ; i < s.size(); i++) {//确定未知数至少是多少进制
if (s[i] >= ''&&s[i] <= ''&&strart_base < s[i] - ''+)
strart_base = s[i] - ''+;
else if (s[i] >= 'a'&&s[i] <= 'z'&&strart_base < s[i] - 'a' + +)
strart_base = s[i] - 'a' + +;
}
ll lb = strart_base-, ub = sum1 + ;
if (translate_to_10(num[unknown].number, lb) > sum1) { puts("Impossible"); continue; }//剪枝
while (ub-lb>) {
ll mid = (lb + ub) >> ;
if (C(mid))ub = mid;
else lb = mid;
}
if (translate_to_10(num[unknown].number, ub) == sum1){
printf("%lld\n",ub);
}
else puts("Impossible");
}
return ;
}

pat 甲级 1010. Radix (25)的更多相关文章

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

  2. PAT甲级1010. Radix

    PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...

  3. PAT 甲级 1010 Radix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...

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

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

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

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

  6. PAT甲组 1010 Radix (二分)

    1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...

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

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

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

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

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

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

随机推荐

  1. 已解决: idea创建并部署SpringMVC项目时 报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    用IDEA创建并运行SpringMVC项目时,最初发现没有Servlet包,这个问题已在上篇解决,然而当我们尝试去运行此时的SpringMVC项目时,发现仍然有错误.ClassNotFoundExce ...

  2. SummerVocation_Learning--java的线程死锁

    public class Test_DeadLock implements Runnable { ; static Object o1 = new Object(),o2 = new Object() ...

  3. 100个linux系统常用指令

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件-A 通-a,但不列出"."和".."-l 列出文件 ...

  4. 利用PHPExcel 实现excel数据的导入导出(源码实现)

    利用PHPExcel 实现excel数据的导入导出(源码实现) 在开发过程中,经常会遇到导入导出的需求,利用phpexcel类实现起来也是比较容易的,下面,我们一步一步实现 提前将phpexcel类下 ...

  5. Python9-MySQL-MySQL存储过程-视图-触发器-函数-day45

    视图:某个查询语句设置别名,日后方便使用 CREATE VIEW v1 as SELECT * FROM student WHERE sid >10 -创建: create view 视图名称 ...

  6. for循环+canvas实现黑客帝国矩形阵

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  7. 谭浩强C第四版(p141)16.输出以下图案

    运行结果: * *** ***** ******* ***** *** * Press any key to continue #include<stdio.h> int main() { ...

  8. 饭卡 HDU - 2546(dp)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...

  9. vba中获取当前sheet页的名称,当前单元格所在位置

    fname = ActiveSheet.Name-------获取当前sheet页的名称        Sname = "" & fname & "&qu ...

  10. 线程、进程、队列、IO多路模型

    操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列.生产者消息者模型.异步模型.IO多路复用模型.select\poll\epoll 高性 ...