HDU 4002 Find the maximum(JAVA)

本文介绍了一道关于欧拉函数的算法题目,通过对欧拉函数性质的深入分析,提出了有效的解决方案。文章首先介绍了题目的背景及要求,随后详细阐述了解题思路,包括如何利用欧拉函数的特性寻找特定数值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4002

 

 

 

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.

 

 

 

 

转自kuangbin

 

作者博客:www.cnblogs.com/kuangbin

通过发现规律其实很简单。

先是把前i个素数的乘积求出来,结果就是不超出n的素数和。

题意:给出一个整数n,求一个数x,x在1到n之间,并且x/φ(x)最大(其中φ(x)为x的欧拉函数)。
思路:
由欧拉函数为积性函数,即:如果gcd(m , n) == 1,则有φ(m * n) == φ(m) * φ(n);且由φ(p^k) == (p - 1) * p^(k - 1),可得
φ(n) == φ(p1^k1 * p2^k2 * p3^k3 * … * pt^kt)
      == φ(p1^k1) * φ(p2^k2) * φ(p3^k3) * … * φ(pt^kt)
      == (p1 - 1) * p1^(k1 - 1) * (p2 - 1) * p2^(k2 - 1) * (p3 - 1) * p3^(k3 - 1) * … * (pt - 1) * pt^(kt - 1)
      == p1^k1 * (1 - 1/p1) * p2^k2 * (1 - 1/p2) * p3^k3 * (1 - 1/p3) * … * pt^kt * (1 - 1/pt)
      == n * (1 - 1/p1) * (1 - 1/p2) * (1 - 1/p3) * … * (1 - 1/pt)
于是有
f(n) == n/φ(n) == 1 / [ (1 - 1/p1) * (1 - 1/p2) * (1 - 1/p3) * … * (1 - 1/pt) ]   (1)
由 (1)式 可知:要使f(x)最大,须使x含尽量多的不同素数因子。

 

样例,n=10,结果是6=2*3,n=100时,结果为30=2*3*5;

分析可知,先把从2开始的连续素数的乘积存在数组cc[][]中。然后找到一个不超过n的最大的cc[][]就是答案了。

 

代码如下:

 

 

package practice;

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
	static int[] prim = new int[1020];
	static BigInteger[] dp = new BigInteger[1020];
	static int cnt = 0;

	public void cal() {
		boolean[] vis = new boolean[1020];
		for (int i = 0; i <= 1005; i++) {
			vis[i] = false;
			dp[i] = BigInteger.ZERO;
			// dp[i] = BigInteger.valueOf(0);
		}
		for (int i = 2; i <= 1010; i++) {
			if (!vis[i]) {
				prim[++cnt] = i;
				for (int j = i; j * i <= 1010; j++) {
					vis[j * i] = true;
				}
			}
		}
		// for(int i = 1; i <= cnt; i++)
		// {
		// System.out.println(prim[i]);
		// }

		dp[0] = BigInteger.ONE;
		for (int i = 1; i <= cnt; i++) {
			dp[i] = dp[i - 1].multiply(BigInteger.valueOf(prim[i]));
		}
		// for(int i = 1; i <= cnt; i++)
		// {
		// System.out.println(dp[i]);
		// }
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main m = new Main();
		m.cal();
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			int T = 0;
			T = cin.nextInt();
			while (T > 0) {
				BigInteger n = cin.nextBigInteger();
				int pos = 0;
				for (int i = 1; i <= cnt; i++) {
					if (n.compareTo(dp[i]) < 0) {
						pos = i - 1;
						break;
					}
				}
				System.out.println(dp[pos]);
				T--;
			}
		}
	}
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值