PAT 甲级【1010 Radix】
- 本题范围long型(35)^10
- 枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举
- 代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] words = br.readLine().split(" "); String s1 = words[0]; String s2 = words[1]; int tag = Integer.valueOf(words[2]); long radix = Long.valueOf(words[3]); long n1 = 0;
long n2 = 0;
long max = 0; if (tag == 2) {
StringBuilder sb = new StringBuilder(s1);
s1 = s2;
s2 = sb.toString();
}
n1 = get(s1, radix);
max = getmax(s2);
long last = -1;
int m = s2.length();
long bound = max + 1;
long upper = max + 1;
if (m != 1) {
upper = (long) Math.pow((double) n1 / (select(s2.charAt(0))), 1.1f / (m - 1))+10;
// bound = (long) Math.pow((double) n1 / (select(s2.charAt(0)) + 1), 1.1f / (m - 1));
}
// bound = Math.max(max + 1, bound);
// while (bound <= upper) {
long mid = (bound + upper) / 2;
n2 = get(s2, mid);
if (n2 > n1) {
upper = mid - 1;
} else if (n2 == n1) {
System.out.println(mid);
return;
} else {
bound = mid + 1;
}
}
System.out.println("Impossible");
return;
} public static long getmax(String s2) {
long max = 0;
for (int i = 0; i < s2.length(); i++) {
long digit = select(s2.charAt(i));
if (digit > max) {
max = digit;
}
}
return max;
} private static long get(String s1, long radix) {
long n1 = 0;
long base = 1;
for (int i = s1.length() - 1; i >= 0; i--) {
long digit = select(s1.charAt(i));
n1 += digit * base;
base *= radix;
}
return n1;
} public static long select(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else {
return ch - 'a' + 10;
}
}
}
二分
本页面将简要介绍二分查找,由二分法衍生的三分法以及二分答案。
二分法
定义
二分查找(英语:binary search),也称折半搜索(英语:half-interval search)、对数搜索(英语:logarithmic search),是用来在一个有序数组中查找某一元素的算法。
过程
以在一个升序数组中查找一个数为例。
它每次考察数组当前部分的中间元素,如果中间元素刚好是要找的,就结束搜索过程;如果中间元素小于所查找的值,那么左侧的只会更小,不会有所查找的元素,只需到右侧查找;如果中间元素大于所查找的值同理,只需到左侧查找。
性质
时间复杂度
二分查找的最优时间复杂度为
。
二分查找的平均时间复杂度和最坏时间复杂度均为
。因为在二分搜索过程中,算法每次都把查询的区间减半,所以对于一个长度为
的数组,至多会进行
次查找。
空间复杂度
迭代版本的二分查找的空间复杂度为
。
递归(无尾调用消除)版本的二分查找的空间复杂度为
PAT 甲级【1010 Radix】的更多相关文章
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- PAT 甲级 1010 Radix
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
- PAT甲组 1010 Radix (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
- PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新
题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...
- PAT甲级——A1010 Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
随机推荐
- 错误解决:ElasticSearch SearchResponse的Hits[]总是比totalHits少一条记录
在做ElasticSearch查询操作的时候,发现Hits[].length总是比totalHits.value少1.代码如下: SearchRequest request = new SearchR ...
- Build 和 Compile 区别
- 问题解决:Ubuntu18.04显示器分辨率不正常
在Ubuntu18.04下出现显示器分辨率不正确的情况,只能选择1024x768的分辨率,没有其它选项,显示器本身可以支持1920x1080的分辨率.经查询,采用cvt, xrandr的方法不成功,显 ...
- Java集合篇之set,面试官:请说一说HashSet、LinkedHashSet、TreeSet的区别?
写在开头 Java的集合世界中主要由List,Set,Queue,Map构成,我们在之前的博文中已经学习了List,接下来我们继续学习Set集合. Set特点:存取无序,不可以存放重复的元素,不可以用 ...
- Direct2D CreateBitmap的使用
当需要设置位图的混合模式时,应该使用ID2D1DeviceContext而不是ID2D1RenderTarget. 代码如下: #define WIN32_LEAN_AND_MEAN #include ...
- pinia
Pinia学习 Vue3中 使用 官网:https://pinia.web3doc.top/introduction.html 安装 yarn add pinia # 或者使用 npm npm ins ...
- 文心一言 VS 讯飞星火 VS chatgpt (202)-- 算法导论15.3 1题
一.对于矩阵链乘法问题,下面两种确定最优代价的方法哪种更高效?第一种方法是穷举所有可能的括号化方案,对每种方案计算乘法运算次数,第二种方法是运行RECURSIVE-MATRIX-CHAIN.证明你的结 ...
- 用Taro写一个微信小程序——渲染 HTML
在taro小程序里面渲染一段html代码,具体可查看文档https://docs.taro.zone/docs/html 一.渲染 HTML 文档推荐使用 dangerouslySetInnerHTM ...
- 【LeetCode二叉树#04】判断对称二叉树、相同的树、另一棵子树、树的子结构(二叉树相等判断)
对称二叉树 力扣题目链接(opens new window) 给定一个二叉树,检查它是否是镜像对称的. 思路 本题中,不能单纯去比较左右子节点的是否对称(都有值且不为空) 因为如果按上面那样做的话,到 ...
- MySQL基础3-数据库增删改操作
一.SQL通用语法 SQL 语法可以单行或者多行书写,以分号结尾. SQL 语句可以使用空格/缩进来增强语句的可读性. MySQL 数据库的 SQL 语句不区分大小写,关键字建议使用大写. 注释: 单 ...