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

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--;
        }
    }
    
}





欧拉函数(Euler's Totient Function),也称为积性函数,是指小于等于正整数n的数中与n互质的数的个数。我们通常用φ(n)表示欧拉函数。 具体来说,如果n是一个正整数,那么φ(n)表示小于等于n的正整数中与n互质的数的个数。例如,φ(1)=1,因为1是唯一的小于等于1的正整数且1与1互质;φ(2)=1,因为小于等于2的正整数中只有1与2互质;φ(3)=2,因为小于等于3的正整数中与3互质的数是1和2。 欧拉函数的计算方法有很多,下面介绍两种常见的方法: 1. 分解质因数法 将n分解质因数,假设n的质因数分别为p1, p2, …, pk,则φ(n) = n × (1 - 1/p1) × (1 - 1/p2) × … × (1 - 1/pk)。例如,对于n=30,我们将其分解质因数得到30=2×3×5,则φ(30) = 30 × (1-1/2) × (1-1/3) × (1-1/5) = 8。 2. 筛法 我们可以使用筛法(Sieve)来计算欧拉函数。具体地,我们可以先将φ(1)至φ(n)全部初始化为其下标值,然后从2开始遍历到n,将所有能被当前遍历到的数整除的数的欧拉函数值减1即可。例如,对于n=6,我们先初始化φ(1)=1, φ(2)=2, φ(3)=3, φ(4)=4, φ(5)=5, φ(6)=6,然后从2开始遍历,将2的倍数的欧拉函数值减1,即φ(4)=φ(6)=2;然后遍历3,将3的倍数的欧拉函数值减1,即φ(6)=2。最终得到φ(1)=1, φ(2)=1, φ(3)=2, φ(4)=2, φ(5)=4, φ(6)=2。 欧拉函数在数论中有很重要的应用,例如RSA算法的安全性就基于欧拉函数的难解性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值