-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy path9.cs
158 lines (147 loc) · 4.36 KB
/
9.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// The Computer Language Benchmarks Game
// https://benchmarksgame-team.pages.debian.net/benchmarksgame/
//
// contributed by Flim Nik
// small optimisations by Anthony Lloyd
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
public unsafe static class FannkuchRedux
{
static int taskCount;
static int[] fact, chkSums, maxFlips;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void FirstPermutation(short* p, short* pp, int* count, int n, int idx)
{
for (int i = 0; i < n; ++i) p[i] = (byte)i;
for (int i = n - 1; i > 0; --i)
{
int d = idx / fact[i];
count[i] = d;
if (d > 0)
{
idx %= fact[i];
for (int j = i; j >= 0; --j) pp[j] = p[j];
for (int j = 0; j <= i; ++j) p[j] = pp[(j + d) % (i + 1)];
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void NextPermutation(short* p, int* count)
{
var first = p[1];
p[1] = p[0];
p[0] = first;
int i = 1;
while (++count[i] > i)
{
count[i++] = 0;
var next = p[1];
p[0] = next;
for (int j = 1; j < i;) p[j] = p[++j];
p[i] = first;
first = next;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Copy(short* p, short* pp, int n)
{
var startL = (long*)p;
var stateL = (long*)pp;
var lengthL = n / 4;
int i = 0;
for (; i < lengthL; i++)
{
stateL[i] = startL[i];
}
for (i = lengthL * 4; i < n; i++)
{
pp[i] = p[i];
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int CountFlips(short* p, short* pp, int n)
{
int flips = 1;
int first = *p;
short temp;
if (p[first] != 0)
{
Copy(p, pp, n);
do
{
++flips;
if (first > 2)
{
short* lo = pp + 1, hi = pp + first - 1;
do
{
temp = *lo;
*lo = *hi;
*hi = temp;
} while (++lo < --hi);
}
temp = pp[first];
pp[first] = (short)first;
first = temp;
} while (pp[first] != 0);
}
return flips;
}
static void Run(int n, int taskSize)
{
int* count = stackalloc int[n];
int taskId, chksum = 0, maxflips = 0;
short* p = stackalloc short[n];
short* pp = stackalloc short[n];
while ((taskId = Interlocked.Decrement(ref taskCount)) >= 0)
{
FirstPermutation(p, pp, count, n, taskId * taskSize);
if (*p != 0)
{
var flips = CountFlips(p, pp, n);
chksum += flips;
if (flips > maxflips) maxflips = flips;
}
for (int i = 1; i < taskSize; i++)
{
NextPermutation(p, count);
if (*p != 0)
{
var flips = CountFlips(p, pp, n);
chksum += (1 - (i & 1) * 2) * flips;
if (flips > maxflips) maxflips = flips;
}
}
}
chkSums[-taskId - 1] = chksum;
maxFlips[-taskId - 1] = maxflips;
}
public static void Main(string[] args)
{
int n = args.Length > 0 ? int.Parse(args[0]) : 7;
fact = new int[n + 1];
fact[0] = 1;
for (int i = 1; i < fact.Length; i++)
{
fact[i] = fact[i - 1] * i;
}
var PC = Environment.ProcessorCount;
taskCount = n > 11 ? fact[n] / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2) : PC;
int taskSize = fact[n] / taskCount;
chkSums = new int[PC];
maxFlips = new int[PC];
var threads = new Thread[PC];
for (int i = 1; i < PC; i++)
{
(threads[i] = new Thread(() => Run(n, taskSize))).Start();
}
Run(n, taskSize);
for (int i = 1; i < threads.Length; i++)
{
threads[i].Join();
}
Console.WriteLine($"{chkSums.Sum()}\nPfannkuchen({n}) = {maxFlips.Max()}");
}
}