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. Maven编译的时候加载本地路径jar

    大家都知道Maven的依赖是通过pom文件管理的,只要配置了<dependency>,Maven就会从本地仓库 -> 远程仓库 -> 中央仓库获取依赖的jar. 但是如果仓库中 ...

  2. BZOJ 2806 【CTSC2012】 Cheat

    题目链接:Cheat 话说这道题很久以前某人就给我们考过,直到现在,我终于把这个坑填上了…… 这道题要我们把一个串\(S\)划分成若干块,每块长度不小于\(L_0\),使得能够在文章库中完全匹配的块的 ...

  3. angular的 表单

    一般来讲表单可能遇到的问题:1.如何数据绑定.2.验证表单.3.显示出错信息.4.整个form的验证.5.避免提交没有验证通过的表单.6.防止多系提交. input属性:nameng-modelng- ...

  4. poj2411 轮廓线dp裸题

    题意:用12的骨牌覆盖nm的矩阵的方案数 题解:dp[i][j]表示枚举到了第i行,j状态的方案数,三种转移,向上的,要求不是第一行而且上面的没有覆盖过,向下的,要求不是第一列而且左边没有覆盖过,不放 ...

  5. poj-2096-期望/dp

    http://poj.org/problem?id=2096 有n种病毒,s个服务器,每天等概率的在某个服务器上发现某一种病毒,问发现所有种类病毒且覆盖所有的服务器的期望天数. 利用全期望公式可以将期 ...

  6. java实现的18位身份证格式验证算法

    公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码.1.地址码表示编码对象常住户口所在县(市. ...

  7. Jenkins用户权限管理

    一.插件安装 插件:Role-based Authorization Strategy版本:2.3.2 二.全局安全配置 进入Jenkins后点击系统管理进入全局安全配置 当插件安装好的时候,授权策略 ...

  8. Alpha阶段第1周Scrum立会报告+燃尽图 01

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...

  9. 通过java解析域名获得IP地址

    IP地址是Internet主机的作为路由寻址用的数字型标识,人不容易记忆.因而产生了域名(domain name)这一种字符型标识. DNS即为域名解析服务.在这里我们如果想通过java程序来解析域名 ...

  10. CUDA Samples: matrix multiplication(C = A * B)

    以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...