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
注意点:1.left<=right;
2.数据范围用long long
#include<bits/stdc++.h>
using namespace std; long long digit[]; const long long inf = (1LL<<)-; void init(){ for(char c='';c<='';c++)
digit[c] = c-''; for(char c='a';c<='z';c++)
digit[c] = c-'a'+;
} long long convertNum10(string str1,long long radix,long long t){
long long ans=; long long len = str1.size(); for(long long i=;i<len;i++){
char c=str1[i];
ans=ans*radix+digit[c]; if(ans<||ans>t) return -;
} return ans; } long long cmp(string str2,long long radix,long long t){
long long n2=convertNum10(str2,radix,t);
if(n2<)return ;
else if(n2==t) return ;
else if(n2>t) return ;
else return -; } long long binary_search(string &str2,long long low,long long high,long long t){
long long left=low,right=high;
long long mid; while(left<=right){
mid=(left+right)/; long long flag=cmp(str2,mid,t); if(flag<)left =mid+;
else if(flag>)right = mid-;
else return mid;
} return -; } long long findLargest(string &str){
long long ans=-;
long long len=str.size(); for(long long i=;i<len;i++){
if(digit[str[i]]>ans)
ans=digit[str[i]];
} return ans+; } int main(){
string str1,str2;
long long tag,radix; cin>>str1>>str2>>tag>>radix; init(); if(tag==)
swap(str1,str2); long long t=convertNum10(str1,radix,inf); long long low = findLargest(str2); long long high=max(low,t)+; long long ans=binary_search(str2,low,high,t); if(ans==-)cout<<"Impossible\n";
else cout<<ans<<endl; }
1010 Radix (25 分)的更多相关文章
- 1010 Radix (25 分),PTA
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 1010 Radix (25分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- 1010 Radix (25分)
改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...
- 【PAT甲级】1010 Radix (25 分)(二分)
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
- 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 ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- 1010. Radix (25)(未完成)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
随机推荐
- jquery实现的ajax
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEnco ...
- kafka 简单安装以及java小demo
文章目录 第1步,下载解压 kafka: 第2步,运行 kafka: 第3步,创建topic 第4步,生产者发送消息 第5步,消费者接收消息 使用 java 客户端 kafka 0.8.0版本demo ...
- Java.io包
Java.io.BufferedInputStream 类添加功能到另一个输入流,缓冲输入以及支持mark和reset methods.Following是关于缓冲输入流的要点: 当创建缓冲输入,创建 ...
- Django 同步数据库的时候app中的models的表没有成功创建
出现 no changes detected python3 manage.py makemigrations --empty blog # blog就是你的app名字,此处要写成自己的app名字 ...
- kafka安装指导
本篇博客主要讲解在虚拟机安装之后,从如下五个方面进行讲解kafka的安装过程. (1)如何配置虚拟机网络 (2)如何安装JDK (3)如何安装zookeeper (4)如何安装kafka (5)kaf ...
- 牛客小白月赛18 G Forsaken的三维数点
思路: 这是一道树状数组和二分的题,用线段树空间直接爆,时间也会超 然后这道题我犯了一个很低级的错误,导致我wa了十发左右,一个int型变量用lld输入,然后他给的提示是运行错误,我哭了,我一直以为是 ...
- 好947 Mybatis 配置resultMap 带參数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
//TMD 写几个demo 还有大站採集 <a target=_blank href="http://hao947.com/" target="_blank&quo ...
- 2018-8-10-win10-uwp-ping
title author date CreateTime categories win10 uwp ping lindexi 2018-08-10 19:17:19 +0800 2018-2-13 1 ...
- tar - tar 档案文件管理程序的 GNU 版本。
总览 tar [ - ] A --catenate --concatenate | c --create | d --diff --compare | r --append | t --list | ...
- python wave 库 读取 BytesIO 对象的注意事项
程序中遇到需要使用临时文件时,常使用内存中的 io.BytesIO() 代替实体二进制文件,以避免磁盘IO,同时免去了考虑文件名的麻烦. file = io.BytesIO() file.write( ...