ACM-ICPC Jiaozuo Onsite 2018 Resistors in Parallel (思维+java大数+找规律)

题目来源

ACM-ICPC Jiaozuo Onsite 2018

题目粘贴过来有点变化,既然来了肯定见过原题~~嘻嘻~~

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 kk resistors with resistances R_1, R_2, \cdots, R_kR1​,R2​,⋯,Rk​ in parallel and their combined resistance RR:

\displaystyle R = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2} + \cdots + \frac{1}{R_k}}.R=R1​1​+R2​1​+⋯+Rk​1​1​.

Now you have nn resistors, the ii-th of which has a resistance of r_iri​ ohms with the equation

\displaystyle r_i = \begin{cases} \infty & \text{if $i$ can be divided by $d^2$ for some integers $d\ge 2$,}\\ i & \text{otherwise.} \end{cases}ri​={∞i​if i can be divided by d2 for some integers d≥2,otherwise.​

You also have nn selections, the ii-th of which is a set of resistors S_iSi​ such that

\displaystyle S_i = \{\text{the $j$-th resistor} \mid \text{$j$ is a divisor of $i$}\}.Si​={the j-th resistor∣j is a divisor of i}.

Please find a selection in which the resistors form a parallel resistor with the minimum resistance and output the reduced fraction \frac{p}{q}qp​ of its resistance.

Input

The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 100100.

For each test case, the only one line contains an integer nn, where 1 \leq n \leq 10^{100}1≤n≤10100.

Output

For each test case, output a line containing a reduced fraction of the form \text{p/q}p/q indicating the minimum possible resistance, where \text{p}p and \text{q}q should be positive numbers that are coprime.

样例输入复制

3
10
100
1000

样例输出复制

1/2
5/12
35/96

题意:给出一个n,找出从1~n最小的并联电阻。就是找1~n每个数的因子,注意如果因子能被平方数整除,那这个因子就是无穷大,然后根据公式计算并联电阻,找到1~n中并联电阻最小的一个。

题解:因为数据很大所以需要,java大数,详细的过程请看代码:

import java.util.*;
import java.math.*;
public class Main {
	static Scanner cin = new Scanner (System.in);
	static boolean [] vis = new boolean [521];
	static BigInteger [] dp = new BigInteger[521];
	static int [] pr = new int [521];
	static int cnt=0;
	static void getpr() {//欧拉筛    找素数
		vis[0]=vis[1]=true;
		for (int i = 2; i <= 520;i++) {//我找到520,可以少找一点,只要使所有的素数乘起来大于10^100就好
			if(!vis[i]) pr[++cnt]=i;
			for (int j = 1; j <= cnt && i*pr[j] <= 520;j++) {
				vis[i*pr[j]]=true;
				if(i%pr[j]==0) break;
			}
		}
	}
	public static void main(String[] args) {
		getpr();
		int t=cin.nextInt();
		while(t-->0) {
			int k;
			BigInteger sum = cin.nextBigInteger();
			BigInteger ans = BigInteger.ONE;
			dp[0]=BigInteger.ONE;
			for (k = 1;ans.multiply(BigInteger.valueOf(pr[k])).compareTo(sum)<=0;k++) {
				ans=ans.multiply(BigInteger.valueOf(pr[k]));
			}//找规律
			k--;
			for (int i = 1; i <= k;i++) {
				dp[i]=dp[i-1].add(dp[i-1].multiply(BigInteger.valueOf(pr[i])));
			}//找规律
			BigInteger gcd=ans.gcd(dp[k]);
			System.out.println(ans.divide(gcd)+"/"+dp[k].divide(gcd));//直接除以最大公因数
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值