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 N​1​​ and N​2​​, 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 <iostream>
#include <string>
#include<math.h>
#include <cctype>
#include <algorithm>
using namespace std; //最简单的一种一种格式试
int main()
{
string N1, N2;
long long int tag,radix;//防止溢出
cin >> N1 >> N2 >> tag >> radix;
if (tag == )
{
string N = N2;
N2 = N1;
N1 = N;
}
long long int aim = , n = ;//防止化为10制止时溢出
for (int i = N1.length() - , j = ; i >= ; ++j, --i)//将N1划为10进制
{
n = isdigit(N1[i]) ? (N1[i] - '') : (N1[i] - 'a' + );
aim += n * pow(radix, j);
}
long long int l, r, m;
char it = *max_element(N2.begin(), N2.end());//找出最大的元素
l = (isdigit(it) ? it - '' : it - 'a' + ) + ;//找到N2形成的最小进制
r = max(aim, l);
while (l <= r)
{
m = l + (r - l) / ;
long long int res = , n;
for (int i = N2.length() - , j = ; i >= ; ++j, --i)
{
n = isdigit(N2[i]) ? (N2[i] - '') : (N2[i] - 'a' + );
res += n * pow(m, j);
}
if (res == aim)
break;
else if (res< || res>aim)
r = m - ;
else
l = m + ;
}
if (l > r)
cout << "Impossible" << endl;
else
cout << m << endl; return ; }

PAT甲级——A1010 Radix的更多相关文章

  1. PAT甲级1010. Radix

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

  2. PAT 甲级 1010 Radix

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

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

  4. pat 甲级 1010. Radix (25)

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

  5. PAT A1010.Radix 二分法

    PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...

  6. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  8. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  9. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

随机推荐

  1. 新金融ABS如何做?听听这几十家券商、互金高管的经验之谈

    新金融ABS如何做?听听这几十家券商.互金高管的经验之谈 2016-11-24 零壹财经 ID:36104743 时间:2016年11月初 地点:北京东城区南湾子3号院(场地提供方:无讼.天同律师事务 ...

  2. django2 rest_framework + vue.js + mysql5.6 实现增删改查

    1.安装pymysql,mysqlclient,创建项目django-admin startproject django3 2.在Mysql中创建一个数据库叫django3db,打开项目,修改一下数据 ...

  3. iOS开发之SceneKit框架--SCNAction.h

    1.SCNAction简介 主要负责节点SCNNode的属性,实现node的渐变.移动.出现.消失.实现动画等. 2.相关API 节点的移动(earthNode的初始坐标(5,0,0)) //从当前位 ...

  4. (十一)Json文件配置

    接上一节,新建一个项目:JsonConfigSample 依然添加Microsoft.AspNetCore.All 在项目下新建一个Class.json配置文件 { ", "Cla ...

  5. Linux QtCreator 创建工程

    这一天天的,都快成废物了, 每天忙得要死, 各种乱七八糟杂事,连点学习的时间都没有了, 这才一年不碰Linux,创建工程都不会了, Ubuntu 1N.N.N + QtCreator 创建工程 不安装 ...

  6. Java开发系列-JDBC

    概述 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和 ...

  7. 网站PC端和移动端,用户通过设备识别

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!--<me ...

  8. 字符数组拷贝与strcpy函数

    代码: ],str2[]; ;i<;i++) { str1[i]='a'; } strcpy(str2,str1); 让找出错误的地方. 先来看下strcpy函数: 使用格式:char* str ...

  9. HDU 3923 Invoker | 暑训Day1 C题填坑

    暑训第一天,专题为组合数学与概率期望. 最近一个月都没有学习新的知识,上午听聚聚讲课头脑都是一片空白.加上长期没刷题,下午做练习题毫无感觉.到晚上总算理清了蓝书上的一些概念,跟着榜单做题.最后唯独剩下 ...

  10. python3-常用模块之序列化

    序列化 : 把其他的数据类型转换成 字符串或者bytes 序列 : 列表.元组.字符串.bytes 为什么要把其他数据类型转换成字符串? 能够在网络上传输的只能是bytes,能够存储在文件里的只有by ...