PAT 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.
思路
输出可以使得N1=N2的最小进制。
因为此题的进制上限是正无穷,因此考虑使用二分法来挑选最佳进制。因此,问题可以转化为如何选择进制的最大值与最小值。
小技巧
①如果flag=2,那么swap(a,b )
②用string来存字符串,统一将字符转换为对应的整数,方便运算,见代码中的init()
函数(如果用char[]来存,就不能这么操作了,因为'0'-'0' = '\0',strlen函数会出问题)。
这个题有比较坑的地方
①long long类型有可能溢出。这就造成了如果N1的范围大于了long long的范围,有可能会编程负数。我看一些博客中,简单的加了一个判断条件N2 < 0
。但是面对一个超范围的数字,应该不能简单的判断N1与N2的大小(例如N2 = -1,但有可能是LLONG_MAX + LLONG_MAX + 1;而N1 = 8,但有可能是LLONG_MAX + LLONG_MAX + 9,这个时候通过N2<0,来作为N2>N1的判断条件是不对的)。好在这题的数据没有考虑的这个问题。
②一定要注意进制的最大值是无穷,而不是35。但是最大值是可以确定的。设N1对应的十进制为num,那么N2的最大进制不超过num。因为N2越小,最佳进制就要越大。N2的最小值为1(最佳进制下;0的话num一定也满足),那么num就已经足够了。另外要保证最大进制大于最小进制,因此代码中有个max操作。
代码
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <limits.h>
using namespace std;
string a, b;
long long flag, radex;
long long minn = INT_MIN, maxx = INT_MIN;
long long to10(string s, long long radex){
long long p = 1;
long long sum = 0;
for(int i = s.length() - 1; i >= 0; i--){
sum = sum + p * s[i];
p *= radex;
}
return sum;
}
void prove(long long dec){
while(minn <= maxx){
long long mid = (minn + maxx) >> 1;
long long num = to10(b, mid);
if(num < 0 || num > dec){
maxx = mid - 1;
}
else if(num == dec){
cout << mid;
return;
}
else{
minn = mid + 1;
}
}
cout << "Impossible";
}
void init(){
for(int i = 0; i < a.length(); i++){
if(isdigit(a[i])) a[i] = a[i] - '0';
else a[i] = a[i] - 'a' + 10;
}
for(int i = 0; i < b.length(); i++){
if(isdigit(b[i])) b[i] = b[i] - '0';
else b[i] = b[i] - 'a' + 10;
}
}
int main() {
cin >> a >> b >> flag >> radex;
if(flag == 2) swap(a, b);
init();
minn = *max_element(b.begin(), b.begin()) + 1;
long long dec = to10(a, radex);
maxx = max(dec, minn);
prove(dec);
return 0;
}
PAT 1010 Radix (二分)的更多相关文章
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- PAT 1010 Radix 进制转换+二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT 1010 Radix (25分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- 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 (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
- 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
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT Radix[二分][进制转换][难]
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
随机推荐
- 洛谷P1177 【模板】快速排序
https://www.luogu.org/problem/P1177 #include<bits/stdc++.h> using namespace std; int n; multis ...
- Java TreeSet集合 比较器排序Comparator的使用
比较器排序Comparator的使用 存储学生对象,并遍历,创建TreeSet集合使用带参构造方法 要求,按照学生年龄从小到大排序,如果年龄相同,则按照姓名的字母循序排序 结论 用TreeSet集合存 ...
- vs2010安装
1.网盘上2010安装包可用,先下载到电脑上,然后找到setup文件,安装即可 2.下载插件,基本的文本对齐,tab键补齐等功能 3.测试hello world程序 4.出现的问题 一个文件夹下有多个 ...
- 题解【洛谷P1379】八数码难题
题面 典型的\(\text{BFS}\). 双向广搜是一种对\(\text{BFS}\)的优化,它适用于起点和终点都明确的题目. 这里给出我的双向广搜模板. inline int bfs()//双向广 ...
- 在CentOS上通过Docker方式安装Redis
在CentOS上通过Docker方式安装Redis 首先保证已经安装docker. 拖拽redis镜像 docker pull redis 启动redis docker run -p 6379:637 ...
- ansible笔记(12):变量(一)
1.定义变量规范 先说说怎样定义变量,变量名应该由字母.数字.下划线组成,变量名需要以字母开头,ansible内置的关键字不能作为变量名. 2.定义变量,使用关键字:vars 定义变量用vars,调用 ...
- ORA-00917: missing comma
问题描述 ORA-00917: missing comma 问题原因 逗号,引号什么的多了或者少了,或者换行引起的
- XMLHttpRequest: 网络错误 0x2ee4, 由于出现错误 00002ee4 而导致此项操作无法完成
原因: IE11有默认设置安全策略,如果url需要证书,一发送请求IE11就会拒绝,因为ssl certificate(SSL证书) 是非法的 解决方案: (1)修改IE浏览器配置 - 用户使用的电脑 ...
- 当要打开PDB时为何会有Warning: PDB altered with errors.
对PDB执行 alter pluggable database pdbprod2 open; 操作后提示:Warning: PDB altered with errors. 来自AskScuti博客园 ...
- 剑指offer(leetcode 10.) 正则表达式匹配
这题一年前就做过,当时刚开始刷leetcode,提交了几十次过不去,就放那没管了.今天剑指offer又遇到这题,终于做出来了,用的dp. class Solution { public: bool i ...