HDU 4002Find the maximum(尤拉函式)

果7發表於2013-08-19

Find the maximum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1459    Accepted Submission(s): 632


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
Hint
If the maximum is achieved more than once, we might pick the smallest such n.
 


                       題目大意:找出不超過n的n/phi(n)的最大值。首先要知道phi(n))表示不超過n且與n互質的正整數的個數。

              解題思路:phi(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)   p1到pn是x的素因子,n/phi(n)則表示1/(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),則希望素因子最多的時候分母最小,分值最大。詳解見程式碼。

              題目地址:Find the maximum

AC程式碼:
import java.util.*;
import java.io.*;
import java.math.*;

public class Main  //qixifestival 
{
     public static void main(String args[])
     {
    	 int mark[],i,j; //mark為1,則表示prim
    	 mark=new int[502];
    	 for(i=0;i<=500;i++)
    		 mark[i]=1;
    	 mark[0]=0;mark[1]=0;
    	 for(i=2;i<=25;i++)  //篩選素數
    	 {
    		 if(mark[i]==1)
    		 {
    			 for(j=i*i;j<500;j+=i)
    				 mark[j]=0;
    		 }
    	 }
    	 
    	 BigInteger prim[];     //儲存素數
    	 int t=0;
    	 prim=new BigInteger[102];
    	 for(i=2;i<=500;i++)
    		 if(mark[i]==1)
    			 prim[++t]=BigInteger.valueOf(i);
         
    	 //System.out.println(t);  
    	 BigInteger res[];     //儲存素數積,即結果
    	 res=new BigInteger[80];
    	 res[1]=BigInteger.valueOf(2);
    	 for(i=2;i<56;i++)
    		 res[i]=res[i-1].multiply(prim[i]);
    	 
    	 /*String s=res[55].toString();  
    	 int len=s.length();           
    	 System.out.println(len);*/  //輸出104說明55可以裝下100位
    	 
    	 Scanner cin= new Scanner(System.in);
    	 int n;
    	 n=cin.nextInt();
    	 while(n!=0)
    	 {
    		 n--;
    		 BigInteger cur;
    		 cur=cin.nextBigInteger();
    		 for(i=55;i>=1;i--)
    		 {
    			 if(res[i].compareTo(cur)<=0)
    			 {
    				 System.out.println(res[i]);
    				 break;
    			 }
    		 }
    	 }
     }
}

//1406MS 4144K



相關文章