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 = ...
随机推荐
- 如何搭建openvpn
一.什么是openvpn Openvpn是一款基于openssl的开源vpn软件,它可以很好的运行在linux及windows各发行版本中,它的核心技术是虚拟网卡,其实它更像是一个底层的网卡驱动软件, ...
- jQuery学习- 获取与设置属性的函数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- getopt例子
(本例基于win7 + python3.4) import getopt, sys ''' getopt 模块专门用来处理命令行参数 函数 getopt(args, shortopts, longop ...
- 【HAOI2010】软件安装
题面 题解 缩点之后一个裸的树型背包 代码 #include<cstdio> #include<cstring> #include<algorithm> #defi ...
- #6472. 「ICPC World Finals 2017」难以置信的任务 Mission Improbable
可以简化一下问题,假设Patrick把箱子都拿走但是原来有箱子的位置留下一个,现在要放箱子使得每行每列最大值都满足,最少放多少个. 设第\(i\)行的最大值是\(H(i)\),第\(i\)列的是\(W ...
- Proe/Creo 零件库mnu文件制作批处理
proe零件库自定义时需要菜单文件mnu,百度了下网上还没有人制作,偶然间Google时在PTC论坛上看到一德国人分享了自己制作的bat文件用于对文件夹(及子文件夹)产生mnu文件,我在将他的文件翻译 ...
- Unity优化方向——优化Unity游戏中的垃圾回收(译)
介绍 当我们的游戏运行时,它使用内存来存储数据.当不再需要该数据时,存储该数据的内存将被释放,以便可以重用.垃圾是用来存储数据但不再使用的内存的术语.垃圾回收是该内存再次可用以进行重用的进程的名称. ...
- Http protocal
https://tools.ietf.org/html/rfc2616 1. 状态码:status code 1xxx:信息--请求被接收,继续下一步处理 2xxx:成功--请求行为被 ...
- swoole中退出、异常与错误的处理笔记
关于PHP这方面的知识 可以看 https://www.cnblogs.com/zyf-zhaoyafei/p/6928149.html 进行补课 然后下面记录一下使用swoole的时候需要注意的地方 ...
- Win7-64位PowerDesigner下MySQLODBC驱动问题
操作系统:win7-64位,PowerDesigner15.1(以下简称PD), MYSQL-ODBC-64驱动.安装完MYSQL-ODBC-64却找不到相关驱动,用PD反导数据库,却找不到Mysql ...