PAT A1010.Radix 二分法
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 二分法的更多相关文章
- PAT A1010 Radix (25 分)——进制转换,二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT 1010 Radix 进制转换+二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT甲级——A1010 Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT 1010 Radix (25分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- A1010. Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT 1010 Radix (二分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- A1010 Radix (25 分)
一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
随机推荐
- Springboot+ mybatis+ mysql配置@Slf4j
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver # 驱动 name: testDB # 配置名,可以随便写 userna ...
- The type initializer for System.Data.SqlClient.SqlConnection threw an exception
The type initializer for System.Data.SqlClient.SqlConnection threw an exception net framwork啥原因 xp电脑
- 1024程序员节宅男节日快乐 -- JAVA快速开发平台,JEECG 3.8宅男优化版本发布
JEECG 3.8 版本发布,系统全面升级,重构上传组件.优化代码生成器机制! 导读 ⊙平台性能优化,系统更稳定,速度闪电般提升 ⊙系统上传组件全面重构,使用plupload组件,解决flash的 ...
- 使用TensorFlow训练模型的基本流程【转】
原文地址(https://github.com/wmpscc/TensorflowBaseDemo ) 本篇文章将介绍使用tensorflow的训练模型的基本流程,包括制作读取TFRecord,训练和 ...
- VS2013 切换用户
控制面板---------------用户账户---------------凭据管理器----------------Windows凭据,可以修改和删除登录用户:根据TFS服务器的IP地址或网址或服务 ...
- APK签名说明
在 Android 系统下, 一些公司会将自己做的APK进行管控,授权签名后方可使用. APK所属的软件公司会提供签名包,例如: 第一步:是要检查所操作的 PC 机是否安装 JDK,如果没有安装,请安 ...
- Apple watch ,小米微信通知
Applewatch怎么显示微信通知?iWatch显示微信消息通知设置方法.大家都知道Applewatch上收到微信消息,iWatch是不会显示通知的,需要用户自行设置才行.下面小编来教大家如何设置A ...
- java方法体
执行顺序: 静态代码块 mian方法 构造代码块 构造方法 静态代码块只执行一次.
- 20175213 2018-2019-2 《Java程序设计》第8周学习总结
教材学习内容总结 1:泛型主要目的是建立具有类型安全的集合框架,如链表,散列映射等数据结构. 泛型类的声明: class People<E> People是泛型类的名称,E是其中泛型,E可 ...
- 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)
承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行 在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...