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. Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...

  2. 原生DOM操作vs框架虚拟DOM比较

    1. 原生 DOM 操作 vs. 通过框架封装操作. 这是一个性能 vs. 可维护性的取舍.框架的意义在于为你掩盖底层的 DOM 操作,让你用更声明式的方式来描述你的目的,从而让你的代码更容易维护.没 ...

  3. http cookie的domain使用

    问题描述 最近遇到了一个因cookie domain设置不正确导致公司自研的分布式session组件无法生效的问题. 公司自研的这套分布式session组件依赖于设置在cookie中的sessionI ...

  4. 伪多项式时间 Pseudo-polynomial time

    2018-03-15 14:20:08 伪多项式时间:如果一个算法的传统时间复杂度是多项式时间的,而标准时间复杂度不是多项式时间的,则我们称这个算法是伪多项式时间的. 想要理解“伪多项式时间”,我们需 ...

  5. JAVA synchronized关键字锁机制(中)

    synchronized 锁机制简单的用法,高效的执行效率使成为解决线程安全的首选. 下面总结其特性以及使用技巧,加深对其理解. 特性: 1. Java语言的关键字,当它用来修饰一个方法或者一个代码块 ...

  6. 获取div的高度

    1.获取div的文档总高度(必须DOM操作): var scrollHeight=document.getElementById("inner").scrollHeight; // ...

  7. Java中如何读写cookie (二)

    Java中删除cookie Cookie[]   cookies=request.getCookies();       //cookies不为空,则清除       if(cookies!=null ...

  8. Eclipse CDT 配置C /C ++ 标准库 (UBUNTU 12 )

    http://blog.csdn.net/wudiwo/article/details/7682320

  9. 【MVC】MusicStore相关资料

    引言 当你对MVC的项目结构有一定了解时,那就可以开始学习一个世界级的MVC入门demo--MusicStore.学习的绝招就是把它抄一遍. 相关资料 MVC Music Store  Codeple ...

  10. JBOSS context root 项目名字默认不写

    进到 %JBOSS_HOME%/configuration/standalone.xml,修改下面节点 <virtual-server name="localhost" en ...