1. 程式人生 > 實用技巧 >HDU - 6182 A Math Problem

HDU - 6182 A Math Problem

給定n

問 k ^ k <= n的最大k

注意到n的範圍才1e18,考慮列舉k就好了

不用想複雜

import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.math.*;
import java.io.*;
import java.util.Scanner;
import static java.lang.System.out; import static java.lang.Math.*; import java.util.*; import java.math.BigInteger; public class Main { static BigInteger quickPower(BigInteger a, BigInteger b) { BigInteger base = a; BigInteger ans = BigInteger.ONE; while(b.compareTo(BigInteger.ZERO) > 0){
if(b .and(BigInteger.ONE).equals(BigInteger.ONE)) ans = ans.multiply(base); base = base.multiply(base); b = b.divide(BigInteger.valueOf(2)); } return ans; } public static void main(String[] args) { Scanner in = new Scanner (System.in); BigInteger n;
while(in.hasNextBigInteger()){ n = in.nextBigInteger(); int cnt = 15; for(int i = 15; i >= 1 ;i--){ if(quickPower(BigInteger.valueOf(i),BigInteger.valueOf(i)).compareTo(n) > 0) cnt--; else break; } System.out.println(cnt); } } }