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 = ...
随机推荐
- 文件服务之二:ftp协议
FTP连接 命令连接 传输命令(客户端发给服务端的命令),服务端的21/tcp 数据连接 传输数据(传输数据时建立,数据传输完拆除) 数据链接的建立方法:主动.被动 主动模式(PORTstyle服务器 ...
- 分布式一致性算法2PC和3PC
为了解决分布式一致性问题,产生了不少经典的分布式一致性算法,本文将介绍其中的2PC和3PC.2PC即Two-Phase Commit,译为二阶段提交协议.3PC即Three-Phase Commit, ...
- shell脚本修改文本中匹配行之前的行的方法
原创文件,欢迎阅读,禁止转载. 例子中是把 finish 前一行的 "yes" 改成 "YES"有一个方法就是利用sed+awk一起来完成. zjk@zjk:~ ...
- Python——Django运行问题
1.Python3.7+Django2.2操作Mysql数据库时出现如下错误:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3. ...
- 43_redux_counter应用_使用redux调试用具
1.要在chrome中安装插件 redux-devtools_2_12_1.crx 2.在开发工具注入 npm install --save-dev redux-devtools-extension ...
- 百度地图点聚合MarkerClusterer性能优化
公司要求做个百度地图点聚合的性能优化,需一次性加载9万条数据. 记录下自己的优化过程.(只想看优化代码的可直接移步:步骤三) 一.引入百度地图 vue项目中,在index.html文件中用script ...
- 虚拟机安装及ubuntu-16.04.3-desktop-amd64.iso映像文件的安装
虚拟机安装及ubuntu-16.04.3-desktop-amd64.iso映像文件 搞了大半天才搞清楚装linux的前提是要先安装虚拟机的 先下载虚拟机,在然后创建虚拟机,在虚拟机里面再安装linu ...
- crontab,定时任务执行找不到库or shell可执行,crontab 定时任务下就不能执行,tensorflow,ImportError: libcuda.so.1: cannot open shared object file: No such file or directory
在线上启动一个定时任务,但是起来查看,发现任务执行找不到库,报cuda错误: ImportError: libcuda.so.1: cannot open shared object file: No ...
- C#客户端和服务端数据的同步传输 (转载)
客户端: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;u ...
- Polar Code(1)关于Polar Code
Polar Codes于2008年由土耳其毕尔肯大学Erdal Arikan教授首次提出,Polar Codes提出后各通信巨头都进行了研究.2016年11月18日(美国时间2016年11月17日), ...