B - Find the maximum HDU - 4002

 Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6.
HG is the master of X Y. One day HG wants to teachers XY something about Euler's Totient function by a mathematic game. That is HG gives a positive integer N and XY tells his master the value of 2<=n<=N for which φ(n) is a maximum. Soon HG finds that this seems a little easy for XY who is a primer of Lupus, because XY gives the right answer very fast by a small program. So HG makes some changes. For this time XY will tells him the value of 2<=n<=N for which n/φ(n) is a maximum. This time XY meets some difficult because he has no enough knowledge to solve this problem. Now he needs your help. 

Input
There are T test cases (1<=T<=50000). For each test case, standard input contains a line with 2 ≤ n ≤ 10^100.
Output
For each test case there should be single line of output answering the question posed above.
Sample Input

2
10
100

Sample Output

6
30

Hint

If the maximum is achieved more than once, we might pick the smallest such n.  

欧拉函数,这里设为f(x)。

f(x) = x * (1 - 1/p1) * (1 - 1/p2)* …*(1 - 1/pi)
其中pi为x的质因子。
因此x/f(x) =1 /((1 - 1/p1) * (1 - 1/p2)* …(1 - 1/pi)) = (p1*p2..pi)/((p1-1)(p2-1)..(pi - 1))
令g(x) = x/(x - 1) = 1 + 1/(x - 1),(x >= 2) g(x) 在[2,)单调递增,因为x总是可以
写成多个质因子相乘的形式,要令x/f(x)最大,则需要从2开始取质因子并相乘,并
保证其小于x,为了保证这样的x最小,则质因子不重复取即可。
java大数。

import java.math.BigInteger;
import java.util.Scanner;
import java.lang.Math;
public class dd {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int b[] = new int[200];
        b[0] = 2;
        int len = 1;
        for(int i = 3;i <= 300;i += 2){
            int t = (int)Math.sqrt((double)i);
            boolean flag = true;
            for(int j = 2;j <= t;++j){
                if(i % j == 0){
                    flag = false;
                    break;
                }
            }
            if(flag){
                b[len++] = i;
            }
        }
        int T;
        T = scan.nextInt();
        while(T != 0){
            T--;
            BigInteger n = scan.nextBigInteger();
            BigInteger a = BigInteger.valueOf(1);
            int k = 0;
            while(a.compareTo(n) <= 0){
                BigInteger t = BigInteger.valueOf(b[k++]);
                a = a.multiply(t);
            }
            k--;
            a = a.divide(BigInteger.valueOf(b[k]));
            //System.out.println(b[k]);
            System.out.println(a);
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值