B

来源:互联网 发布:中世纪2王国优化9补丁 编辑:程序博客网 时间:2024/05/01 22:37
 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

210100

Sample Output

630

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);        }    }}
原创粉丝点击