HDU 4002 Find the maximum(JAVA 大数找规律 )
2015-09-24 22:44
651 查看
题目链接: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
HintIf 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--;
}
}
}
}
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
HintIf 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--;
}
}
}
}
相关文章推荐
- JAVA利用泛型返回类型不同的对象
- JAVA实现对Excel的操作
- 细说Java内部类, 静态内部类, 局部类, 匿名内部类
- JAVA_SE基础——31.this关键字
- What does “java: No match.” mean?
- Java多线程基础知识(四)
- Java覆盖和隐藏
- Callable和Future、FutureTask
- MyEclipse SVN 插件
- 转载老师的一篇文章——java自学之路
- 一个关于java数据库驱动包的小发现
- Java基础知识强化74:正则表达式之分割功能 (扩展练习)
- 用代码窥探Java支持的语言
- Java 基础总结
- Java学习日记-2 零零碎碎
- [转载]java servlet过滤器控制frame框架中的页面跳转
- Java基础知识强化73:正则表达式之分割功能
- Android 开发在Eclipse提示信息 This element neither has attached source nor attached Javadoc
- java基金会 之 HashMap统计csvWord文档
- java编程——数据的加法