1010. Radix (25)

时间限制
400 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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

题意:已知其中一个数的字码以及进制和另一个数的字码,推断另一个数的进制。思路:首先有可能出现的最大进制不是36,可能会很大,所以直接暴力搜索的话很可能会卡时,所以需要二分搜索。这样我们只需确定另一个数字进制出现的可能范围[max_radix,min_radix]内进行二分搜索就行。AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30+5
#define M_MAX 2001
typedef unsigned long long ll;
int tag,radix;
struct Number {string number;ll base;
}num[2];
bool known , unknown;
ll sum1, sum2;
ll translate_to_10(string s,ll base) {//转化成十进制ll sum = 0;for (int i = 0; i < s.size();i++) {if (s[i] >= '0'&&s[i] <= '9') {sum = sum*base + s[i] - '0';}else if (s[i] >= 'a'&&s[i] <= 'z') {sum = sum*base + s[i] - 'a' + 10;}}return sum;
}bool C(ll x) {ll sum = translate_to_10(num[unknown].number, x);if (sum<0)return true;//太大导致数值溢出if (sum >= sum1)return true;else return false;
}int main() {while (cin>>num[0].number>>num[1].number>>tag>>radix) {known = !(tag & 1),unknown=!known;num[known].base = radix;sum1 = translate_to_10(num[known].number, num[known].base);int strart_base = 2; string s = num[unknown].number;for (int i = 0; i < s.size(); i++) {//确定未知数至少是多少进制if (s[i] >= '2'&&s[i] <= '9'&&strart_base < s[i] - '0'+1)strart_base = s[i] - '0'+1;else if (s[i] >= 'a'&&s[i] <= 'z'&&strart_base < s[i] - 'a' + 10+1)strart_base = s[i] - 'a' + 10+1;}ll lb = strart_base-1, ub = sum1 + 1;if (translate_to_10(num[unknown].number, lb) > sum1) { puts("Impossible"); continue; }//剪枝while (ub-lb>1) {ll mid = (lb + ub) >> 1;if (C(mid))ub = mid;else lb = mid;}if (translate_to_10(num[unknown].number, ub) == sum1){printf("%lld\n",ub);}else puts("Impossible");}return 0;
}

转载于:https://www.cnblogs.com/ZefengYao/p/8538946.html

pat 甲级 1010. Radix (25)相关推荐

  1. PAT甲级1010 Radix :[C++题解]进制位、秦九韶算法、二分(PAT通过率最低的一道题0.11)

    文章目录 题目分析 题目链接 题目分析 分析: 本题思路分两步. 第一步:先把给出数值和进制的数,暂定为N1,转换成10进制,即为target. 第二步: 判断一下N2在多少进制下是等于target的 ...

  2. PAT 甲级 A1010 Radix (25 分)

    题目传送门 这个题用二分做,我自己写的二分呢太菜了,只能拿到19分,不放出来丢人了.后面看了yxc的代码,美妙绝伦哈. 慢慢来拜读一下. #include "bits/stdc++.h&qu ...

  3. PAT Advanced—1010 Radix (25分)

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

  4. [Java] 1010. Radix (25)-PAT甲级

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  5. 【测试点分析】1010 Radix (25 分)_37行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...

  6. 【PAT - 甲级1010】Radix (25分)(二分,进制转化)

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

  7. PAT甲题题解-1010. Radix (25)-二分搜索

    题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...

  8. PAT甲级1010 (进制和二分法)

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

  9. 19年冬季第二题 PAT甲级 1166 Summit (25分)

    7-3 Summit (25分) A summit (峰会) is a meeting of heads of state or government. Arranging the rest area ...

最新文章

  1. 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
  2. android 模拟飞行,安卓版模拟飞行 X Plane 9试玩
  3. 2013年海康威视校园招聘笔试题
  4. 每日一笑 | 为什么Python比Java更受欢迎?
  5. 定义EJB 3.1视图(本地,远程,无接口)
  6. java stopself_然后,即使我停止了服务,Context.startForegroundService()也没有调用Service.startForeground()...
  7. 昆明第八中学2021高考成绩查询,昆明市第八中学2021年招生录取分数线
  8. 别让数据坑了你!用置信学习找出错误标注(附开源实现)
  9. 第 18 章 Policy
  10. 扁平化女装shop商城模板
  11. 【英语学习】【医学】Unit 02 The Brain and Its Functions
  12. mysql无法添加或更新子行_MYSQL:错误:无法添加或更新子行:外键约束失败
  13. .NET 下运用策略模式
  14. 做好嘈杂环境的语音识别,目前难点主要在哪里?
  15. javascript:typeof与instanceof区别
  16. 配置VSS2005的Internet访问(转)
  17. 测试之串口连接及调试
  18. 开源免费跨平台且超级傻瓜的视频分割的软件VidCutter(附CSDN下载)
  19. 小米note3android8.0,小米Note3 lineage16 安卓9.0 极致省电 纯净 完美root Xposed 经典版...
  20. 小白软件测试入门基础--测试用例

热门文章

  1. mysql外键约束脚本_使用SQL脚本创建数据库,操作主键、外键与各种约束(MS SQL Server)...
  2. 马希荣计算机应用,天津师范大学硕士研究生导师:马希荣
  3. pytorch torch.rand
  4. D3 Geographies
  5. 4.5 面部验证与二分类
  6. mysql客户端工具_性能优化-理解 MySQL 体系结构(MySQL分库分表)
  7. 负载均衡 > 用户指南 > 健康检查 > 健康检查概述
  8. 什么是云原生?聊聊云原生的今生(转)
  9. 载 Kubernetes和OpenStack到底是什么关系?先搞清楚,再系列学习
  10. RedHat Linux和Cent Linux日常运维之安全加固详细篇