Beautiful Numbers KickStart 2017 Round2

 

Problem

We consider a number to be beautiful if it consists only of the digit 1 repeated one or more times. Not all numbers are beautiful, but we can make any base 10 positive integer beautiful by writing it in another base.

Given an integer N, can you find a base B (with B > 1) to write it in such that all of its digits become 1? If there are multiple bases that satisfy this property, choose the one that maximizes the number of 1 digits.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case consists of one line with an integer N.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the base described in the problem statement.

Limits

1 ≤ T ≤ 100.

Small dataset

3 ≤ N ≤ 1000.

Large dataset

3 ≤ N ≤ 1018.

Sample

beautiful number要求数的所有位都是1,对于输入的一个数,要求输出一个进制,使得该数为beautiful number,且长度最大。
容易想到的解法,可以用于小数据集,时间复杂度为O(NlogN)。进制从2开始,循环至N-1,尝试该进制下是否是beautiful。
import java.util.*;
public class Solution{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = 0;
        if(scan.hasNext()) {
            n = Integer.parseInt(scan.next());
        }
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            if(scan.hasNext()) {
                res[i] = helper(Integer.parseInt(scan.next()));
            }
        }
        for(int i = 0; i < n; i++) {
            System.out.println("Case #" + (i + 1) + ": " + res[i]);
        }
        return;
    }
    
    public static int helper(int num) {
        for(int i = 2; i < num; i++) {
            int src = num;
            while(src > 0) {
                //System.out.println("src" + src + "i" + i + "余数:"+ (src % i));
                if(src % i == 1) {
                    src = src / i;
                }  else break;
            }
            //System.out.println();
            if(src == 0) return i;
        }
        return num - 1;
    }
}

大数据集合的解法:3 <= N <= 10^18。此时只能用long类型,long类型最大数值为2^64 - 1 = 9223372036854775807 ~ 4 * 10 ^ 18。所以N转换成beautiful number后的长度不会超过64(~logN)。

则从64开始到2,考虑每个长度是否有一种进位制使得该形式的数对应输入的num。采用二分搜索来搜索进位制,复杂度为O(logN)。每次需要将num和该长度的该进位制组成的数进行比较,组成该数字需要O(log位数)。

算法时间复杂度为O(logN*logN*logN)。

import java.util.*;
import java.io.*;
public class Solution_large {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int t = in.nextInt();  // Scanner has functions to read ints, longs, strings, chars, etc.
        long[] res = new long[t];
        for(int i = 0; i < t; i++) {
            res[i] = beautiful(in.nextLong());
        }
        for(int i = 0; i < t; i++) {
            System.out.println("Case #" + (i + 1) + ": " + res[i]);
        }
    }

    public static long beautiful(long num) {
        for(int bits = 64; bits >= 2; bits--) {
            long radix = getRadix(num, bits);
            if(radix != -1) return radix;
        }
        throw new IllegalStateException("should not reach here");
    }

    public static long getRadix(long n, int bits) {
        long lo = 2, hi = n;
        while(lo < hi) {
            long mid = lo + (hi - lo) / 2;
            long t = convert(mid, bits);
            if(t < n) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        if(convert(lo, bits) == n) return lo;
        return -1;
    }
    public static long convert(long radix, int bits){
        long sum = 0;
        long component = 1;
        for(int i = 0; i < bits; i++) {
            if(Long.MAX_VALUE - sum < component) {
                return Long.MAX_VALUE;
            }
            sum += component;
            if(component > Long.MAX_VALUE / radix) {
                component = Long.MAX_VALUE;
            } else component = component * radix;
        }
        return sum;
    }
}

除了算法的思想重要,另外的重点是注意两个溢出条件,一个是sum的溢出,一个是component的溢出。component领先sum,所以component溢出时候,sum还不一定溢出。

 

 

 
posted @ 2019-03-08 17:02  起点菜鸟  阅读(176)  评论(0编辑  收藏  举报