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 题意:已知其中一个数的字码以及进制和另一个数的字码,推断另一个数的进制。
思路:首先有可能出现的最大进制不是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[];
bool known , unknown;
ll sum1, sum2;
ll translate_to_10(string s,ll base) {//转化成十进制
ll sum = ;
for (int i = ; i < s.size();i++) {
if (s[i] >= ''&&s[i] <= '') {
sum = sum*base + s[i] - '';
}
else if (s[i] >= 'a'&&s[i] <= 'z') {
sum = sum*base + s[i] - 'a' + ;
}
}
return sum;
} bool C(ll x) {
ll sum = translate_to_10(num[unknown].number, x);
if (sum<)return true;//太大导致数值溢出
if (sum >= sum1)return true;
else return false;
} int main() {
while (cin>>num[].number>>num[].number>>tag>>radix) {
known = !(tag & ),unknown=!known;
num[known].base = radix;
sum1 = translate_to_10(num[known].number, num[known].base);
int strart_base = ; string s = num[unknown].number;
for (int i = ; i < s.size(); i++) {//确定未知数至少是多少进制
if (s[i] >= ''&&s[i] <= ''&&strart_base < s[i] - ''+)
strart_base = s[i] - ''+;
else if (s[i] >= 'a'&&s[i] <= 'z'&&strart_base < s[i] - 'a' + +)
strart_base = s[i] - 'a' + +;
}
ll lb = strart_base-, ub = sum1 + ;
if (translate_to_10(num[unknown].number, lb) > sum1) { puts("Impossible"); continue; }//剪枝
while (ub-lb>) {
ll mid = (lb + ub) >> ;
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 ;
}
pat 甲级 1010. Radix (25)的更多相关文章
- 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 ...
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT 甲级 1010 Radix
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- PAT甲组 1010 Radix (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
- 【PAT甲级】1010 Radix (25 分)(二分)
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
随机推荐
- C++大数问题
1.大数的加法 语法:add(char a[],char b[],char s[]); 参数: a[]:被加数,用字符串表示,位数不限 b[]:加数,用字符串表示,位数不限 s[]:结果,用字符串表示 ...
- MySQL - CASE WHEN ... THEN ... ELSE ... END语句
范例: CASE WHEN p.allow_over = 1 THEN p.allow_over_capital ELSE 0 END
- linux替换yum源及配置本地源
linux系统安装后自带的bash源由于在国外,安装软件包的时候会非常慢,最好替换一下yum源. 关于yum源的简单介绍 yum的主要功能是更方便地添加,删除和更新rpmba ...
- react与微信小程序
由组员完成 原文链接 都说react和微信小程序很像,但是像在什么部分呢,待我稍作对比. 生命周期 1.React React的生命周期在16版本以前与之后发生了重大变化,原因在于引入的React F ...
- GNU汇编程序框架
汇编的作用:1.对芯片进行初始化 2. 和C混合编程提升C的运行效率 .section .data < 初始化的数据> .section .bss <未初始化的数据> .sec ...
- 最新手机号正则表达式php包括166等号段
if(!preg_match("/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147)) ...
- java一些问题的解答
1.java 枚举类型和数据二进制等问题思考 以下代码的输出结果是什么?为什么会有这样的输出结果? int X=100; int Y=200; System.out.println("X+Y ...
- configParser模块详谈
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser configPars ...
- Python入门必学:递归函数正确的操作使用方法,案例详解
递归函数,在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以 ...
- 最短路径(最基础,经典的模板和思想):HDU-2544最短路
题目: 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...