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 = ...
随机推荐
- Qt5.4 All Modules
Qt5.4 All Modules Qt Essentials Qt essentials define the foundation of Qt on all platforms. They are ...
- 洛谷 P4593 [TJOI2018]教科书般的亵渎
洛谷 P4593 [TJOI2018]教科书般的亵渎 神仙伯努利数...网上一堆关于伯努利数的东西但是没有证明,所以只好记结论了? 题目本质要求\(\sum_{i=1}^{n}i^k\) 伯努利数,\ ...
- jQuery js 格式化数字
写程序与的时候,有些地方需要js或者jQuery取值,然后将50000000.00格式化成50,000,000.00这种形式: 首先创建formatCurrency.js,代码如下: function ...
- python 另一种打开文章的方式——codecs
通常我们使用python打开文件都是 open(‘beijing.txt’)或者是 with open(‘beijing.txt’)as f 那么今天来给你带来一个新的文档打开方式 python的co ...
- Kali Linux菜单中各工具功能大全
各工具kali官方简介(竖排):https://tools.kali.org/tools-listing 名称 类型 使用模式 功能 功能评价 dmitry 信息收集 whois查询/子域名收集/ ...
- 大神教你零基础学PS,30堂课从入门到精通
ps视频教程,ps自学视频教程.ps免费视频教程下载,大神教你零基础学PS教程视频内容较大,分为俩部分: 大神教你零基础学PS--30堂课从入门到精通第一部分:百度网盘,https://pan.bai ...
- javaweb学习6——自定义标签
声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...
- Vuex 单状态库 与 多模块状态库
之前对 Vuex 进行了简单的了解.近期在做 Vue 项目的同时重新学习了 Vuex .本篇博文主要总结一下 Vuex 单状态库和多模块 modules 的两类使用场景. 本篇所有代码是基于 Vue- ...
- Netty源码分析第5章(ByteBuf)---->第4节: PooledByteBufAllocator简述
Netty源码分析第五章: ByteBuf 第四节: PooledByteBufAllocator简述 上一小节简单介绍了ByteBufAllocator以及其子类UnPooledByteBufAll ...
- python—索引与切片总结
python中索引与切片的熟练掌握对于字符串的操作很有帮助,梳理如下: (1)索引 S = 'hello world' 1)正向索引 正向索引从0开始,向右依次递增. 2)反向索引 反向索引从-1开始 ...