Gym - 102028E E - Resistors in Parallel

题目链接:https://vjudge.net/contest/397411#problem/E

题意:  要R尽量小  R从1到n 取倒数, 如果某一个数能被任意一个数的平方数除尽,这个电阻为正无穷

 

 

思路:找规律 发现 都是*2 *3 *5 *7 猜测是乘质数  答案 也是从1 开始 分子*2  分母*2+1 然后*3 *3+1  用java写即可

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main
 5 {
 6     static BigInteger f[]=new BigInteger[1010];
 7     static BigInteger x[]=new BigInteger[1010];
 8     static BigInteger y[]=new BigInteger[1010];
 9     static BigInteger g[]=new BigInteger[1010];
10     static BigInteger gcd(BigInteger a,BigInteger b)
11     {
12         if(b==BigInteger.valueOf(0)) return a;
13         return gcd(b,a.remainder(b));
14         
15     }
16     public static void main(String[] args)
17     {
18         int t;
19         Scanner cin=new Scanner(System.in);
20         t=cin.nextInt();
21         x[1]=BigInteger.valueOf(1);
22         y[1]=x[1];
23         
24         int cnt=0;
25         for(int i=2;i<=1000;i++)
26         {
27             int flag=0;
28             for(int j=2;j*j<=i;j++)
29             {
30                 if(i%j==0)
31                     flag=1;
32             }
33             if(flag==0)
34             {
35                 cnt++;
36                 g[cnt]=BigInteger.valueOf(i);
37             }
38         }
39         for(int i=2;i<=150;i++)
40         {
41             x[i]=x[i-1].multiply(g[i-1]);
42             y[i]=y[i-1].multiply(g[i-1].add(x[1]));
43         }
44         f[1]=BigInteger.valueOf(1);
45         for(int i=2;i<=150;i++)
46         {
47             f[i]=f[i-1].multiply(g[i-1]);
48         }
49         for(int i=0;i<t;i++)
50         {
51             BigInteger n=cin.nextBigInteger();
52             int index=0;
53             for(int j=1;j<=150;j++)
54             {
55                 if(n.compareTo(f[j])>=0)
56                 {
57                     index=j;
58                 }
59             }
60             BigInteger c=x[index];
61             BigInteger d=y[index];
62             BigInteger e=gcd(c,d);
63             c=c.divide(e);
64             d=d.divide(e);
65             System.out.println(c+"/"+d);
66             
67         }
68         
69     }
70 }
View Code

 

posted @ 2020-10-09 20:03  canwinfor  阅读(125)  评论(0)    收藏  举报