- 注册时间
- 2010-4-21
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 7298
- 在线时间
- 小时
|
发表于 2019-11-7 18:13:56
|
显示全部楼层
本帖最后由 chyanog 于 2019-11-7 19:11 编辑
求解和为20-100的所有解。Mathematica代码,考虑去重,耗时约0.3秒,设置CompilationTarget -> "C"耗时不到0.1秒
- A = {i1, i2, -i1 - i2 + s, i3, i1 + 2 i4 + Mod[s, 2], 3 i1 + i2 + 2 i4 - s + Mod[s, 2], i5, -i1 - i3 + s, 3 i1 + i3 + 2 i4 - s + Mod[s, 2], -2 i1 - 2 i4 + 2 Floor[s/2], i1 - i2 + 2 i4 + i5 + Mod[s, 2], i1 + i2 - i5, i6, i1 - i3 + 2 i4 + i6 + Mod[s, 2], 95 - 2 i1 - 3 i4 - i5 - i6 - 3 s + 3 Floor[s/2], 95 - 5 i1 - i2 - 5 i4 - i6 - 3 s + 5 Floor[s/2], i1 + i3 - i6, 95 - 5 i1 - i3 - 5 i4 - i5 - 3 s + 5 Floor[s/2], -95 + 4 i1 + 5 i4 + i5 + i6 + 4 s - 5 Floor[s/2]};
- F = Symbol["i" <> ToString[#]] &;
- cond = And @@Thread[1 <= Complement[A, {i1, i2, i3, i4, i5, i6}] <= 19] && Unequal @@ A // LogicalExpand;
- iter = Table[{F[i + 1], If[Select[cond, F[i] == Last@Sort[Cases[#, x_Symbol /; x =!= s, -1]] &], Evaluate@If[i < 6, 19, 1], 0]}, {i, 6}];
- cf = Compile[{{i1, _Integer}, {s, _Integer}},
- Module[{B = Internal`Bag[Rest@{0}]}, Do[Internal`StuffBag[B, #, 1], ##2]; Internal`BagPart[B, All]],
- RuntimeAttributes -> {Listable}, RuntimeOptions -> "Speed"(*,CompilationTarget->"C"*)
- ] &[A, Sequence @@ iter];
- func = Sort[Sort /@ Extract[#, List /@ {{1, 2, 3}, {3, 7, 12}, {12, 16, 19}, {19, 18, 17}, {17,13, 8}, {8, 4, 1}}]] &;
- search[s_] := DeleteDuplicatesBy[Join @@ (Partition[#, 19] & /@ cf[Range[19], s]), func];
- ans = Table[{s, search[s]}, {s, 20, 100}]; // AbsoluteTiming
复制代码
画图代码
- plot[A_List] :=
- With[{pts = Append[#, {0, 0}] & /@ Partition[CirclePoints[6], 2, 1, 1]},
- Graphics[{
- Line[pts], MapIndexed[{{EdgeForm[Black], LightBlue, Disk[#, 0.1]}, Style[Text[A[[Tr@#2]], #], 16]} &,
- SortBy[Union @@ (Join[#, Mean /@ Partition[#, 2, 1]] & /@ pts), -N@Last@# &]]
- }, ImageSize -> 300
- ]]
- plot /@ search[22]
复制代码
C代码,没考虑去重,耗时约0.1秒
- #include <stdio.h>
- #include <time.h>
- #define C1 1+i1+i2>s||s>19+i1+i2||i1==i2||2*i1+i2==s||i1+2*i2==s
- #define C2 1+i1+i3>s||s>19+i1+i3||i1==i3||i2==i3||i2==i3||i1+i2+i3==s||i1+i2+i3==s||2*i1+i3==s||i1+2*i3==s
- #define C3 2*i1+i2+i4>19+s||2*i1+i3+i4>19+s||1+s>2*i1+i2+i4||1+s>2*i1+i3+i4||1+i1+i4>s||s>19+i1+i4||i1==i4||i2==i4||i2==i4||i3==i4||i3==i4||i1+i2+i4==s||i1+i2+i4==s||i1+i2+i4==s||i1+i3+i4==s||i1+i3+i4==s||i1+i3+i4==s||2*i1+i4==s||2*i1+i4==s||2*i1+i4==s||2*i1+i2+i4==i3+s||2*i1+i3+i4==i2+s||3*i1+i2+i3+i4==2*s||3*i1+i2+i3+i4==2*s||3*i1+2*i2+i4==2*s||3*i1+2*i3+i4==2*s||i1+2*i4==s||3*i1+i2+2*i4==2*s||3*i1+i3+2*i4==2*s||i2==i3||2*i1+i2==s||2*i1+i3==s
- #define C4 1+i2>i4+i5||i4+i5>19+i2||i1+i2>19+i5||1+i5>i1+i2||i1==i5||i1==i5||i2==i5||i2==i5||i2==i5||i3==i5||i4==i5||2*i1+i2+i4==i5+s||2*i1+i2+i4==i5+s||2*i1+i3+i4==i5+s||i1+i2+i5==s||i1+i3+i5==s||i1+i4+i5==s||i1+i4+i5==s||i1+i4+i5==s||2*i1+i2+i3==i5+s||2*i1+i2+i3==i5+s||i1+i2==i3+i5||i1+i3+i4+i5==i2+s||i1+i3+i4+i5==i2+s||i1+i2==i4+i5||i1+i2==i4+i5||i2+i3==i4+i5||2*(i1+i2)==i5+s||i5+s==2*(i1+i2)||i1+2*i4+i5==i2+s||2*i2==i4+i5||i1+i2==2*i5||i1+2*i2==i4+2*i5||i2==i4
- #define C5 1+i3>i4+i6||i4+i6>19+i3||i1+i3>19+i6||1+i6>i1+i3||i1==i6||i1==i6||i2==i6||i1+i2==i5+i6||i3==i6||i3==i6||i3==i6||i4==i6||2*i1+i2+i4==i6+s||2*i1+i3+i4==i6+s||2*i1+i3+i4==i6+s||i5==i6||i2+i6==i4+i5||i1+i2+i6==s||i1+i3+i6==s||i1+i4+i6==s||i1+i4+i6==s||i1+i4+i6==s||2*i1+i2+i3==i6+s||2*i1+i2+i3==i6+s||i2+i6==i3+i5||i2+i6==i3+i5||i1+i3==i5+i6||i1+i3==i2+i6||i1+i2+i4+i6==i3+s||i1+i2+i4+i6==i3+s||i1+i2+i3==i4+i5+i6||i1+i2+i3==i4+i5+i6||i2+i3==i4+i6||i1+i3==i4+i6||i1+i3==i4+i6||i3+i5==i4+i6||2*(i1+i3)==i6+s||i6+s==2*(i1+i3)||i1+2*i4+i6==i3+s||2*i3==i4+i6||i1+i3==2*i6||i1+2*i3==i4+2*i6||i3==i4
- #define C6 1+2*i1+i2+i4>i5+i7+s||1+2*i1+i3+i4>i6+i7+s||i5+i7+s>19+2*i1+i2+i4||i6+i7+s>19+2*i1+i3+i4||i1+i4>19+i7||1+i7>i1+i4||i1==i7||i1==i7||i2==i7||i1+i2==i5+i7||i1+i2==i5+i7||i1+i2==i5+i7||i3==i7||i1+i3==i6+i7||i1+i3==i6+i7||i1+i3==i6+i7||i4==i7||i4==i7||2*i1+i2+i4==i7+s||2*i1+i2+i4==i7+s||2*i1+i2+i4==i7+s||2*i1+i3+i4==i7+s||2*i1+i3+i4==i7+s||2*i1+i3+i4==i7+s||i5==i7||i6==i7||i2+i7==i4+i5||i2+i7==i4+i5||i1+i2+i7==s||i1+i2+i7==s||i3+i7==i4+i6||i3+i7==i4+i6||i1+i3+i7==s||i1+i3+i7==s||i1+i4+i7==s||2*(i1+i4)==i7+s||2*(i1+i4)==i7+s||i7+s==2*(i1+i4)||2*i1+i2+i3+i4==i5+i7+s||2*i1+i2+i3+i4==i6+i7+s||i1+i4==i2+i7||i1+i4==i3+i7||2*i1+i3+i4+i5==i6+i7+s||2*i1+i2+i4+i6==i5+i7+s||i1+i4==i5+i7||i1+i4==i5+i7||i1+i4==i6+i7||i1+i4==i6+i7||i1+i2+i4==i3+i5+i7||i1+i3+i4==i2+i6+i7||2*i1+2*i2+i4==i5+i7+s||2*i1+2*i3+i4==i6+i7+s||3*i1+i2+i4==i5+i7+s||3*i1+i3+i4==i6+i7+s||3*i1+i2+i3+i4==i5+i6+i7+s||3*i1+i2+i3+i4==i5+i6+i7+s||2*i1+i2+2*i4==i5+i7+s||2*i1+i3+2*i4==i6+i7+s||2*i1+i3+2*i4+i5==i2+i6+i7+s||2*i1+i2+2*i4+i6==i3+i5+i7+s||4*i1+i2+i3+2*i4==i5+i7+2*s||4*i1+i2+i3+2*i4==i6+i7+2*s||i5+i7+2*s==2*(2*i1+i2+i4)||i6+i7+2*s==2*(2*i1+i3+i4)||3*i1+2*i2+i4==2*i5+i7+s||3*i1+2*i3+i4==2*i6+i7+s||3*i1+i2+2*i4==i5+2*i7+s||3*i1+i3+2*i4==i6+2*i7+s||i1+i4==2*i7||2*i1+i2+i4==i5+s||2*i1+i3+i4==i6+s||i2+i6==i3+i5
- int main()
- {
- clock_t t0 = clock();
- int i1, i2, i3, i4, i5, i6, i7, s;
- for (s = 20; s <= 40; s++){
- int cnt = 0;
- for (i1 = 1; i1 <= 19; i1++){
- for (i2 = 1; i2 <= 19; i2++){
- if (C1) continue;
- for (i3 = 1; i3 <= 19; i3++){
- if (C2) continue;
- for (i4 = 1; i4 <= 19; i4++){
- if (C3) continue;
- for (i5 = 1; i5 <= 19; i5++){
- if (C4) continue;
- for (i6 = 1; i6 <= 19; i6++){
- if (C5) continue;
- for (int i7 = 1; i7 <= 19; i7++){
- if (C6) continue;
- cnt++;
- //printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", i1, i2, -i1 - i2 + s, i3, i4, 2 * i1 + i2 + i4 - s, i5, -i1 - i3 + s, 2 * i1 + i3 + i4 - s,-i1 - i4 + s, -i2 + i4 + i5, i1 + i2 - i5, i6, -i3 + i4 + i6, i7,-2 * i1 - i2 - i4 + i5 + i7 + s, i1 + i3 - i6, -2 * i1 - i3 - i4 + i6 + i7 + s, i1 + i4 - i7);
- }
- }
- }
- }
- }
- }
- }
- printf("%d, %d\n", s, cnt);
- }
- printf("Elapsed time %0.4f\n", (clock() - t0) / 1000.0);
- return 0;
- }
复制代码
|
|