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 = ...
随机推荐
- C++_数字时钟
利用C++语言基础,制作了一个模拟电子时钟的程序. #include<iostream> #include<windows.h> //延时与清屏头文件 using namesp ...
- python大法好——Python 正则表达式
Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...
- Java学习之代码块(静态,构造代码块,构造方法)执行顺序
静态代码块 static{ 代码 } 随着类的加载而加载,随类的消失而消失,存在于类中,方法外,最先执行,且只加载1次,可用来加载驱动及初始化对象属性. 构造代码块 { } 也存在于类中, ...
- mysql for循环存储过程
DROP PROCEDURE IF EXISTS test_insert; DELIMITER ;; CREATE PROCEDURE test_insert () BEGIN DECLARE i i ...
- Stack类常用api
1.构造函数 Stack只有一个默认构造函数 Stack() Stack<Integer> stack = new Stack<Integer>(); 2.常用api (1)入 ...
- Linux gzip命令
语法: gzip [-acdfhlLnNqrtvV][-S <压缩字尾字符串>][-<压缩效率>][--best/fast][文件...] 或 gzip [-acdfhlLnN ...
- curl -d中的json存在引号怎么处理?
1\将其改写为I'\''m就可以执行 2\ curl -u elastic:mypass -X GET "localhost:9200/_analyze?pretty" -d 'a ...
- FileProvider的使用及应用更新时提示:解析包出错、失败等问题
Android 7.0以上的版本更新采用系统自带的DownloadManager更新 DOWNLOADPATH ="/download/" https://www.jianshu. ...
- Mongodb数据库学习
数据库 MongoDB (芒果数据库) 数据存储阶段 文件管理阶段 (.txt .doc .xls)优点 : 数据可以长期保存 可以存储大量的数据 使用简单 缺点 : 数据一致性差 数据查找修改不方便 ...
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
配置文件和注解混合使用 创建对象操作使用配置文件方式实现 注入属性的操作使用注解方式实现 编写BookDao.java和OrderDao.java文件 BookDao.java package com ...