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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
#define MOST_NUM 51 #define MAX_VALUE(x, y) ((x) > (y) ? (x) : (y))
static const int gPrimeSize[MOST_NUM] = {0,50,25,34,25,40,17,43,25,34,20,46,17,47,21,27,25,48,17,48,20,29,23,48,17,40,23,34,21,49,14,49,25,31,24,34,17,49,24,32,20,49,14,49,23,27,24,49,17,43,20}; static const int gPrime[MOST_NUM][MOST_NUM] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,0}, {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35,37,38,40,41,43,44,46,47,49,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,29,31,32,33,34,36,37,38,39,41,42,43,44,46,47,48,49,0,0,0,0,0,0,0,0,0,0,0}, {1,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,8,9,10,11,12,13,15,16,17,18,19,20,22,23,24,25,26,27,29,30,31,32,33,34,36,37,38,39,40,41,43,44,45,46,47,48,50,0,0,0,0,0,0,0,0}, {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35,37,38,40,41,43,44,46,47,49,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,7,9,11,13,17,19,21,23,27,29,31,33,37,39,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,31,32,34,35,36,37,38,39,40,41,42,43,45,46,47,48,49,50,0,0,0,0,0}, {1,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,27,28,29,30,31,32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,48,49,50,0,0,0,0}, {1,3,5,9,11,13,15,17,19,23,25,27,29,31,33,37,39,41,43,45,47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,7,8,11,13,14,16,17,19,22,23,26,28,29,31,32,34,37,38,41,43,44,46,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,0,0,0}, {1,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,39,40,41,42,43,44,45,46,47,48,49,50,0,0,0}, {1,3,7,9,11,13,17,19,21,23,27,29,31,33,37,39,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,5,8,10,11,13,16,17,19,20,22,23,25,26,29,31,32,34,37,38,40,41,43,44,46,47,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,7,9,13,15,17,19,21,23,25,27,29,31,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,47,48,49,50,0,0,0}, {1,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,29,31,32,33,34,36,37,38,39,41,42,43,44,46,47,48,49,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,7,9,11,15,17,19,21,23,25,27,29,31,33,35,37,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35,37,38,40,41,43,44,46,47,49,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,9,11,13,15,17,19,23,25,27,29,31,33,37,39,41,43,45,47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,0,0}, {1,7,11,13,17,19,23,29,31,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,0,0}, {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,5,7,8,10,13,14,16,17,19,20,23,25,26,28,29,31,32,34,35,37,38,40,41,43,46,47,49,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,7,9,11,13,15,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,6,8,9,11,12,13,16,17,18,19,22,23,24,26,27,29,31,32,33,34,36,37,38,39,41,43,44,46,47,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,38,39,40,41,42,43,44,45,46,47,48,49,50,0,0}, {1,3,5,7,9,11,13,15,17,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,5,7,8,10,11,14,16,17,19,20,22,23,25,28,29,31,32,34,35,37,38,40,41,43,44,46,47,49,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,7,9,11,13,17,19,21,23,27,29,31,33,37,39,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,42,43,44,45,46,47,48,49,50,0,0}, {1,5,11,13,17,19,23,25,29,31,37,41,43,47,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,44,45,46,47,48,49,50,0,0}, {1,3,5,7,9,13,15,17,19,21,23,25,27,29,31,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,4,7,8,11,13,14,16,17,19,22,23,26,28,29,31,32,34,37,38,41,43,44,46,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,3,5,7,9,11,13,15,17,19,21,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {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,48,49,50,0,0}, {1,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,2,3,4,5,6,8,9,10,11,12,13,15,16,17,18,19,20,22,23,24,25,26,27,29,30,31,32,33,34,36,37,38,39,40,41,43,44,45,46,47,48,50,0,0,0,0,0,0,0,0}, {1,3,7,9,11,13,17,19,21,23,27,29,31,33,37,39,41,43,47,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
struct LinkedNode { int index; struct LinkedNode *next; };
struct BinodeQueue { int *arr; int arrHead; int arrSize; };
struct SoloStack { int *arr; int arrSize; };
static void queuePush(struct BinodeQueue *queue, int value); static void queuePop(struct BinodeQueue *queue); static void stackPush(struct SoloStack *stack, int value); static void stackPop(struct SoloStack *stack); static void recursionCalc(int *nums, int *layer, int i, struct LinkedNode **children, struct SoloStack *stack, int *result);
int *getCoprimes(int *nums, int numsSize, int **edges, int edgesSize, int *edgesColSize, int *returnSize) { const int n = numsSize, buffSize = MAX_VALUE(edgesSize << 1, 1); int x = 0, y = 0; int layer[n], arr[buffSize], counter[MOST_NUM]; struct BinodeQueue queue; struct LinkedNode *node = NULL; struct LinkedNode buff[buffSize], *children[n]; struct SoloStack stack[MOST_NUM]; int *result = NULL; queue.arr = arr; queue.arrHead = 0; queue.arrSize = 0; memset(layer, -1, sizeof(layer)); memset(counter, 0, sizeof(counter)); memset(children, 0, sizeof(children)); result = (int *)malloc(sizeof(int) * n); if(NULL == result) { *returnSize = 0; return result; } memset(result, -1, sizeof(int) * n);
for(x = 0; edgesSize > x; x++) { node = &buff[y]; y++; node->index = edges[x][1]; node->next = children[edges[x][0]]; children[edges[x][0]] = node; node = &buff[y]; y++; node->index = edges[x][0]; node->next = children[edges[x][1]]; children[edges[x][1]] = node; }
layer[0] = 0; queuePush(&queue, 0); while(0 < queue.arrSize) { x = queue.arr[queue.arrHead]; queuePop(&queue); counter[nums[x]]++; for(node = children[x]; NULL != node; node = node->next) {
if(-1 == layer[node->index]) { layer[node->index] = layer[x] + 1; queuePush(&queue, node->index); } } }
y = 0; for(x = 0; MOST_NUM > x; x++) { if(0 < counter[x]) { stack[x].arr = &arr[y]; y += counter[x]; stack[x].arrSize = 0; } else { stack[x].arrSize = 0; } } recursionCalc(nums, layer, 0, children, stack, result); *returnSize = n; return result; }
static void queuePush(struct BinodeQueue *queue, int value) { queue->arr[queue->arrHead + queue->arrSize] = value; queue->arrSize++; return; }
static void queuePop(struct BinodeQueue *queue) { queue->arrHead++; queue->arrSize--; return; }
static void stackPush(struct SoloStack *stack, int value) { stack->arr[stack->arrSize] = value; stack->arrSize++; return; }
static void stackPop(struct SoloStack *stack) { stack->arrSize--; return; }
static void recursionCalc(int *nums, int *layer, int i, struct LinkedNode **children, struct SoloStack *stack, int *result) { int x = 0, y = 0, deepest = -1; struct LinkedNode *node = NULL; for(x = 0; gPrimeSize[nums[i]] > x; x++) { y = gPrime[nums[i]][x]; if(0 < stack[y].arrSize && deepest < layer[stack[y].arr[stack[y].arrSize - 1]]) { deepest = layer[stack[y].arr[stack[y].arrSize - 1]]; result[i] = stack[y].arr[stack[y].arrSize - 1]; } } stackPush(&stack[nums[i]], i); for(node = children[i]; NULL != node; node = node->next) { if(layer[i] < layer[node->index]) { recursionCalc(nums, layer, node->index, children, stack, result); } } stackPop(&stack[nums[i]]); return; }
|