您的位置:首页 > 其它

HDU 4002 Find the maximum(欧拉函数)

2018-03-31 20:01 429 查看
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]区间中iϕ(i)iϕ(i)的最大值,其中ϕ(i)ϕ(i)为i的欧拉函数

思路

直接先对式子套用欧拉公式

iϕ(i)=ii(1−1p1)(1−1p2)⋯(1−1pm)iϕ(i)=ii(1−1p1)(1−1p2)⋯(1−1pm)

=1(1−1p1)(1−1p2)⋯(1−1pm)=1(1−1p1)(1−1p2)⋯(1−1pm)

=p1p2⋯pm(p1−1)(p2−1)⋯(pm−1)=p1p2⋯pm(p1−1)(p2−1)⋯(pm−1)

因为p1p1到pmpm是不同的质数,所以这个就是一个增函数,随着质因子的增多,这iϕ(i)iϕ(i)的值也就增大,所以我们只要找不超过n质因子越多的数就好了,换句话说与其找这个数不如直接用不同的质因子来乘成到小于等于n为止,因为n的范围是,1010010100所以我们质数打表打到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;
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: