Skip to content

Commit ee5f5bb

Browse files
committed
Comprehensive Test file for online compiling
Successfully compiled at http://www.codechef.com/ide (gcc-4.9.2)
1 parent 0c4c190 commit ee5f5bb

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//Single source file for online compiling
2+
//Successfully compiled at http://www.codechef.com/ide (gcc-4.9.2)
3+
#include <stdio.h>
4+
5+
short arraySum (const short x[], short size){
6+
int total = 0;
7+
short i;
8+
for(i = 0; i < size; ++i){
9+
total += x[i];
10+
}
11+
return total;
12+
};
13+
14+
void printShortArray (const char* x, const short y[], short size){
15+
fputs(x, stdout);
16+
short i;
17+
for(i = 0; i < size; ++i)
18+
printf("%d ", y[i]);
19+
puts("");
20+
};
21+
22+
int factorial(short x){
23+
int y = 1;
24+
while(x > 1){
25+
y *= x;
26+
--x;
27+
}
28+
return y;
29+
};
30+
31+
int arrayFactorial(const short x[], short size, short startingIndex, short minus){ //~7/1-17:00 returns sum of each element's factorial O(N)
32+
short i = startingIndex;
33+
int total = factorial(x[i++] - minus);
34+
while(i < size){
35+
total *= factorial(x[i++]);
36+
}
37+
return total;
38+
};
39+
40+
void promotionSignature (const short freq[], short freqSize, int result[], short resultSize, short startingIndex){
41+
short i, j = 0, k = 0;
42+
while(freq[j] == 0){
43+
++j;
44+
}
45+
46+
short remainingDigits = resultSize - startingIndex;
47+
48+
for(i = 0; i < startingIndex; ++i){
49+
result[i] = -1;
50+
}
51+
for(; i < resultSize; ++i){
52+
result[i] = factorial(remainingDigits) / arrayFactorial(freq, freqSize, j, k);
53+
if(!result[i])
54+
++result[i];
55+
++k;
56+
if(k == freq[j]){
57+
++j;
58+
k = 0;
59+
}
60+
--remainingDigits;
61+
}
62+
};
63+
64+
void steps2state(const short freq[], short freqSize, short result[], short resultSize, short steps){
65+
short sigSize = resultSize - 1;
66+
int sig[sigSize];
67+
short i = 0, j = 0, target = 0;
68+
69+
short copy[freqSize];
70+
for(i = 0; i < freqSize; ++i){
71+
copy[i] = freq[i];
72+
}
73+
74+
--copy[0];
75+
promotionSignature(copy, freqSize, sig, sigSize, 0);
76+
for(i = 0; i < sigSize; ++i){
77+
while( steps >= sig[i]){
78+
steps -= sig[i];
79+
++copy[target];
80+
++target;
81+
while(copy[target] == 0){
82+
++target;
83+
}
84+
--copy[target];
85+
promotionSignature(copy, freqSize, sig, sigSize, i);
86+
}
87+
j = 0;
88+
while(copy[j] == 0){
89+
++j;
90+
}
91+
result[i] = target;
92+
target = j;
93+
--copy[j];
94+
}
95+
result[sigSize] = target;
96+
};
97+
98+
short state2steps(const short freq[], short freqSize, short state[], short stateSize){
99+
100+
short copy[freqSize];
101+
short i = 0;
102+
for(i = 0; i < freqSize; ++i){
103+
copy[i] = freq[i];
104+
}
105+
--copy[0];
106+
107+
short zeroState[stateSize];
108+
steps2state(freq, freqSize, zeroState, stateSize, 0);
109+
110+
short steps = 0;
111+
short sigSize = stateSize - 1;
112+
int sig[sigSize];
113+
promotionSignature(copy, freqSize, sig, sigSize, 0);
114+
115+
for(i = 0; i < sigSize;){
116+
if(state[i] == zeroState[i]){
117+
--copy[zeroState[i + 1]];
118+
++i;
119+
} else {
120+
steps += sig[i];
121+
++copy[zeroState[i]];
122+
++zeroState[i];
123+
while(copy[zeroState[i]] == 0){
124+
++zeroState[i];
125+
}
126+
--copy[zeroState[i]];
127+
promotionSignature(copy, freqSize, sig, sigSize, i);
128+
short k = i, l, m;
129+
for(l = 0; l < freqSize; ++l){
130+
for(m = 0; m < copy[l]; ++m){
131+
zeroState[++k] = l;
132+
}
133+
}
134+
}
135+
}
136+
137+
return steps;
138+
}
139+
140+
int main() {
141+
const short freq[] = {2, 2, 2, 1};
142+
short freqSize = 4;
143+
short stateSize = arraySum(freq, freqSize);
144+
short uBound = factorial(stateSize)/arrayFactorial(freq, freqSize, 0, 0);
145+
short result[stateSize];
146+
short steps, steps2, input;
147+
for(steps = 0;steps < uBound; ++steps){
148+
steps2state(freq, freqSize, result, stateSize, steps);
149+
printShortArray("State: ", result, stateSize);
150+
steps2 = state2steps(freq, freqSize, result, stateSize);
151+
printf("Steps: %d\n", steps2);
152+
}
153+
return 0;
154+
}

0 commit comments

Comments
 (0)