題目鏈接: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]);
}
}
}
}