• hdu 4002 Find the maximum


    Find the maximum

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


    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.
     
    Source
     1 import java.io.*;
     2 import java.awt.*;
     3 import java.math.BigInteger;
     4 import java.util.Scanner;
     5 
     6 public class Main {
     7 
     8     static int prime[] = new int [1002];
     9     static int len = 0;
    10     static BigInteger dp[] = new BigInteger[1002]; 
    11     public static void main(String[] args) {
    12         fun();
    13         int T=0;
    14         Scanner cin  =  new Scanner(System.in);
    15         T=cin.nextInt();
    16         while(T>0)
    17         {
    18             BigInteger n = cin.nextBigInteger();
    19             int x=0;
    20             for(int i=1;i<=len;i++)
    21             {
    22                 if(n.compareTo(dp[i])<0)
    23                 {
    24                     x=i-1;
    25                     break;
    26                 }
    27             }
    28             System.out.println(dp[x]);
    29             T--;    
    30         }
    31     }
    32     static void fun(){
    33         boolean s[] = new boolean[1007];
    34         for(int i=0;i<s.length;i++){
    35             s[i]=false;
    36         }
    37         for(int i=0;i<dp.length;i++){
    38             dp[i]=BigInteger.ZERO;
    39         }
    40         for(int i=2;i<1007;i++)
    41         {
    42             if(s[i]==true) continue;
    43             prime[++len]=i;
    44             for(int j=i*2;j<1007;j=j+i)
    45                 s[j]=true;
    46         }
    47         dp[0] = BigInteger.ONE;
    48         for(int i=1;i<=len;i++)
    49         {
    50             dp[i] = dp[i-1].multiply(BigInteger.valueOf(prime[i]));
    51         }
    52     }
    53 }
  • 相关阅读:
    第一周C语言作业
    C语言I博客园作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3714999.html
Copyright © 2020-2023  润新知