杭电4002 亚洲区预赛大连赛区

本文详细介绍了如何使用Java解决大数问题,并通过不断学习和实践,最终成功AC(Accepted)的过程。从基础知识入手,逐步深入,最终完成一道具有挑战性的编程题目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题先是用数学公式可以推出来一个式子,接下来就是大数,,,碰到好几次大数了,以前都是用c或c++写的,这道题用c或c++实在是太麻烦,于是一咬牙,一跺脚,硬着头皮看起了Java,上午看了一点,下午问了问xd一些基本语法,于是便开始写了,既借助网上代码,又不断的问xd,终于ac了,这叫一个不容易啊,,用Java写的第一道题啊,,,,接下来再练一下Java,把nyoj上的大数题用Java再做一遍。。。。。。题目:

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
ac代码:

import java.util.*; import java.math.BigInteger; public class Main { /** * @param args */ public static boolean pri(int n){ for(int i=2;i<=(int)Math.sqrt(n);++i) { if(n%i==0) return false; } return true; } public static void main(String[] args) { // TODO Auto-generated method stub BigInteger aa[]=new BigInteger[110]; int j=0,tt; int bb[]=new int[110]; Scanner cin=new Scanner(System.in); for(int i=2;j<110;++i) { if(pri(i)) { bb[j++]=i;} } aa[1]=new BigInteger("6"); BigInteger b6=new BigInteger("6"); //BigInteger b2=new BigInteger("2"); for(int i=2;i<105;++i) { aa[i]=new BigInteger(""+bb[i]).multiply(aa[i-1]); } BigInteger n; tt=cin.nextInt(); while(tt-->0) { n=cin.nextBigInteger(); if(n.compareTo(b6)<0) { if(n.compareTo(BigInteger.ONE)==0) System.out.println("1"); else System.out.println("2"); } else if(n.compareTo(b6)==0) System.out.println("6"); else{ for(int i=1;i<102;++i) { if(n.compareTo(aa[i])<=0) { System.out.println(aa[i-1]); break; } } } } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值