PAT 甲级 1010 Radix
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536
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 <bits/stdc++.h>
using namespace std; string N1, N2;
int radix, tag;
long long sum = 0; long long Pow(long long a, long long b) {
long long ans1 = 1; while(b) {
if(b % 2) {
ans1 = ans1 * a;
b --;
} else {
a = a * a;
b /= 2;
}
}
return ans1;
} long long num(string s, int system) {
int ls = s.length();
reverse(s.begin(), s.end());
long long ans = 0;
if(system <= 10) {
for(int i = 0; i < ls; i ++)
ans += (s[i] - '0') * Pow(system, i);
} else {
int temp;
for(int i = 0; i < ls; i ++) {
if(s[i] >= '0' && s[i] <= '9')
temp = s[i] - '0';
else temp = s[i] - 'a' + 10; ans += temp * Pow(system, i);
}
}
return ans;
} long long Find(string s, long long res) {
char it = *max_element(s.begin(), s.end());
long long l = (isdigit(it) ? it - '0': it - 'a' + 10) + 1;
long long r = max(res, l);
long long mid; while(l <= r) {
mid = (l + r) / 2;
long long rec = num(s, mid);
if(rec == res) return mid;
else if(rec > res || rec < 0) r = mid - 1;
else l = mid + 1;
}
return -1;
} int main() {
cin >> N1 >> N2 >> tag >> radix;
int l1 = N1.length(), l2 = N2.length();
long long out = 0;
if(tag == 1) {
sum = num(N1, radix);
out = Find(N2, sum);
} else {
sum = num(N2, radix);
out = Find(N1, sum);
} if(out == -1) printf("Impossible\n");
else printf("%lld\n", out);
return 0;
}
明天就过年啦 希望新年会很多不一样
FHFHFH
PAT 甲级 1010 Radix的更多相关文章
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- 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 (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- PAT甲组 1010 Radix (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
- PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新
题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...
- PAT甲级——A1010 Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 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(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
随机推荐
- 谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版
谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版 一.讲座正文: 大家好!我是贾扬清237,目前在Google Brain83,今天有幸受雷鸣师兄邀请来和大家聊聊Caffe60.没有太多准备, ...
- CF 1093 G. Multidimensional Queries
G. Multidimensional Queries 链接 分析: 考虑如何去掉绝对值符号. $\sum \limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|$,由于k ...
- Codeforces 912 E.Prime Gift (折半枚举、二分)
题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...
- 【RAC搭建报错】You need disks from at least two different failure groups, excluding quorum disks and quorum failure groups, to create a Disk Group with normal redundancy
报错: You need disks from at least two different failure groups, excluding quorum disks and quorum fai ...
- 交换机 路由器 OSI7层模型
第1章 网络基础 1.1 网络的出现 解决计算机通讯的需求 实现计算机信息可以传递 1.2 主机之间实现通讯基本要求(三要素) ①. 需要在两台主机之间建立物理连接,物理连接的方式有网线 光纤线 wi ...
- zabbix3.4 监控路由器报错No Such Instance currently exists at this OID
zabbix 3.4 监控报错No Such Instance currently exists at this OID 1.首先查看监控的路由器的监控项是否报警 监控主机报错出现这个 No Such ...
- Python科学计算库-Numpy
NumPy 是 Python 语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库,也是学习 python 必学的一个库. 1. 读取文件 numpy.gen ...
- 41F继电器座的解剖与妙用
摘要:如果继电器不是焊在电路板上使用,就需要有个插座,这样方便接线,否则继电器的管脚是没法固定导线的.实际项目中使用了HF41F的继电器(宏发),在选择继电器座的时候,有一点感想,分享给大家.继电器是 ...
- QTP日常积累
1.init同步测试对象 同步测试对象: CODE: Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit(" ...
- java并发之线程池的使用
背景 当系统并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要消耗大量的系统资源. 所以需要一个办法使得线程可以 ...