2018 焦作 onsite E - Resistors in Parallel(数学或规律+大数)

题目链接:http://codeforces.com/gym/102028/problem/EE. Resistors in Parallel

time limit per test
2.0 s
memory limit per test
1024 MB
input
standard input
output
standard output

In this physics problem, what we are concerned about are only resistors. If you are poor at physics, do not worry, since solving this problem does not require you to have advanced abilities in physics.

Resistors are said to be connected together in parallel when both of their terminals are respectively connected to each terminal of the other resistors.

We have the following parallel resistor equation for k resistors with resistances R1, R2, …, Rk in parallel and their combined resistance R:

Now you have n resistors, the i-th of which has a resistance of ri ohms with the equation

You also have n selections, the i-th of which is a set of resistors Si such that

Please find a selection in which the resistors form a parallel resistor with the minimum resistance and output the reduced fraction of its resistance.
Input

The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 100.

For each test case, the only one line contains an integer n, where 1 ≤ n ≤ 10100.
Output

For each test case, output a line containing a reduced fraction of the form p/q indicating the minimum possible resistance, where p and q should be positive numbers that are coprime.
Example
Input
Copy

3
10
100
1000

Output
Copy

1/2
5/12
35/96

解析:

前i个素数相乘的积就是最大值所在的位置,得大数,然鹅java忘了;
好久没写java,啥都不记得了

code:

/* 
BigInteger.valueOf(s)  // 将参数转化为指定类型,这里是BigInteger 
b.compareTo(BigInteger.ZERO) // 若小于返回-1,等于则返回0,大于则返回1  ;BigInteger.ZERO是大数的0
Scanner sc=new Scanner(System.in);
BigInteger big1=sc.nextBigInteger();
BigInteger big2=sc.nextBigInteger();
System.out.println("加法操作:" + big1.add(big2));
System.out.println("减法操作:" + big1.subtract(big2));
System.out.println("乘法操作:" + big1.multiply(big2));
System.out.println("除法操作:" + big1.divide(big2));
BigInteger result[] = big1.divideAndRemainder(big2);
System.out.println("相除后的商是:" + result[0]);
System.out.println("相除后的余数是:" + result[1]);
*/

import java.math.BigInteger;
import java.util.Scanner;



public class Main{
	
	static int N= 100005;
	static int[] prime = new int[N];
	
	static BigInteger[] num = new BigInteger[110];
	static BigInteger[] fz = new BigInteger[110];
	static BigInteger[] fm = new BigInteger[110];
	
	//gcd 
	public static BigInteger gcd(BigInteger a, BigInteger b)
	{
		if(b.compareTo(BigInteger.ZERO)==0) // 若小于返回-1,等于则返回0,大于则返回1
			return a;
		else 
			return gcd(b,a.mod(b));
	}
	
	static BigInteger Gcd;
	
	public static void getPrime() // 素数筛,注意静态函数只能调用静态变量
	{
		num[0]=BigInteger.valueOf(1);
		fz[0]=BigInteger.valueOf(1);
		fm[0]=BigInteger.valueOf(1);
		
		for(int i=2;i<N;i++)
		{
			if(prime[0]>100) break;
			if(prime[i]==0)
			{
				prime[++prime[0]]=i;
				num[prime[0]]=num[prime[0]-1].multiply(BigInteger.valueOf(i));
				fz[prime[0]]=fz[prime[0]-1].multiply(BigInteger.valueOf(i));
				fm[prime[0]]=fm[prime[0]-1].multiply(BigInteger.valueOf(i+1));
				Gcd=gcd(fz[prime[0]],fm[prime[0]]);
				fz[prime[0]]= fz[prime[0]].divide(Gcd);
				fm[prime[0]]= fm[prime[0]].divide(Gcd);
				
			}
			for(int j=1;j<=prime[0]&&i*prime[j]<N;j++)
			{
				prime[i*prime[j]]=1;
				if(i%prime[j]==0)
					break;
			}
		}
	}
	
	
	public static void main(String[] args){
		
		getPrime();
		
		
		Scanner sc = new Scanner(System.in);
		
		
		int t;
		
		// for(int i=0;i<2;i++)
		// {
			// System.out.println(num[i]);
		// }
		BigInteger n;
		t=sc.nextInt();
		while(t--!=0)
		{
			n=sc.nextBigInteger();
			int idx=0;
			while(n.compareTo(num[idx])>=0)//返回第一个大于n的位置idx
			{
				idx++;
			}
			if(idx==1)
				System.out.println("1/1");
			else
			{
				System.out.println(fz[idx-1]+"/"+fm[idx-1]);
			}
			
			
			
		}
		
	}
}

你可能感兴趣的:(高精度计算与代数计算)