2018焦作ICPC E - Resistors in Parallel题目链接
Time limit 2000 ms
Memory limit 1048576 kB
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.
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.
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.
3
10
100
1000
1/2
5/12
35/96
规律:
10,6=2*3
100,30=2*3*5
1000,210=2*3*5*7
因子和:
1,1
2,3
6,12
30,72
210,576
分子:
1 2 6 30 210
*2 *3 *5 *7(全是素数)
分母:
1 3 12 72 576
*3 *4 *6 *8(为上面素数+1)
思路:用Java大数去写,先打素数表,然后按照规律用大数求值
1 import java.io.*; 2 import java.util.Scanner; 3 import java.math.BigInteger; 4 import java.math.*; 5 import java.util.*; 6 public class Main 7 { 8 static int N=1005; 9 static int cnt=0; 10 static int[] prime=new int[N]; 11 static int[] is=new int[N]; 12 public static void table(){ 13 is[0]=is[1]=1; 14 for(int i=2;i<N;i++){ 15 if(is[i]==0){ 16 prime[++cnt]=i; 17 } 18 else continue; 19 for(int j=2;i*j<N;j++){ 20 is[i*j]=1; 21 } 22 } 23 } 24 public static void main (String[] args) throws java.lang.Exception 25 { 26 table(); 27 Scanner scan=new Scanner(System.in); 28 int t; 29 t=scan.nextInt(); 30 while(t--!=0){ 31 BigInteger x=scan.nextBigInteger(); 32 BigInteger fz=BigInteger.valueOf(1); 33 BigInteger fm=BigInteger.valueOf(1); 34 BigInteger bef_fz=fz; 35 BigInteger bef_fm=fm; 36 for(int i=1;i<=cnt;i++){ 37 bef_fz=fz; 38 bef_fm=fm; 39 fz=fz.multiply(BigInteger.valueOf(prime[i])); 40 fm=fm.multiply(BigInteger.valueOf(prime[i]+1)); 41 if(fz.compareTo(x)==1)break; 42 } 43 BigInteger y=bef_fz.gcd(bef_fm); 44 bef_fz=bef_fz.divide(y); 45 bef_fm=bef_fm.divide(y); 46 System.out.println(bef_fz+"/"+bef_fm); 47 } 48 } 49 }