鱼C论坛

 找回密码
 立即注册
查看: 3349|回复: 4

题目155:计算电容电路

[复制链接]
发表于 2016-9-1 01:03:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Counting Capacitor Circuits

An electric circuit uses exclusively identical capacitors of the same value C.
The capacitors can be connected in series or in parallel to form sub-units, which can then be connected in series or in parallel with other capacitors or other sub-units to form larger sub-units, and so on up to a final circuit.

Using this simple procedure and up to n identical capacitors, we can make circuits having a range of different total capacitances. For example, using up to n=3 capacitors of 60 F each, we can obtain the following 7 distinct total capacitance values:

p155_capacitors1.gif


If we denote by D(n) the number of distinct total capacitance values we can obtain when using up to n equal-valued capacitors and the simple procedure described above, we have: D(1)=1, D(2)=3, D(3)=7 ...

Find D(18).

Reminder : When connecting capacitors C1, C2 etc in parallel, the total capacitance is CT = C1 + C2 +...,
whereas when connecting them in series, the overall capacitance is given by: p155_capsform.gif


题目:

在电路中,使用大小为 C 的电容若干只。

这些电容能并联或串联地形成子单元,一个子单元又可以和别的电容或子单元,以并联或串联地形成更大的子单元,如此反复进而形成整个电路。

以这个方式来连接 n 个相同的电容,我们能使整个电路拥有不同的电容值,这些电容值会有个大小范围。比如,使用 3 个 60F 的电容,我们可以组成下面七种总电容不同的电路:

p155_capacitors1.gif


如果我们定义 D(n) 为总电容值的个数,那么使用 n 个相同的电容,以不同的方式组成电路的话,我们可以计算出:D(1)=1, D(2)=3, D(3)=7 ...

请给出 D(18) 的值。

提示,电容并联的话,总电容为 CT = C1 + C2 +...,串联的话,则为
p155_capsform.gif

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-2 22:40:56 | 显示全部楼层
这越来越难了吧。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 11:47:53 | 显示全部楼层
本帖最后由 guosl 于 2022-9-21 20:42 编辑
  1. /*
  2. 答案:3857447
  3. */
  4. #include <iostream>
  5. #include <set>
  6. using namespace std;

  7. int gcd(int a, int b)
  8. {
  9.   int c;
  10.   while (a != 0)
  11.   {
  12.     c = a;
  13.     a = b % a;
  14.     b = c;
  15.   }
  16.   return b;
  17. }

  18. struct Rational
  19. {
  20.   int num;
  21.   int den;
  22.   Rational(int n = 0, int d = 1)
  23.   {
  24.     int g = gcd(n, d);
  25.     num = n / g;
  26.     den = d / g;
  27.   }
  28.   bool operator<(const Rational& r) const
  29.   {
  30.     return (num * r.den < r.num* den);
  31.   }
  32. };

  33. const int n = 18;//电容的总数
  34. set<Rational> caps[n + 1];//caps[i]为由i个电容通过各种联法得到的所有电容值
  35. set<Rational> uniq;//合并电容值

  36. int main(void)
  37. {
  38.   caps[1].insert(Rational(1, 1));
  39.   uniq.insert(Rational(1, 1));
  40.   for (int i = 2; i <= n; ++i)
  41.   {
  42.     set<Rational>& c = caps[i];
  43.     for (int j = 1; j <= i / 2; ++j)
  44.     {
  45.       set<Rational>& a = caps[j];
  46.       set<Rational>& b = caps[i - j];
  47.       for (set<Rational>::const_iterator ia = a.begin(); ia != a.end(); ++ia)
  48.       {
  49.         for (set<Rational>::const_iterator ib = b.begin(); ib != b.end(); ++ib)
  50.         {
  51.           int z = ia->num * ib->den + ia->den * ib->num;
  52.           c.insert(Rational(ia->num * ib->num, z));//串联
  53.           c.insert(Rational(z, ia->den * ib->den));//并联
  54.         }
  55.       }
  56.     }
  57.     for (set<Rational>::const_iterator ic = c.begin(); ic != c.end(); ++ic)
  58.       uniq.insert(*ic);
  59.   }
  60.   cout << "D(" << n << ")=" << uniq.size() << endl;
  61.   return 0;
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-26 21:41:43 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-15 22:29 编辑
  1. from math import gcd
  2. def parallel(a,b): #串联
  3. #a=a[0]/a[1];b=b[0]/b[1];return a+b
  4.     a0 = a[1]*b[0]+a[0]*b[1]
  5.     a1 = a[1]*b[1]
  6.     g = gcd(a0, a1)
  7.     return (a0//g, a1//g)

  8. def series(a,b): #并联
  9. #a/c, b/d => c/a+d/b => cb+ad/ab = ab/(cb+ad)
  10. #1/(a[1]/a[0] + b[1]/b[0]) = 1/(a[1]*b[0]+a[0]*b[1] /a[0]*b[0]) = a[0]*b[0] / a[1]*b[0]+a[0]*b[1]
  11.     a0 = a[0]*b[0]
  12.     a1 = a[1]*b[0]+a[0]*b[1]
  13.     g = gcd(a0, a1)
  14.     return (a0//g, a1//g)

  15. print(parallel((60,1),(60,1))) #120
  16. print(series((60,1),(60,1))) #30

  17. maxn = 18
  18. Cn = [set([]) for i in range(19)]
  19. Cn[1] = {(60,1)}
  20. for n in range(2, maxn+1):
  21.     r = set()
  22.     for i in range(1, (n//2) +1):
  23.         for a in Cn[i]:
  24.             for b in Cn[n-i]:
  25.                 r.add(parallel(a,b))
  26.                 r.add(series(a,b))
  27.     Cn[n] = r

  28. r = set()
  29. for c in Cn:
  30.     r = r.union(c)
  31. print(18, len(r)) #3857447
  32. #算法没有优化,花费了大概1分钟。。。
复制代码

点评

我很赞同!: 5.0
我很赞同!: 5
记得加代码格式哦~  发表于 2020-7-15 22:30
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 05:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表