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. 【bzoj4972】小Q的方格纸 前缀和

    题目让O(1)预处理出来 类三角形边界及内部的和 根据这个图 就是一个大矩形-左边的绿色的矩形 - 蓝色的大三角形 + 右上角突出的蓝色的小三角形 #include<bits/stdc++.h& ...

  2. ES7学习笔记——Array.prototype.includes和求幂运算符**

    一直以来,在前端开发时使用的基本都是ES5,以及少量的ES6.3月份换工作面试时,发现一些比较大的公司,对ES6比较重视,阿里的面试官直接问ES7和ES8,对于从未接触过人来说,完全是灾难.由此也显现 ...

  3. HDU 3605 Escape(状态压缩+最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意: 有n个人和m个星球,每个人可以去某些星球和不可以去某些星球,并且每个星球有最大居住人数,判断是否所 ...

  4. mis权限系统

    在mis中开发,主要目的是有一个统一的权限管理(即r360.right表),以及一个统一的系统和界面供后台配置管理 1.数据库准备工作: mis后台涉及表: right表是权限操作表,role_rig ...

  5. 调用libpci库出现的问题和解决方法

    调用libpci库出现的问题和解决方法   本方案以pciutils-3.5.1为例.   1. 从以下地址下载pciutils-3.5.1.tar.xz https://www.kernel.org ...

  6. hdu 4747 mex 线段树+思维

    http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...

  7. 线程池ThreadPoolExecutor里面4种拒绝策略

    ThreadPoolExecutor类实现了ExecutorService接口和Executor接口,可以设置线程池corePoolSize,最大线程池大小,AliveTime,拒绝策略等.常用构造方 ...

  8. Java网络编程学习A轮_08_NIO的Reactor模型

    参考资料: 了解 Java NIO 的 Reactor 模型,大神 Doug Lea 的 PPT Scalable IO in Java 必看:http://gee.cs.oswego.edu/dl/ ...

  9. 人工神经网络 Artificial Neural Network

    2017-12-18 23:42:33 一.什么是深度学习 深度学习(deep neural network)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高 ...

  10. Credentials(Rails5.2新) 很基础的知识点,具体还要实操。

    Credentials(Rails5.2新) 增加config/credentials.yml.enc 憎加config/master.key 移除了5.1使用的config/secrets.yml, ...