题目

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

题目分析

已知两个数(最多有10位),其中一个数的基数,求使两个数相等的另一个数的基数,

解题思路

  1. 二分查找,寻找另一个数的基数,使得两个数字十进制相等

易错点

  1. 已知数字最多有10位,如果基数大于10,可能会超过int范围,需要使用long long
  2. 暴力枚举会超时,需要使用二分查找
  3. 未知基数数字的进制,下界为所有数位中最大数+1,上界为max(基准数十进制,下界)(如:100000 10第一个数字基数为10,第二个数字基数为1000000也能使两个数相等)
  4. 未知基数的数字转换为10进制后,可能溢出(为负值),表示基数太大要high=w(若没有该判断,测试点:10 12 13 15 16 6 8会错误)

知识点

  1. 任意进制转换为十进制
long long conver_radix(long long radix,string vs) {
long long ans = 0,index=0;
for(int i=vs.length()-1; i>=0; i--) {
int temp=isdigit(vs[i])?vs[i]-'0':vs[i]-'a'+10;
ans += temp*pow(radix,index++);
}
return ans;
}

Code

Code 01

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <climits>
using namespace std;
long long conver_radix(long long radix,string vs) {
long long ans = 0,index=0;
for(int i=vs.length()-1; i>=0; i--) {
int temp=isdigit(vs[i])?vs[i]-'0':vs[i]-'a'+10;
ans += temp*pow(radix,index++);
}
return ans;
}
int main(int argc,char * argv[]) {
string N1,N2,w,y;
long long tag,radix;
cin>>N1>>N2>>tag>>radix;
if(tag==1) w=N2,y=N1;
else w=N1,y=N2;
long long ys=conver_radix(radix,y); //已知进制数的十进制
char it = *max_element(w.begin(),w.end());
long long low=(isdigit(it)?it-'0':it-'a'+10)+1; //进制下界:找到最大数字+1, 若最大数字为9,那么最小进制应该为10
long long high=max(low,ys); //进制上界:基准十进制数与下界取较大,INT_MAX不对,第一个测试点错误
long long m = low+((high-low)>>1);
while(low<=high) {// 二分查找
m = low+((high-low)>>1);
// m作为基数,转换数字到十进制,与另外一个数字十进制比较
long long ws = conver_radix(m,w);
if(ws==ys) {
break;
} else if(ws<0||ws>ys) {
high=m-1;
} else {
low=m+1;
}
}
if(low>high)printf("Impossible");
else printf("%d",m);
return 0;
}

PAT Advanced 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 (25)

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

  3. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

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

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

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

  5. 1010 Radix (25 分),PTA

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...

  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. 1010 Radix (25分)

    改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...

  8. PAT甲级1010. Radix

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

  9. PAT甲组 1010 Radix (二分)

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

随机推荐

  1. Java Integer Addition Subtration Overflow 整数加减溢出

    leetCode有道题Reverse Integer,因为int的最大值为2的31次方减一,最小值为-2的31次方. 我一开始的代码将res递归加放在try中,以为溢出会有异常,然而并没有. 因为出传 ...

  2. 【OJ2216】小奇的数列

    题目大意 : 给定一个长度为 \(n\) 的数列,以及 \(m\) 次询问,每次给出三个数 \(l\),\(r\) 和 \(P\),询问 \((\sum_{i=l_1}^{r_1}a_i)\;mod\ ...

  3. Spark SQL 笔记

    Spark SQL 简介 SparkSQL 的前身是 Shark, SparkSQL 产生的根本原因是其完全脱离了 Hive 的限制.(Shark 底层依赖于 Hive 的解析器, 查询优化器) Sp ...

  4. Linux预习第三章节《重定向与管道符》20200219

  5. 为什么vue中的data用return返回

    1.为什么在项目中data需要使用return返回数据呢? 不使用return包裹的数据会在项目的全局可见,会造成变量污染:使用return包裹后数据中变量只在当前组件中生效,不会影响其他组件. 当一 ...

  6. SPOJ - AMR11H Array Diversity (排列组合)

    题意:给定n个数,求包含最大值和最小值的子集(数字连续)和子序列(数字不连续)的个数. 分析: 1.如果n个数都相同,则子集个数为N * (N + 1) / 2,子序列个数为2N-1. 2.将序列从头 ...

  7. Day2-T4

    原题目 当然这是原题+,要输路径的.所以必须DFS. Describe:DP or DFS code: #include<bits/stdc++.h> using namespace st ...

  8. java课程之团队开发冲刺阶段2.7

    昨日总结: 1.完整实现课前闹钟提醒功能 遇到的困难: 1.没有遇到大的问题,细节地方没有处理好出现了一下小的情况 今天的任务: 1.实现对课程查询的完整实现 当日总结: 1.以前是使用二级联动下拉框 ...

  9. mini2440 裸机程序,下载到nand 里面,复制到 sdram 中不运行

    按照韦东山的视频中 sdram的裸机代码,写了一份,通过 minitools 下载到 0x30000000,然后烧录到 nand中,接过不能正常运行. 尝试过多种方法后,只有一种解决方法,就是不要用 ...

  10. 关于Wasserstein GAN的一些笔记

    这篇笔记基于上一篇<关于GAN的一些笔记>. 1 GAN的缺陷 由于 $P_G$ 和 $P_{data}$ 它们实际上是 high-dim space 中的 low-dim manifol ...