首页 > 其他分享 >PAT_A 1010 Radix

PAT_A 1010 Radix

时间:2023-10-21 11:55:06浏览次数:35  
标签:radix PAT lo ll tag number Radix N2 1010

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

总结

二分 刚开始二分查找写错,导致只能得17分,修改后ac。

// 二分写错
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tag,radix;
string a,b;
ll decimalNum(string a, ll r){
	ll t = 0, e = 0, c = 0;
	for(ll i = 0; i < a.size() ; i++){
		if('0' <= a[i] && a[i] <= '9'){
			e = a[i] - '0';
		}else{
			e = a[i] - 'a' + 10;
		}
		t = t*r + e;
		if(t < 0) return -1;
	}
	return t;
}
ll radixMin(string a){
	ll m = 2, e = 0;
	for(ll i = 0; i < a.size() ; i++){
		if('0' <= a[i] && a[i] <= '9'){
			e = a[i] - '0';
		}else{
			e = a[i] - 'a' + 10;
		}
		m = max(m, e+1);
	}
	return m;
}
int main(){
	cin>>a>>b>>tag>>radix;
	if(a==b){
		cout<<radix;
		return 0;
	}
	if(tag == 2) swap(a,b);
	ll c = decimalNum(a, radix);
	ll lo = max(radixMin(b), 2LL);
	ll hi = max(lo, c)+1+1;
	ll t = 0, mi = 0;
	while(lo < hi){
		mi = (lo+hi)/2;
		t = decimalNum(b, mi);
		if(t > 0 && t < c){
			lo = mi + 1;
		}else{
			hi = mi;
		}
	}
	t == c ? cout<<mi : cout<<"Impossible";
	return 0;
}
// 二分修改
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tag,radix;
string a,b;
ll decimalNum(string a, ll r){
    ll t = 0, e = 0, c = 0;
    for(ll i = 0; i < a.size() ; i++){
        '0' <= a[i] && a[i] <= '9' ? e = a[i] - '0' : e = a[i] - 'a' + 10;
        t = t*r + e;
        if(t < 0) return -1;
    }
    return t;
}
ll radixMin(string a){
    ll m = 2, e = 0;
    for(ll i = 0; i < a.size() ; i++){
        '0' <= a[i] && a[i] <= '9' ? e = a[i] - '0' : e = a[i] - 'a' + 10;
        m = max(m, e+1);
    }
    return m;
}
int main(){
    cin>>a>>b>>tag>>radix;
    if(tag == 2) swap(a,b);
    ll c = decimalNum(a, radix);
    ll lo = max(radixMin(b), 2LL);
    ll hi = max(lo, c)+1;
    ll t = 0, mi = 0;
    while(lo <= hi){
        mi = (lo+hi)/2;
        t = decimalNum(b, mi);
        if (t == c) break;
        else if(t > 0 && t < c) lo = mi + 1;
        else hi = mi-1;
    }
    t == c ? cout<<mi : cout<<"Impossible";
    return 0;
}

标签:radix,PAT,lo,ll,tag,number,Radix,N2,1010
From: https://www.cnblogs.com/Afinis/p/17778727.html

相关文章

  • 【愚公系列】2023年10月 二十三种设计模式(十九)-观察者模式(Observer Pattern)
    ......
  • 利用 CSS 的 clip-path 属性快速画三角形、气泡框
    clip-path 结合polygon函数,可以快速切出一个三角形、气泡框。a.三角形有三个顶点,因此 polygon 需要传三个参数,每个参数是顶点的x和y轴位置百分比:#triangle-1{-webkit-clip-path:polygon(50%0,100%100%,0100%);clip-path:polygon(50%0,100%100%,......
  • xxl-job执行java任务报错: unable to find valid certification path to requested tar
    1、错误:xxl-job调用https接口显示证书验证失败[错误信息:sun.security.validator.ValidatorException:PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException:unabletofindvalidcertificationpathtorequestedtarget]2023-10-2015......
  • PAT_A 1085 Perfect Sequence
    Givenasequenceofpositiveintegersandanotherpositiveinteger p.Thesequenceissaidtobea perfectsequence if M≤m×p where M and m arethemaximumandminimumnumbersinthesequence,respectively.Nowgivenasequenceandaparameter p,y......
  • python sys.path介绍
    pythonsys.path介绍介绍当我们导入模块时,python解释器会通过sys.path中的环境变量搜索。sys.path是一个列表,里面包含已添加到环境变量中的路径。使用sys.path.append({路径})可以往里面添加自定义的环境变量。使用当我们想要导入某个文件中的文件失败时,可以将其文件夹路......
  • Java资源文件获取方法详解:从 Classpath 到 Web 应用程序
    在Java开发中,访问和读取资源文件是一个常见的需求。这些资源可以是配置文件、图像、音频、视频、文本文件等。在Java中,获取资源文件有多种方式,包括直接通过类路径(Classpath)访问,或者通过Web应用程序的上下文路径(ContextPath)访问。以下我们将详细探讨这些方法。通过类路径(Classpath)......
  • 华为路由器配置NAT,PAT
    NAT概述NAT(NetworkAddressTranslation)又称为网络地址转换,用于实现私有网络和公有网络之间的互访。私有网络地址和公有网络地址私有网络地址(以下简称私网地址)是指内部网络或主机的IP地址,公有网络地址(以下简称公网地址)是指在互联网上全球唯一的IP地址。IANA(InternetAssigne......
  • Git-error: invalid path
    gitcheckoutisp原因是Win和Linux文件格式不一致,要么切换到Linux环境下操作,要么gitconfigcore.protectNTFSfalse查了下官方手册,官方原话:Ifsettotrue,donotallowcheckoutofpathsthatwouldcauseproblemswiththeNTFSfilesystem大概意思是说NTFS有个路径保......
  • PAT_A 1038 Recover the Smallest Number
    Givenacollectionofnumbersegments,youaresupposedtorecoverthesmallestnumberfromthem.Forexample,given{32,321,3214,0229,87},wecanrecovermanynumberssuchlike32-321-3214-0229-87or0229-32-87-321-3214withrespecttodifferentor......
  • [ABC207F] Tree Patrolling 题解
    [ABC207F]TreePatrolling弱智DP题,设\(f(i,j,0/1/2)\)表示在点\(i\),子树中有\(j\)个点被覆盖,且\(i\)点自身状态是未被覆盖/被自身覆盖/被某个儿子覆盖,然后树上背包更新就行了。代码:#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintmo......