【PAT甲级 - C++题解】1010 Radix

简介: 【PAT甲级 - C++题解】1010 Radix

1010 Radix


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

Sample Output 1:

2


Sample Input 2:

1 ab 1 2
• 1

Sample Output 2:

Impossible

题意


N1 和 N 2 N2N2 是两个不超过 10 1010 位的数字,radix 是其中一个数字的进制,如果 tag 为 1 11,则 radix 是 N 1 N1N1 的进制,如果 tag 为 2 22,则 radix 是 N 2 N2N2 的进制。


输出使得 N 1 = N 2 N1=N2N1=N2 成立的另一个数字的进制数。


如果等式不可能成立,则输出 Impossible。


如果答案不唯一,则输出更小的进制数。

思路

这道题涉及到了秦九韶算法:

假设给定一个 d 进制 abc ,那么我要转化成十进制的公式为:a∗d2+b∗d1+c ,放到循环中其实就是:

//假设n为字符串
long long res = 0;
for(auto x : n)
  res = res * d + x - '0';


1.png

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
//将字符转换成数字
int get(char c)
{
    if (c <= '9')  return c - '0';
    return c - 'a' + 10;
}
//获得十进制值
LL calc(string n, LL r)
{
    LL res = 0;
    for (auto c : n)
    {
        //double存的数字比long long更大
        //如果已经大于1e16则一定大于target,直接返回一个超大值即可
        if ((double)res * r + get(c) > 1e16) return 1e18;
        res = res * r + get(c); //秦九韶算法
    }
    return res;
}
int main()
{
    string n1, n2;
    cin >> n1 >> n2;
    int tag, radix;
    cin >> tag >> radix;
    if (tag == 2)  swap(n1, n2);    //统一化处理
    LL target = calc(n1, radix);    //获得n1的十进制
    //获得二分的上边界和下边界
    LL l = 0, r = max(target, 36ll);
    for (auto c : n2)  l = max(l, (LL)get(c) + 1);
    //二分查找
    while (l < r)
    {
        LL mid = l + r >> 1;
        if (calc(n2, mid) >= target) r = mid;
        else    l = mid + 1;
    }
    if (calc(n2, r) == target) cout << r << endl;
    else puts("Impossible");
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
303 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
382 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
363 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
331 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
257 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
304 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
511 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
759 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)