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 = 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)的更多相关文章
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- PAT 甲级 1010 Radix
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- 1010 Radix (25 分)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 1 ...
- PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新
题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT甲组 1010 Radix (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
随机推荐
- iOS动画进阶 - 手摸手教你写ShineButton动画
移动端访问不佳,请访问我的个人博客 前段时间在github上看见一个非常nice的动画效果,可惜是安卓的,想着用swift写一个iOS版的,下下来源代码研究了一下,下面是我写代码的心路历程 先上图和d ...
- C#中的静态构造函数
https://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.140).aspx A static constructor is used to ini ...
- [JS] - level8 kata
https://www.codewars.com/kata/57e3f79c9cb119374600046b function hello(name) { if(name == "" ...
- 解决Github Desktop Repo publish 失败问题
参考: src refspec master does not match any when pushing commits in git Github上传项目步骤和常见问题 Trying to gi ...
- Python学习札记(二十九) 模块2
参考:使用模块 NOTE 1.内建sys模块: #!/usr/bin/env python3 import sys 'a test module' __author__ = 'wasdns' def ...
- sudo环境变量问题;程序库函数寻找
1. sudo 和 root不完全等效,继承的环境变量不一样,最主要的区别还是输入的密码不同. 2. 使用sudo去执行一个程序时,出于安全的考虑,这个程序将在一个新的.最小化的环境中执行,也就是说, ...
- DWZ 框架详解
这是一个智障的框架,能别用就别用.
- Django配置让其他电脑访问网站(包括:修改IP和端口)
http://blog.sina.com.cn/s/blog_9c5364110101fyk7.html
- python爬虫脚本下载YouTube视频
python爬虫脚本下载YouTube视频 爬虫 python YouTube视频 工作环境: python 2.7.13 pip lxml, 安装 pip install lxml,主要用xpath ...
- UVALive-2966 King's Quest(强连通+二分图匹配)
题目大意:有n个男孩和和n个女孩,已只每个男孩喜欢的女孩.一个男孩只能娶一个女孩.一个女孩只能嫁一个男孩并且男孩只娶自己喜欢的女孩,现在已知一种他们的结婚方案,现在要求找出每个男孩可以娶的女孩(娶完之 ...