PAT甲组 1010 Radix (二分)
1010 Radix (25分)
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 \(N_1\) and \(N_2\), 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
Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible
题意
给出两个正整数和其中一个数的基底,找到另一个数的基底,使两个数相等
思路
因为每个数不超过\(10\)位,每位最大都可能到z,所以待求的基底可能是一个很大的数,用二分法求另一个数的基底。
将一直基底的数求出来之后,二分另一个数的基底。如果当前基底下的结果小于\(0\)或大于已知数,那么这个基底就是偏大的,一直二分直至相等,否则返回\(-1\)。
二分的左区间为待求基底的每位数字上最大值加一,右区间为max(左区间,已知数)
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
ll get_num(string n,ll radix)
{
ll ans=0;
int l=n.length();
for(int i=l-1;i>=0;i--)
{
int res;
if(n[i]>='0'&&n[i]<='9')
res=n[i]-'0';
else
res=n[i]-'a'+10;
ans+=res*pow(radix,l-i-1LL);
}
return ans;
}
bool check(ll num,string n,ll radix)
{
ll res=get_num(n,radix);
if(num==res)
return true;
return false;
}
ll solve(ll num,string n)
{
ll l,r;
int low=0;
for(auto i:n)
{
if(i>='0'&&i<='9')
low=max(low,i-'0');
else
low=max(low,i-'a'+10);
}
l=low+1LL,r=max(1LL*l,num);
while(l<=r)
{
ll mid=(l+r)/2;
ll res=get_num(n,mid);
if(res<0||res>num)
r=mid-1;
else if(res<num)
l=mid+1;
else if(res==num)
return mid;
}
return -1;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
string s1,s2;
int tag;
ll radix;
cin>>s1>>s2>>tag>>radix;
ll num1,num2;
if(tag==1)
{
num1=get_num(s1,radix);
if(solve(num1,s2)==-1)
cout<<"Impossible";
else
cout<<solve(num1,s2);
cout<<endl;
}
else
{
num2=get_num(s2,radix);
if(solve(num2,s1)==-1)
cout<<"Impossible";
else
cout<<solve(num2,s1);
cout<<endl;
}
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.\n";
#endif
return 0;
}
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 (二分)
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
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- PAT Radix[二分][进制转换][难]
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
随机推荐
- 学习java的第二十一天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- linux shell中的条件判断语句
http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...
- Linux基础命令---nslookup查询域名工具
nslookup nslookup是一个查询DNS域名的工具,它有交互和非交互两种工作模式. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- InnoDB的行锁模式及加锁方法
MYSQL:InnoDB的行锁模式及加锁方法 共享锁:允许一个事务度一行,阻止其他事务获取相同数据集的排他锁. SELECT * FROM table_name WHERE ... LOCK IN S ...
- 基于Web的质量和测试度量指标
直观了解软件质量和测试的完整性 VectorCAST/Analytics可提供便于用户理解的web仪表盘视图来显示软件代码质量和测试完整性指标,让用户能够掌握单个代码库的趋势,或对比多个代码库的度量指 ...
- 熔断和降级的初步详解实现(NET Core控制台输出讲解Polly)
概述 很多朋友包括我,对于"八股文"可以说是比较熟练的,每次面试前都会专研不少东西,各种固定答案.专业术语都是张口就来,一个字,稳. 八股文:程序员八股文是指程序员在面试过程中经常 ...
- bjdctf_2020_babystack2
此题考整型的有符号无符号的东西... 下载文件还是,先检查一下保护. 64位程序,只开启了堆栈不可执行,看一下ida的伪代码. 大概流程就是先让你输入一个数,这个数就是后面read的可以输入的长度,要 ...
- 对Spring IOC容器的思考
最近在看Spring5的视频教学,学到了IOC容器这块,对IOC有些浅薄的理解,分享一二:有错误之处,还请大佬指出 IOC(Inversion of Control 控制反转),是面向对象编程中的一种 ...
- 通过idea创建Maven项目整合Spring+spring mvc+mybatis
创建项目 File→new→project 然后就不断next直到项目面板出来 设置文件夹 注意:这里我个人习惯,在java下还建了ssm文件夹,然后再cont ...
- 3、回溯算法解题套路框架——Go语言版
前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...