Find the maximum 欧拉函数 高精度 BigInteger TWT Tokyo Olympic 2COMBO-1

欧拉函数挑战
本文介绍了一个基于欧拉函数的数学游戏编程挑战,通过分析题目给出的数学表达式,提出了一种利用质因数分解的方法来解决大数值输入的问题,并使用Java的BigInteger实现。

Find the maximum

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


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

喜闻乐见欧拉函数!

马上在草稿纸上写写写,发现题目给的那个玩意儿化简后就是(p1*p2*p3*。。。。。*pn)/((p1-1)*(p2-1)*(p3-1)*。。。。*(pn-1))

那么就明白了。。。一个数拥有不同的质因子数量越多,这个数的题目所求值就越大

问题是题目给的区间是10^100。。肯定要找区间吧。。。。每个区间都有一个最大值

但是不想写c++的打表。。。。就想试一试一直听说的java的BigInteger

想法就是先打个2000以内的素数,然后将它们分别累乘用数组储存起来,这个数字的含义就是比该数大但比下一个求得值小的n/phi(n)的最大值

感觉还挺好用的,就是不知道为什么书上给的埃氏筛法模版写进去会wa。。。。。

哪日明白了补上吧,还要补上打表的程序

未完待续

java代码:

package acm;
import java.io.*;
import java.awt.*;
import java.math.BigInteger;
import java.util.Scanner;
import java.lang.*;
public class Main {
	static int maxn=2005;
	static boolean vis[]=new boolean [maxn];
	static int prime[]=new int [maxn];
	static BigInteger save[]=new BigInteger[maxn];
	static void init(){
		int i,j,p_n=1;
		for(i=0;i<maxn;i++){
			vis[i]=false;
		}
		for(i=0;i<maxn;i++){
			save[i]=BigInteger.ZERO;
		}
		/*int m=(int)Math.sqrt((double)maxn+0.5);
		for(i=2;i<=m;i++){
			if(vis[i]==false){
				prime[p_n]=i;
				p_n++;
				for(j=i*i;j<maxn;j+=i){
					vis[j]=true;
				}
			}
		}*/
		for(i=2;i<maxn;i++){
            if(vis[i]==false){
                prime[p_n]=i;
                p_n++;
                for(j=2*i;j<maxn;j+=i){
                    vis[j]=true;
                }
            }
        }
		save[0]=BigInteger.ONE;
		for(i=1;i<p_n;i++){
			save[i]=save[i-1].multiply(BigInteger.valueOf(prime[i]));
		}
	}
    public static void main(String[] args) {
        init();
        int t,i,j;
        Scanner in = new Scanner(System.in);
        t=in.nextInt();
        while(t!=0){
        	BigInteger n =in.nextBigInteger();
        	for(i=1;;i++){
        		if(n.compareTo(save[i])<0){
        			break;
        		}
        	}
        	System.out.println(save[i-1]);
        	t--;
        }
    }
    
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值