HDU 4002Find the maximum(尤拉函式)
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.
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
相關文章
- HDU2588GCD(尤拉函式)GC函式
- HDU 1695-GCD(容斥原理+尤拉函式)GC函式
- HDU 3501 Calculation 2 (尤拉函式應用)函式
- HDU3501 Calculation 2(尤拉函式推廣)函式
- 尤拉函式φ函式
- 4939 尤拉函式函式
- 尤拉函式入門函式
- 尤拉函式詳解函式
- poj 2478 尤拉函式函式
- 淺談尤拉函式函式
- 尤拉函式性質和模版函式
- 轉載:尤拉函式知識點總結及程式碼模板及尤拉函式表函式
- Note -「因數的尤拉函式求和」函式
- bzoj2818: Gcd(尤拉函式)GC函式
- POJ 2478-Farey Sequence(尤拉函式)函式
- B.日記和尤拉函式函式
- 尤拉計劃512題(冪的尤拉總計函式和)函式
- HDU 6298 Maximum Multiple(找規律)
- SG 函式初步 HDU 1536 && HDU 1944函式
- POJ 2048 Longge's problem (尤拉函式 積性函式)函式
- HDU 4279 2012網路賽Number(數論 尤拉函式結論約數個數)函式
- POJ 2407-Relatives-尤拉函式函式
- HDU 1709 The Balance(母函式)函式
- HDU1729 Stone Game (SG函式)GAM函式
- [藍橋杯][演算法提高VIP]尤拉函式演算法函式
- 51nod1262 扔球(尤拉函式)函式
- 尤拉函式、整除分塊和擴充套件歐幾里得函式套件
- §3. 復變數的指數函式·尤拉公式變數函式公式
- 尤拉計劃533題:卡邁克爾函式函式
- POJ2487Farey Sequence(尤拉函式&&法雷級數)函式
- 51nod1040 最大公約數之和 (尤拉函式 )函式
- BZOJ 2818 Gcd (莫比烏斯反演 或 尤拉函式)GC函式
- 杭電ACM hdu 2110 Crisis of HDU 解題報告(母函式)ACM函式
- 杭電ACM hdu 1171 Big Event in HDU 解題報告(母函式)ACM函式
- 紫書 例題 10-27 UVa 10214(尤拉函式)函式
- HDU 5795 A Simple Nim (SG函式+打表找規律)函式
- HDU 1846-Brave Game(巴什博弈-SG函式)GAM函式
- bzoj2190: [SDOI2008]儀仗隊(尤拉函式)函式