PAT 甲级【1010 Radix】

这篇具有很好参考价值的文章主要介绍了PAT 甲级【1010 Radix】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  1. 本题范围long型(35)^10
  2. 枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举
  3. 代码如下
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】 的数组,至多会进行 PAT 甲级【1010 Radix】 次查找。

空间复杂度

迭代版本的二分查找的空间复杂度为 PAT 甲级【1010 Radix】

递归(无尾调用消除)版本的二分查找的空间复杂度为 文章来源地址https://www.toymoban.com/news/detail-711396.html

到了这里,关于PAT 甲级【1010 Radix】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(35)
  • 1114 Family Property (PAT甲级)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    浏览(49)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(39)
  • 1083 List Grades (PAT甲级)

    Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. Input Specification: Each input file contains one test case. Each case is given in the following format: where  name[i]  and  ID[i

    2024年02月08日
    浏览(48)
  • 1072 Gas Station (PAT甲级)

    A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendatio

    2024年02月09日
    浏览(40)
  • PAT甲级图论相关题目

    PAT甲级图论相关题目: 分数 25 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some o

    2024年01月21日
    浏览(52)
  • 1021 Deepest Root (PAT甲级)

    1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)_柳婼的博客-CSDN博客 柳婼的解法在这里,两次dfs,还是挺好玩的。 我的解法比较暴力,就是先用并查集算连通分量(这个其实还是dfs来算会更方便),如果只有一个连通分量,那deepest root一定在仅有一条arc的

    2024年02月15日
    浏览(32)
  • 1111 Online Map (PAT甲级)

    这道题我读题不仔细导致踩了个大坑,一个测试点过不了卡了好几个小时:第二个dijkstra算法中,题目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我却想当然地认为在fastest path is not unique的时候,判断标准是最短距离…… Input our

    2024年02月07日
    浏览(42)
  • 1074 Reversing Linked List (PAT甲级)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月09日
    浏览(40)
  • PAT 甲级【1007 Maximum Subsequence Sum】

    本题是考察动态规划与java的快速输入: max[i]表示第i个结尾的最大的连续子串和。b begin[i]表示第[begin[i],i]为最大和的开始位置 超时代码: 未超时: 能用动态规划解决的问题,需要满足三个条件:最优子结构,无后效性和子问题重叠。 具有最优子结构也可能是适合用贪心的

    2024年02月08日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包