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 ...
随机推荐
- zoj 3747 递推dp
Attack on Titans Time Limit: 2 Seconds Memory Limit: 65536 KB Over centuries ago, mankind faced ...
- python 操作Excel表格,解压zip包,压缩zip包,目录遍历
import zipfile import os,shutil import openpyxl file_list_pos="" fileName="" zip ...
- 上海仪电Azure Stack技术深入浅出系列1:谈Azure Stack在私有云/混合云生态中的定位
2.2 Azure Stack Azure Stack到2017年7月才提供GA版本,但目前还是可以通过技术预览版了解该技术.Azure Stack本质上是核心Azure服务的一个私有实例. Micr ...
- tenserflow models包的安装
1.下载 models包 https://github.com/tensorflow/models 2.将models包拷贝到本机Python包的安装地址即可,本机Python包的安装地址的查看方式可 ...
- Android------视频播放器(包含全屏播放,快退,快进,腾讯新闻的列表播放等)
前段时间做了一个新闻APP,涉及到了列表视频播放,和腾讯新闻APP差不多,总结了一下代码,写了一个Demo来分享给大家. 用了 TabLayout+RecylerView+自定义视频控件 完成的 ...
- 10个有趣的Javascript和CSS库
Tailwind CSS Tailwind是用于构建自定义用户界面的实用CSS框架. 每个Tailwind小应用都有多种尺寸,这使得创建响应式界面变得非常简单. 您可以自定义颜色,边框尺寸,字体,阴影 ...
- HTML5里的placeholder属性
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 联想A390T刷机ROOT教程
一.联想A390T手动进入Recovery的方法: [步骤一]首先,将你的A390T手机关机,关机状态下,先按住电源键2秒,不要松开,再同时按下音量加.音量减两个键,此时,3个键一直按住不要放开,几秒 ...
- 各种liunx发行版本包管理器对比
关于Ubuntu安装软件问题:apt-get和dpkg区别? 两者的区别是dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的软件包用apt可以再安装一遍,系统不知道之前安装过了, ...
- 201621123010《Java程序设计》第3周学习总结
1.本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你认 ...