|
发表于 2023-10-2 16:51:44
|
显示全部楼层
已提交该数列,可以由以下C代码计算。计算积和式目前还没有比较高效的算法。经测试展开后的表达式确实比用循环快(相当于循环展开),但是大于四阶时式子太长,就不再展开了。下面的C代码还不如Mathematica版速度快。- #include <stdio.h>#include <time.h>unsigned __int64 count;int per(int **A, int n){ if (n == 1) return A[0][0]; if (n == 2) return (A[0][0] * A[1][1] + A[0][1] * A[1][0]); if (n == 3) return A[0][0] * (A[1][1] * A[2][2] + A[1][2] * A[2][1]) + A[0][1] * (A[1][0] * A[2][2] + A[1][2] * A[2][0]) + A[0][2] * (A[1][0] * A[2][1] + A[1][1] * A[2][0]); if (n == 4) return A[0][0] * (A[1][1] * (A[2][2] * A[3][3] + A[2][3] * A[3][2]) + A[1][2] * (A[2][1] * A[3][3] + A[2][3] * A[3][1]) + A[1][3] * (A[2][1] * A[3][2] + A[2][2] * A[3][1])) + A[0][1] * (A[1][0] * (A[2][2] * A[3][3] + A[2][3] * A[3][2]) + A[1][2] * (A[2][0] * A[3][3] + A[2][3] * A[3][0]) + A[1][3] * (A[2][0] * A[3][2] + A[2][2] * A[3][0])) + A[0][2] * (A[1][0] * (A[2][1] * A[3][3] + A[2][3] * A[3][1]) + A[1][1] * (A[2][0] * A[3][3] + A[2][3] * A[3][0]) + A[1][3] * (A[2][0] * A[3][1] + A[2][1] * A[3][0])) + A[0][3] * (A[1][0] * (A[2][1] * A[3][2] + A[2][2] * A[3][1]) + A[1][1] * (A[2][0] * A[3][2] + A[2][2] * A[3][0]) + A[1][2] * (A[2][0] * A[3][1] + A[2][1] * A[3][0])); int sum = 0, *m[--n]; for (int i = 0; i < n; i++) m[i] = A[i + 1] + 1; for (int i = 0; i <= n; i++) { sum += (A[i][0] * per(m, n)); if (i == n) break; m[i] = A[i] + 1; } return sum;}int permanent(int *A, int n){ int *m[n]; for (int i = 0; i < n; i++) m[i] = A + (n * i); return per(m, n);}void f(int *a, int x, int n){ for (a[n] = -1; a[n] <= 1; a[n]++) if (n == 0) { if (permanent(a, x) == 0) count++; } else f(a, x, n - 1);}int main(){ clock_t t0 = clock(); for (int i = 1; i <= 4; i++) { count = 0; int a[i*i]; f(a, i, i*i - 1); printf("%llu\n", count); } printf("Elapsed time %0.4fs\n", (clock() - t0) / 1000.0); return 0;}// 1, 33, 7555, 13482049, 186481694371
复制代码 Mathematica代码- n=4;F=Symbol["i"<>IntegerString[#,10,2]]&;perValue=Table[F[i],{i,n^2}]~Partition~n//Permanent;iter=Table[{F[i],-1,1},{i,3,n^2}];fun=Compile[{{i,_Integer}},Module[{count=0},With[{i01=Floor[i/3]-1},{i02=i-1-3Floor[i/3]},Do[If[#==0,count++],##2]];count],RuntimeAttributes->{Listable},CompilationTarget->"C",RuntimeOptions->"Speed"]&[perValue,Sequence@@iter];(len=Total@fun[Range[0,8]])//AbsoluteTiming
复制代码 |
|