码迷,mamicode.com
首页 > 编程语言 > 详细

2018 焦作E java 高精度暴力

时间:2019-11-01 09:15:14      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:bre   return   gem   pack   ted   mat   imp   ++i   ack   

https://vjudge.net/problem/Gym-102028E

暴力,线性递推分母

//package acm;
import java.math.BigInteger;
import java.util.Scanner;

import com.sun.org.apache.bcel.internal.generic.SWAP;
import com.sun.org.apache.bcel.internal.generic.StackConsumer;

import sun.management.GcInfoCompositeData;
public class Main{
	public static final int maxn=1000,maxm=200000;
	public static BigInteger gcd(BigInteger a,BigInteger b) {
		if(b.compareTo(BigInteger.ZERO)==0)return a; 
		else return gcd(b, a.remainder(b));
	}
    public static void main(String[] argc){
    	int[]a=new int[maxn];
    	for(int i=2;i<maxn;i++) a[i]=i;
    	for(int i=2;i<maxn;i++) if(a[i]!=0) 
    		for(int j=i*2;j<maxn;j=j+i) a[j]=0;
    	Scanner cin=new Scanner(System.in);
    	int casn=cin.nextInt();
    	int[] prime=new int[10000]; 
    	for(int ii=1;ii<=casn;++ii) {
    		BigInteger x=cin.nextBigInteger();
    		BigInteger res=BigInteger.ONE;
    		BigInteger now=BigInteger.ONE;
    		int i=1;
    		while(true) {
    			i++;
    			if(a[i]==0) continue;
    			if(res.multiply(BigInteger.valueOf(a[i])).compareTo(x)<=0) {
    				res=res.multiply(BigInteger.valueOf(a[i]));
    				now=now.multiply(BigInteger.valueOf(a[i]+1));
    			}else break;
    		}
    		BigInteger g=gcd(res,now);
    		res=res.divide(g);
    		now=now.divide(g);
    		System.out.println(res+"/"+now);
    	}
    }
}

 

2018 焦作E java 高精度暴力

标签:bre   return   gem   pack   ted   mat   imp   ++i   ack   

原文地址:https://www.cnblogs.com/nervendnig/p/11775062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!