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;
}


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

目录
相关文章
|
安全 Java
Jprofile解析dump文件使用详解(一)
Jprofile解析dump文件使用详解(一)
1329 1
Jprofile解析dump文件使用详解(一)
|
存储 SQL 监控
22 PostgreSQL 监控3PostgreSQL 性能快照和图形化分析工具 pg_stats_info 的使用|学习笔记
快速学习22 PostgreSQL 监控3PostgreSQL 性能快照和图形化分析工具 pg_stats_info 的使用
22 PostgreSQL 监控3PostgreSQL 性能快照和图形化分析工具 pg_stats_info 的使用|学习笔记
基于宜搭的“设备报修”实践案例
设备报修是各企业、学校、医院等单位必不可少的应用场景,包括设备管理、用户报修、报修单管理、派单管理、维修管理等。那么,如何利用宜搭+钉钉实现高效的设备报修管理呢?
基于宜搭的“设备报修”实践案例
|
3月前
|
人工智能 自然语言处理 运维
从“念稿”到“对答如流”:大模型加持下的AI外呼产品推荐
本文对比传统与大模型外呼差异,以阿里云瓴羊Quick Service为例,解析其“听懂+办完”能力:意图识别准确率93.2%,支持改地址、催发货等闭环操作;多模型集成、合规内置、线路优化提升接通率。2025年大模型外呼渗透率达40%–60%,正加速替代传统IVR。
326 1
|
3月前
|
人工智能 前端开发 API
阿里云百炼平台两大入口说明:产品介绍官网、管理控制台,免费 7000 万 Tokens 领取地址
阿里云百炼是AI大模型服务平台,提供官网(产品介绍、计费说明、免费领7000万Tokens)与控制台(API Key管理、Token用量查询、模型配置)两大入口,支持按量付费及多种省钱方案。在阿里云百炼官网:https://t.aliyun.com/U/fPVHqY 免费领取千万Tokens
292 2
|
3月前
|
数据采集 存储 JSON
猫眼商品(票务 / 影片)数据获取完整方案
猫眼平台严禁爬虫抓取,官方无免费商用API。合规途径为对接猫眼商务或选用合规第三方封装接口。可获取影片、影院、排期、票价等数据,但逆向接口风险高、维护难。须严守使用边界:仅限内部分析,禁售数据、禁刷库存、限频访问。
478 1
|
4月前
|
人工智能 安全 Linux
DeployBrain AI - AI自动部署工具:说句话就能把项目部署上线
DeployBrainAI 是一款基于自然语言交互的智能部署工具,用户只需用中文描述需求(如"部署WordPress博客"或运行GitHub项目),AI即可自动完成环境检测、方案选择(Docker/源码等)、安装配置、服务启动及安全验证等全流程,支持Web项目、数据库、开源工具等多样化部署场景。 核心特点: 零基础友好:无需掌握Linux/Docker等技能,一句话触发自动化部署。
313 1
DeployBrain AI - AI自动部署工具:说句话就能把项目部署上线
|
4月前
|
编解码 人工智能 算法
视频超分省钱攻略:用「低分辨率生成 + 超分交付」把 AI 视频成本压掉一半
AI 视频直接出 1080p / 4K 往往是最贵的路径。这篇攻略以 Seedance 2.0 Mini 的 480p 为底片档位,拆解献丑 AI 视频节点两档超分(超分增强与 Topaz)的计费规则,给出低分辨率生成再超分的成本算法、分辨率与补帧的取舍原则,以及六条可直接照做的省钱操作。
951 1
|
5月前
|
安全 Android开发 前端开发
安卓云手机使用体验优化技术科普 2026 个人云端设备选购参考指南
2026安卓云手机选购指南:揭秘三大后台方案差异。前端联动易中断、定时请求频触发安全验证,而桃心云采用的“云端独立进程”方案——不改系统底层、画面与运行分离、断连不停服,兼顾流畅性与账号安全,是个人长期稳定使用的优选。(239字)
|
7月前
|
存储 安全 算法
密码泄露了?别慌!GitHub、微软、Google都在用的“虚拟MFA”,到底有多强?
密码易泄露?虚拟MFA(多因素认证)用手机动态验证码构筑第二道防线,即使密码失窃,攻击者也无法登录。基于TOTP算法,兼容Google/Microsoft Authenticator等应用,实现简单、成本低廉、安全高效。(239字)
715 1