2018焦作 Gym - 102028E - Resistors in Parallel 【乱搞】

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/qindicating the minimum possible resistance, where p and q should be positive numbers that are coprime.

分析:通过手动打表得出,最小值分别在1,1*2,1*2*3,1*2*3*5,1*2*3*5*7。。。得到。

分子即使连续的素数乘积,分母是分子的全部因子之和(公式)。

由于要用到高精度,直接用java会比较好写。

//package ac;
import java.math.*;
import java.util.*;;
public class Main {
public static void main(String[] args) {
        boolean[] vis=new boolean[10004];
        int [] prim = new int[10004];
        int cnt=0;
        for(int i=2;i<10004;++i)
        {
            if(!vis[i])prim[cnt++]=i;
            for(int j = 0;j<cnt && i*prim[j] < 10004;j++)
            {
                vis[i*prim[j]]=true;
                if(i%prim[j]==0)break;
            }
        }
        BigInteger[] temp = new BigInteger[10004];
        temp[0]=new BigInteger("1");
        for(int i=1;i<=cnt;i++)
        {
            temp[i]=temp[i-1].multiply(new BigInteger("0"+prim[i-1]));
        }
        BigInteger base = new BigInteger("1");
        Scanner cin = new Scanner (System.in);
        int t=cin.nextInt();
        while(true)
        {
            t--;
            if(t<0)break;
            BigInteger n =cin.nextBigInteger();
            if(n.compareTo(new BigInteger("1"))==0) {
                System.out.println(1+"/"+1);
                continue;
            }
            else if(n.compareTo(new BigInteger("6"))==-1) {
                System.out.println(2+"/"+3);
                continue;
            }
            int pos=0;
            for(int i=1;i<=cnt;i++)
            {
                if(temp[i].compareTo(n)==1){
                    n=temp[i-1];
                    pos=i-1;
                    break;
                }
            }
            BigInteger mul = new BigInteger("4");
            BigInteger dw = new BigInteger("12");
            for(int i=2;i<pos;i++)
            {
                BigInteger cha = new BigInteger("0"+(prim[i]-prim[i-1]));
                mul=mul.add(cha);
                dw=dw.multiply(mul);
            }
            BigInteger g = dw.gcd(n);
            dw=dw.divide(g);
            n=n.divide(g);
            System.out.println(n+"/"+dw);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/90112462