PAT A1010.Radix

链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536

算法笔记实战指南P167

题意:

给2个数:N和M,给出N的进制,求出M的进制为多少时,才能与N相同,如果不存在这样的进制,给出“Impossible”

N 和 M 都以字符串的形式给出,最多10个digits,从‘0’-‘9’,‘a’-‘z’,代表0-35

注意:

1. 虽然M每位最大是35,但是M的进制可能大于36。

2. M 的进制应该比它的 每一位都大,所以至少是它的所有数位上的最大的数+1,而不是直接以1位L,(这是题目要求)

3. M的进制最大值 = max(M的进制最小值,N的十进制)+1

4. M需要遍历的进制较多,需要二分,将复杂度降到O(log N) ,其中N = M的进制最大值 - M的进制最小值

5. 用上long long , 而且这样也会溢出,因此,转换为十进制后小于N,可能是进制太小,也可能是溢出造成小于0

代码如下:

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std; typedef long long LL; char n[],m[]; int charToInt(char c)
{
if(isdigit(c))
return c-'';
return c-'a'+;
} // 注意:ans 可能会超过LL的范围,溢出,此时返回值小于0
LL toDecimal(LL radix, char a[]){
LL ans = ;
for(int i = ;a[i];i++){
ans = ans * radix + charToInt(a[i]);
}
return ans;
} // 题目要求了进制必须大于每一位数字,这里查找最小的进制
int findMinRadix(char a[]){
int ans = -;
for(int i = ;a[i];i++){
ans = max(ans,charToInt(a[i]));
}
ans++;
return ans;
} LL solve(LL l, LL r, LL x){
LL preJudge = toDecimal(r,m);
// 注意这里也可以不预先判断,但是如果预先判断了,一定要注意小于x不代表真的小,还可能是溢出
if(preJudge>&&preJudge<x)
return -;
LL mid;
while(l<=r){
// 防止 l + r 溢出
mid = l + (r-l)/;
LL y = toDecimal(mid, m);
if(y==x)
return mid;
// 注意判断是否溢出,如果溢出,也代表选择的进制太大
if(y<||y>x)
r = mid-;
else
l = mid+;
}
return -;
} int main(){
int target, radix;
while(scanf("%s%s%d%d",n,m, &target, &radix)!=EOF){
char tmp[];
if(target==){
strcpy(tmp,n);
strcpy(n,m);
strcpy(m,tmp);
}
LL x = toDecimal(radix, n);
LL l = findMinRadix(m);
LL r = max(l, x)+;
LL ans = solve(l,r,x);
if(ans==-)
printf("Impossible\n");
else
printf("%lld\n",ans);
}
return ;
}

PAT A1010.Radix 二分法的更多相关文章

  1. PAT A1010 Radix (25 分)——进制转换,二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  2. PAT 1010 Radix 进制转换+二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  3. PAT甲级——A1010 Radix

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  4. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  5. A1010. Radix

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  6. PAT 1010 Radix (二分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  7. 已经菜到不行了 PAT 1010. Radix (25)

    https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...

  8. A1010 Radix (25 分)

    一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...

  9. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

随机推荐

  1. 在windows 10 上使用aspnet_regiis.exe -i 命令报 “此操作系统版本不支持此选项” 的解决办法

    用CMD窗口在C:\Windows\Microsoft.NET\Framework64\v4.0.30319下使用命令aspnet_regiis -i 报错: “此操作系统版本不支持此选项” .结果是 ...

  2. 树莓派3使用openSUSE Ports 42.3 驱动GPIO注意事项

    安装好opensuse 42.3以后,安装wiringPi库. 由于/proc/cpuinfo文件缺少“Hardware”信息,导致出现如下错误: Oops: Unable to determine ...

  3. python列表中的pop函数

    再python的列表中,有许多的内置方法,而在这里我主要向大家介绍一下pop函数. pop函数主要是用于删除列表中的数据.而其删除值时会返回删除的值.如果没有参数传入时, 则会默认认为删除列表的最后一 ...

  4. django后台admin csv 格式表格导出

    1.在app下新建一个.py文件  此例commen.py commen.py (内容)(具体怎么导出的也不知道这么写就对了) import csv from django.http import H ...

  5. word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法

    公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...

  6. Python re.findall函数不能匹配但是notepad++能匹配

    我使用同样的表达式匹配同样的网页源码,在notepad++里面不能直接使用,需要将内容都弄到同一行中. 但是我使用 requests.get(self.url).content.decode('UTF ...

  7. 基于Keepalived的MySQL高可用

    keepalived负责的是故障转移,至于故障转以后的节点之间数据的一致性问题依赖于具体的复制模式.不管是主从.一主多从还是双主.集群节点个数.主从具体的模式无关(常规复制,半同步复制,GTID复制, ...

  8. 转码器ffmpeg安装

    网络上很多帖子 但是基本上都是没有验证过复制粘贴的 以下是我自己装时流程和网络上的差不多但是中间不通的地方已经改正 centos7 1. 安装autoconf cd /App/srcwget http ...

  9. Error:java: Compilation failed: internal java compiler error 解决办法

    https://blog.csdn.net/jdjdndhj/article/details/70256989

  10. jQuery判断鼠标滚动方向

    var scrolltop = new Array(); var index = 0; scrolltop[0] = 0; $(document).scroll(function(){ index + ...