1010 Radix
1010 Radix (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
题意:给出两个数,求在已知其中一个数的进制下,另外一个数最小进制下两个数相等。
解析:误导信息:the set { 0-9, a
-z
} where 0-9 represent the decimal numbers 0-9, and a
-z
represent the decimal numbers 10-35. 以为进制最大也就36 其实是可以无穷大。但是也不是,在测试数据中,超过long long就是溢出,则Impossible。可知一个个遍历过去肯定不理想,超时。可以想到二分查找。之前一直在第七个测试点卡着 只有24分,现在终于明白,原来若存在进制,这最大也就是da,因为超过da,不可能da==db。
#include<bits/stdc++.h>
using namespace std;#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}int main()
{char a[15],b[15];int c[15];ll tag,radix,l,la;ll da=0,db=0;cin>>a>>b>>tag>>radix;if(tag==1){ll k=1;la=strlen(a);for(ll i=la-1; i>=0; i--){if(a[i]>='a'&&a[i]<='z'){da+=(a[i]-'a'+10)*k;}else da+=(a[i]-'0')*k;k*=radix;}l=strlen(b);for(ll i=l-1; i>=0; i--){if(b[i]>='a'&&b[i]<='z'){c[i]=b[i]-'a'+10;}else c[i]=b[i]-'0';}}if(tag==2){ll k=1;la=strlen(b);for(ll i=la-1; i>=0; i--){if(b[i]>='a'&&b[i]<='z'){da+=(b[i]-'a'+10)*k;}else da+=(b[i]-'0')*k;k*=radix;}l=strlen(a);for(int i=l-1; i>=0; i--){if(a[i]>='a'&&a[i]<='z'){c[i]=a[i]-'a'+10;}else c[i]=a[i]-'0';}}ll maxx=0;for(ll i=0; i<l; i++){if(c[i]>maxx)maxx=c[i];}ll left=maxx+1,right=da+1;//最大的进制ll ans;int f=0;while(left<=right){ll mid=(left+right)/2;db=0;ll k=1;for(ll i=l-1; i>=0; i--){db+=c[i]*k;k*=mid;}if(da==db){ans=mid;f=1;right=mid-1;}else if(db<0||db>da){right=mid-1;}else if(db<da){left=mid+1;}}if(!f)cout<<"Impossible"<<endl;elsecout<<left<<endl;return 0;
}
1010 Radix相关推荐
- PAT甲级1010 Radix :[C++题解]进制位、秦九韶算法、二分(PAT通过率最低的一道题0.11)
文章目录 题目分析 题目链接 题目分析 分析: 本题思路分两步. 第一步:先把给出数值和进制的数,暂定为N1,转换成10进制,即为target. 第二步: 判断一下N2在多少进制下是等于target的 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- [Java] 1010. Radix (25)-PAT甲级
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- 1010 Radix
目录 总结 解题过程 总结 1. 短小精悍的一道二分算法题,总体思路是,将字符串1(如果tag不是1将两个字符串调换一下即可)转化为10进制,再用二分法看能否找到另一个进制使得两个字符串的10进制数相 ...
- PAT (Advanced Level) 1010 Radix(二分+模拟)
题目链接:点击查看 题目大意:给出两个数n1和n2,再给出其中一个数的进制,问另一个数能否选择一个进制,使得两个数的值相等 题目分析:首先这个题目一开始会错意了,因为题中的表示只给出了0~9以及a~z ...
- 【测试点分析】1010 Radix (25 分)_37行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...
- PAT 1010 Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 1010 Radix (25 分)
一.问题描述 输入两个数N1.N2,和一个标记tag.一个基数radix,其意义是:tag为1时,radix是N1的基数:tag为2时,radix是N2的基数.方便起见,以下假定tag为1,则目标就是 ...
- PAT Advanced—1010 Radix (25分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
最新文章
- 探测服务器操作系统工具,探测服务器操作系统
- 吐血整理:手拿几个大厂offer的秘密武器!
- IOS开发笔记13-Object-C中的NSString
- MySQL-分库分表初探
- Ajax+asp.net无刷新验证用户名
- Python - 虚拟环境
- Electron——常用的工具列表
- 从绝望中寻找希望,人生必将辉煌
- Protel 99se汉化步骤
- 正交试验设计例题及答案_正交试验设计中的方差分析-例题分析
- 读书笔记-《程序员成长课》
- ERP开源框架 + 二次开发平台 介绍
- 计算机策略删除必须要一个密码,取消电脑开机密码
- 在阿里 AI Lab 做 NLP 高级算法专家是一种什么样的体验?
- 运用Python轻松爬取网易云的音乐,小白都能懂的爬虫教程
- SQL 拼接多个字段的值一个字段多条记录的拼接
- 小白的柳州麻将黑科技.Part1 = 你不知道的门清
- 看门狗2服务器位置,看门狗2怎么爬进服务器 | 手游网游页游攻略大全
- ZIF-8包裹溶菌酶作配体的金纳米颗粒(Lys-AuNPs)|磁性g-C3N4/Fe3O4@ZIF-8纳米复合材料|ZIF-8@银基SERS基底|齐岳试剂
- [活动召集]福建PHP社区聚会