Find the maximum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2293 Accepted Submission(s): 959
Problem Description
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
题意
求 [ 2 , n ] [2,n] [2,n]区间中 i ϕ ( i ) \frac{i}{\phi (i)} ϕ(i)i的最大值,其中 ϕ ( i ) \phi (i) ϕ(i)为i的欧拉函数
思路
直接先对式子套用欧拉公式
i
ϕ
(
i
)
=
i
i
(
1
−
1
p
1
)
(
1
−
1
p
2
)
⋯
(
1
−
1
p
m
)
\frac{i}{\phi (i)}=\frac{i}{i(1-\frac{1}{p_{1}})(1-\frac{1}{p_{2}})\cdots(1-\frac{1}{p_{m}})}
ϕ(i)i=i(1−p11)(1−p21)⋯(1−pm1)i
=
1
(
1
−
1
p
1
)
(
1
−
1
p
2
)
⋯
(
1
−
1
p
m
)
=\frac{1}{(1-\frac{1}{p_{1}})(1-\frac{1}{p_{2}})\cdots(1-\frac{1}{p_{m}})}
=(1−p11)(1−p21)⋯(1−pm1)1
=
p
1
p
2
⋯
p
m
(
p
1
−
1
)
(
p
2
−
1
)
⋯
(
p
m
−
1
)
=\frac{p_{1}p_{2}\cdots p_{m}}{(p_{1}-1)(p_{2}-1)\cdots(p_{m}-1)}
=(p1−1)(p2−1)⋯(pm−1)p1p2⋯pm
因为
p
1
p_1
p1到
p
m
p_{m}
pm是不同的质数,所以这个就是一个增函数,随着质因子的增多,这
i
ϕ
(
i
)
\frac{i}{\phi (i)}
ϕ(i)i的值也就增大,所以我们只要找不超过n质因子越多的数就好了,换句话说与其找这个数不如直接用不同的质因子来乘成到小于等于n为止,因为n的范围是,
1
0
100
10^{100}
10100所以我们质数打表打到60即可,用java的BigIntege来操作比较方便
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String [] args)
{
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281};
BigInteger a[]=new BigInteger[60];
for(int i=0;i<60;i++)
a[i]=BigInteger.valueOf(prime[i]);
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t-->0)
{
BigInteger b=new BigInteger(in.next());
BigInteger c=BigInteger.ONE;
for(int i=0;i<60;i++)
{
if(c.multiply(a[i]).compareTo(b)<=0)
c=c.multiply(a[i]);
else
{
System.out.println(c);
break;
}
}
}
}
}