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 ...
随机推荐
- 87、使用TensorBoard进行可视化学习
1.还是以手写识别为类,至于为什么一直用手写识别这个例子,原因很简单,因为书上只给出了这个类子呀,哈哈哈,好神奇 下面是可视化学习的标准函数 ''' Created on 2017年5月23日 @au ...
- 如何在android studio中cordova的混合开发
基于Android Studio 中Cordova的开发 cordova简介 Cordova的前身是PhoneGap 官网: (http://cordova.io) Cordova应是运行在客户端本地 ...
- apiDoc部署搭建
apiDoc介绍: 目前,越来越多的项目进行前后端分离,那么就有需要比较规范的来管理API文档.而apiDoc这个API文档管理器,可以根据你项目代码的注释来进行自动生成API文档.同时支持C#.Ja ...
- 关于禁止微信端webapp内部下拉出现QQ浏览器的问题
<!DOCTYPE html> <html> <head> <title></title> </head> <style ...
- 写第一个 swift,iOS 8.0程序
class ViewController: UIViewController { @IBAction func btn_click(sender : AnyObject) { println(&quo ...
- Android开发:Handler的简单使用(一)
1.Handler是什么? 原文: A Handler allows you to send and process Message and Runnable objects associated w ...
- tee - 从标准输入写往文件和标准输出
总览 (SYNOPSIS) tee [OPTION]... [FILE]... 描述 (DESCRIPTION) 把 标准输入 的 数据 复制到 每一个 文件 FILE, 同时 送往 标准输出. -a ...
- JS对象 屏幕分辨率的高和宽 window.screen 对象包含有关用户屏幕的信息。 1. screen.height 返回屏幕分辨率的高 2. screen.width 返回屏幕分辨率的宽
屏幕分辨率的高和宽 window.screen 对象包含有关用户屏幕的信息. 1. screen.height 返回屏幕分辨率的高 2. screen.width 返回屏幕分辨率的宽 注意: 1.单位 ...
- robotframework+python3+selenium之web相关关键字---第二集
1.F5可查看所有关键字,如图: 2.浏览器相关关键字: 2.1 Open Browser https://www.baidu.com chrome # 打开浏览器,rf默认使用火狐浏 ...
- vue 同时使用过渡和动画
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...