1. 程式人生 > 實用技巧 >Gym - 102028E E - Resistors in Parallel

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