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 = ...
随机推荐
- 20145209刘一阳《JAVA程序设计》第八周课堂测试
第八周课堂测试 1.下面代码中共有(C)个线程? public class ThreadTest { public static void main(String args[]){ MyThread ...
- 洛谷 P1198 [JSOI2008]最大数
洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...
- log4j配置单独日志文件输出
log4j.logger.batteryHistory=ERROR,BD log4j.appender.BD=org.apache.log4j.FileAppender log4j.appender. ...
- CF 258 D. Little Elephant and Broken Sorting
D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...
- Developing modules for the Apache HTTP Server 2.4
Developing modules for the Apache HTTP Server 2.4 Available Languages: en This document explains how ...
- 一个导致MGR数据混乱Bug的分析和修复
1.背景 MGR是个好东西,因为他从本质上解决了数据不一致的问题.不光是解决了问题,而且出自名门正派(Oracle的MySQL团队),对品质和后续的维护,我们是可以期待的. 但是在调研的过程中,发现有 ...
- msil 笔记
public class TestImpl : ITest { // Properties public string Address { get { return "abc"; ...
- MySQL与DevC++的连接问题
0.引言 MySQL作为当前的一个主流的开源的关系型数据库,受到大家的广泛关注.DevC++也作为一个开源的简单的C++编辑器,有着比VS更简便更快捷的编译.但是目前网上,却很少有对 MySQL连接D ...
- Loadrunner 性能指标
https://wenku.baidu.com/view/bf395a1db7360b4c2e3f64ca.html 希望能记住最好记住吧,这个很重要的. qq,979506750多交流
- java中文显示乱码的解决方式
myeclipse 10 import 源文件后java文件中文乱码问题,*.java文件中的中文不能显示,都是乱码 解决方法(网上找的,已经过验证): 一.将整个project设置编码UTF-8(U ...