PAT甲级 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 = 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 <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
// 变为10进制数
LL convert(string str, LL radix)
{
    LL len = str.length(), decimal = 0;
    for (LL i = 0; i < len; i++)
    {
        LL n = isdigit(str[i]) ? str[i] - '0' : str[i] - 'a' + 10;
        decimal += n * pow(radix, len - i - 1);
    }
    return decimal;
}
// 二分查找
LL find_radix(string str, LL n1)
{
    char n = *max_element(str.begin(), str.end());
    LL left = isdigit(n) ? n - '0' + 1 : n - 'a' + 11;
    LL right = n1 + 1;
    while (left <= right)
    {
        LL mid = (left + right) / 2;
        LL n2 = convert(str, mid);
        if (n2 < 0 || n2 > n1)
        {
            right = mid - 1;
        }
        else if (n2 == n1)
        {
            return mid;
        }
        else
        {
            left = mid + 1;
        }
    }
    return -1;
}
int main()
{
    string s1, s2;
    LL tag, radix;
    cin >> s1 >> s2 >> tag >> radix;
    if (tag == 2)
    {
        swap(s1, s2);
    }
    LL n1 = convert(s1, radix);
    LL rst = find_radix(s2, n1);
    if (rst != -1)
    {
        printf("%lld", rst);
    }
    else
    {
        printf("Impossible");
    }
    return 0;
}


先把已知进制数转化为十进制,然后未知进制数用不同进制转十进制代入比较,这里使用的是二分查找的方法进行优化。

目录
相关文章
|
存储 NoSQL Redis
Redis的Lua脚本有什么作用?
Redis Lua脚本用于减少网络开销、实现原子操作及扩展指令集。它能合并操作降低网络延迟,保证原子性,替代不支持回滚的事务。通过脚本,代码复用率提高,且可自定义指令,如实现分布式锁,增强Redis功能和灵活性。
513 1
|
Java 数据库连接 数据库
Spring事务简介及案例:模拟银行账号间转账业务
Spring事务简介及案例:模拟银行账号间转账业务
359 0
|
8月前
|
运维 jenkins 测试技术
Websoft9 面板是干什么的?
Websoft9 是一款专注于 Web 应用部署与管理的服务器面板工具,通过自动化脚本和图形化界面简化主流开源软件(如 WordPress、GitLab 等)的安装、配置和管理。它支持一键部署、集中管理和基础运维功能,降低技术门槛,适合个人站长、开发者及中小企业使用。提供丰富的应用市场、跨平台兼容性和完善的文档,帮助企业快速实现业务上线并减少运维成本。
262 1
Websoft9 面板是干什么的?
|
存储 druid 算法
磁盘管理工具
在Linux系统安装过程中,磁盘分区是一项重要步骤,可以通过Disk Druid、RAID、LVM等方式进行分区。此外,Linux还提供了fdisk、cfdisk、parted等分区工具。本文介绍了如何使用fdisk命令进行磁盘分区,包括创建、查看、删除分区以及格式化和挂载分区的具体操作步骤。通过这些步骤,可以有效地管理和优化磁盘资源,提高系统的安全性和性能。
382 2
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
264 7
|
JavaScript Java 测试技术
基于大学生社团活动管理的微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
基于大学生社团活动管理的微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
179 2
|
安全 网络协议 网络安全
【题目】2023年国赛信息安全管理与评估正式赛任务书-模块1
【题目】2023年国赛信息安全管理与评估正式赛任务书-模块1
【题目】2023年国赛信息安全管理与评估正式赛任务书-模块1
|
前端开发 JavaScript
vue实现通用分页控件,支持前端分页、后端分页。
vue实现通用分页控件,支持前端分页、后端分页。
317 1
|
算法 数据挖掘 数据处理
超实用!五种常用的多离散化小技巧
超实用!五种常用的多离散化小技巧
692 0