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 (二分)的更多相关文章

  1. PAT甲级1010. Radix

    PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...

  2. 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 ...

  3. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  4. PAT 1010 Radix (二分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  5. PAT Advanced 1010 Radix(25) [⼆分法]

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  6. PAT 甲级 1010 Radix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...

  7. PAT 解题报告 1010. Radix (25)

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  8. PAT Radix[二分][进制转换][难]

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  9. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

随机推荐

  1. Flink 实践教程-进阶(2):复杂格式数据抽取

    作者:腾讯云流计算 Oceanus 团队 流计算 Oceanus 简介 流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的具备一站开发.无缝连接.亚 ...

  2. 1005.K次取反后最大化的数组和

    1005.K次取反后最大化的数组和 目录 1005.K次取反后最大化的数组和 题目 题解 排序+维护最小值min 题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 ...

  3. IDEA中对代码进行测试

    一. 建立对应得目录 二.导入junit依赖 <dependency> <groupId>junit</groupId> <artifactId>jun ...

  4. 商业爬虫学习笔记day2

    1. get传参 (1)url中包含中文报错解决方法 urllib.request.quote("包含中文的url", safe = "string.printtable ...

  5. 复制virtualbox虚拟硬盘

    D:\VirtualBox\VBoxManage.exe clonevdi F:\virtualbox\rac1\rac1.vdi F:\virtualbox\rac2\rac2.vdi 虚拟机软件安 ...

  6. Linux学习 - 修改、查询文件内容

    一.显示文件内容 cat  [-n]  [文件名] 正向显示 -n 显示行号 tac  [文件名] 反向显示 more  [文件名] 可实现分页显示 (空格)或(f) 翻页 (Enter) 换行 (q ...

  7. Python的安装和卸载

    一.Python的安装,去Python官网https://www.python.org/downloads/下载对应安装包 进入官网后鼠标移动到downloads处然后点击Windows 二.安装包下 ...

  8. 2020ACTF pwn writeup

    为了打2021的ACTF,想着把2020年的pwn题做一做吧,发现2020年的pwn题质量还挺高的.反倒是2021年的题目质量不太高,好像是没有专门的pwn师傅出题,可以理解,毕竟办校赛,说白了就是用 ...

  9. 解决电脑连接 iPhone 热点没有 IPv6地址的问题

    问题描述: 初入 iPhone ,电脑使用 ios 共享的热点无法连接 IPv6 地址.但是,直接在 iPhone 上面打开 https://www.test-ipv6.com/ 完美支持 IPv6 ...

  10. android jni-dlerror报undefined symbol: JNI_OnLoad

    以下是很简单的一个官方的jni方法,在MainActivity的onCreate中调用 extern "C" JNIEXPORT jstring JNICALL Java_com_ ...