HDU 4002 find the maximum (number theory-Euler's function)

Source: Internet
Author: User

Find the maximum
Problem descriptioneuler's totient function, Phi (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, Phi (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 PHI (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/PHI (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.
Inputthere are t test cases (1 <= T <= 50000). For each test case, standard input contains a line with 2 ≤ n ≤ 10 ^ 100.
Outputfor each test case there shoshould be single line of output answering the question posed above.
Sample Input
210100
 
Sample output
630HintIf the maximum is achieved more than once, we might pick the smallest such n.   
 
Sourcethe 36th ACM/ICPC Asia Regional Dalian site -- online contest
Recommendlcy | we have carefully selected several similar problems for you: 4007 4003 4008 4005
Question:

Given an N, ask you 1 ~ In N, calculate a number X to maximize the value of X/PHI (X.


Solution:

According to the Euler's function formula, Phi (x) = x (1-1/P1) (1-1/P2) (1-1/P3) (1-1/P4) ..... (1-1/PN)

Then: X/PHI (x) = p1/(p1-1) * P2/(p2-1) *... * pn/(pn-1)

We can see that the more items x/PHI (x), the greater the factor, and the smaller the X/PHI (x), the greater the number, the only difference is 2*3*5*7 ....

Considering that the numbers are large, you can use Java to write them. Is it because of the large Java numbers, you know ..


Solution code:

import java.util.*;import java.math.*;public class Main {public static void main(String[] args) {int maxn=2100;boolean []isPrime =new boolean[maxn];int tol=0;int []v=new int[210];for(int i=0;i<maxn;i++) isPrime[i]=true;  for(int i=2;i<maxn;i++){if(tol>56) break;        if(isPrime[i]) v[tol++]=i;        for(int j=0;j<tol && i*v[j]<maxn;j++){              isPrime[i*v[j]]=false;              if(i%v[j]==0) break;          }}//System.out.println(tol);//for(int i=0;i<tol;i++) System.out.println(v[i]);BigInteger []a=new BigInteger[200];a[0]=new BigInteger("1"); for(int i=1;i<56;i++){BigInteger tmp= BigInteger.valueOf(v[i-1]) ;//System.out.print(tmp+" ");a[i]=a[i-1].multiply(tmp);//System.out.println(a[i]);}Scanner scan=new Scanner(System.in);int t=scan.nextInt();while(t-- >0){String str=scan.next();BigInteger x=new BigInteger(str);BigInteger ans=new BigInteger("2");for(int i=1;i<56;i++){if(x.compareTo(a[i]) >=0){ans=a[i];}else break;}System.out.println(ans);}}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.