1010 Radix (25)(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 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

题意分析:

  给出两个正整数,给出其中一个正整数的基底,求另外一个正整数的基底,使得这两个正整数在各自的基底的十进制数相等

23分代码
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
int rag,radix;
char a[];
char b[];
int c[];
int l;
while(cin>>a>>b>>rag>>radix)
{
int da=,db;
if(rag==)
{
int la=strlen(a);
int k=;
for(int i=la-;i>=;i--)
{
if(a[i]>='a'&&a[i]<='z')
{
da+=(a[i]-'a'+)*k;
}
else
da+=(a[i]-'')*k;
k=k*radix;
}
l=strlen(b);
for(int i=l-;i>=;i--)
{
if(b[i]>='a'&&b[i]<='z')
{
c[i]=b[i]-'a'+;
}
else
c[i]=b[i]-'';
}
} if(rag==)
{
int lb=strlen(b);
int k=;
for(int i=lb-;i>=;i--)
{
if(b[i]>='a'&&b[i]<='z')
{
da+=(b[i]-'a'+)*k;
}
else
da+=(b[i]-'')*k;
k=k*radix;
}
l=strlen(a);
for(int i=l-;i>=;i--)
{
if(a[i]>='a'&&a[i]<='z')
{
c[i]=a[i]-'a'+;
}
else
c[i]=a[i]-'';
}
}
int ma=;
for(int i=;i<l;i++)
{
if(c[i]>ma)
ma=c[i];
}
bool f=;
for(int i=ma+;i<=;i++)
{
db=;
int k=;
for(int j=l-;j>=;j--)
{
db+=c[j]*k;
k=k*i;
}
if(db==da)
{
f=;
cout<<i<<endl;
break;
}
}
if(f==)
cout<<"Impossible"<<endl;
}
return ;
}

 AC代码:

要用long long,要用二分,否则有两个数据点会超时(第7和第18个数据点),二分时,left还好,right居然要这么大(否则第7个数据点卡死),而且,算出来的数<0也代表数太大,爆long long ,要ri=mid-1;而且,题目说有多个要挑出最小的那个。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int main()
{
ll rag,radix;
char a[];
char b[];
int c[];
ll l;
while(cin>>a>>b>>rag>>radix)
{
ll da=,db;
if(rag==)
{
ll la=strlen(a);
ll k=;
for(ll i=la-;i>=;i--)
{
if(a[i]>='a'&&a[i]<='z')
{
da+=(a[i]-'a'+)*k;
}
else
da+=(a[i]-'')*k;
k=k*radix;
}
l=strlen(b);
for(ll i=l-;i>=;i--)
{
if(b[i]>='a'&&b[i]<='z')
{
c[i]=b[i]-'a'+;
}
else
c[i]=b[i]-'';
}
} if(rag==)
{
ll lb=strlen(b);
ll k=;
for(ll i=lb-;i>=;i--)
{
if(b[i]>='a'&&b[i]<='z')
{
da+=(b[i]-'a'+)*k;
}
else
da+=(b[i]-'')*k;
k=k*radix;
}
l=strlen(a);
for(ll i=l-;i>=;i--)
{
if(a[i]>='a'&&a[i]<='z')
{
c[i]=a[i]-'a'+;
}
else
c[i]=a[i]-'';
}
}
ll ma=;
for(ll i=;i<l;i++)
{
if(c[i]>ma)
ma=c[i];
}
ll le=ma+,ri=;
bool f=;
ll mm=;
while(le<=ri)
{
ll mid=(ri+le)/;
db=;
ll k=;
for(ll j=l-;j>=;j--)
{
db+=c[j]*k;
k=mid*k;
}
if(db==da)
{
if(mid<mm)//题目有多个要挑出最小的那个
mm=mid;
ri=mid-;
f=;
}
else if(db>da||db<)//算出来的数<0也代表数太大,爆long long ,要ri=mid-1;
{
ri=mid-;
}
else if(db<da)
{
le=mid+;
} }
if(f==)
cout<<"Impossible"<<endl;
else
{
cout<<mm<<endl;
}
}
return ;
}

PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)的更多相关文章

  1. PAT甲级1010. Radix

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

  2. pat 甲级 1010. Radix (25)

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

  3. PAT 甲级 1010 Radix

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

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

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

  5. 1010 Radix (25 分)

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

  6. PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

    题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...

  7. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  8. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  9. PAT甲组 1010 Radix (二分)

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

随机推荐

  1. Java回顾之一些基础概念

    类的初始化顺序 在Java中,类里面可能包含:静态变量,静态初始化块,成员变量,初始化块,构造函数.在类之间可能存在着继承关系,那么当我们实例化一个对象时,上述各部分的加载顺序是怎样的? 首先来看代码 ...

  2. 关闭pm2

    先查找ID pm2 status 然后 pm2 stop id pm2 delete id

  3. 导出csv文件,导出axlsx文件。gem 'Axlsx-Rails' (470🌟);导入csv文件。

    汇出 CSV 档案 需求:后台可以汇出报名资料 有时候后台功能做再多,也不如 Microsoft Excel 或 Apple Numbers 试算表软件提供的分析功能,这时候如果有汇出功能,就可以很方 ...

  4. Confluence 6 创建一个用户宏

    如果你想创建自定义的宏的话,用户宏能够帮你完成这个任务.这个可以在你系统中应用特定的操作,比如说应用自定义格式等. 用户用是在 Confluence 创建和和管理的,你需要有一定的编码基础才可以. 你 ...

  5. 2-12-配置squid代理服务器加快网站访问速度

    本节所讲内容: squid服务器常见概念 squid服务器安装及相关配置文件 实战:配置squid正向代理服务器 实战:配置透明squid代理提升访问速度 实战:配置squid反向代理加速度内网web ...

  6. windows下的IO模型之完成端口

    本文整理于:http://blog.csdn.net/piggyxp/article/details/6922277 一. 完成端口的优点 完成端口会充分利用Windows内核来进行I/O的调度,是用 ...

  7. 个人知识管理系统Version1.0开发记录(07)

    模 块 复 用 原本还要测试一会的,突然出现一连串诡异的问题,比如,编译少加载个类啊,输入地址少个字母啊,改几行代码一改就是半小时啊.这是在提醒我们大脑疲倦了,所以果断小结,下次继续.这一次简单完成了 ...

  8. HashMap1.8源码分析(红黑树)

    转载:https://segmentfault.com/a/1190000012926722?utm_source=tag-newest https://blog.csdn.net/weixin_40 ...

  9. windows下mysql多实例安装

    在学习和开发过程中有时候会用到多个MySQL数据库,比如Master-Slave集群.分库分表,开发阶段在一台机器上安装多个MySQL实例就显得方便不少. 在 MySQL教程-基础篇-1.1-Wind ...

  10. bzoj1078

    题解: 一道思路题(话说在那个时候有多少人知道左偏树) 考虑最后一个加进来的点 必然满足 (1)从它到根一直是左链上去的 (2)没有左右子树 在这些点中寻找一个最浅的 然后删除 代码: #includ ...