/* swapped == see if swapped also tested */ static void trymove(struct move *mm, int mvnum, int swapped) { int pos; /* position on board */ int rval; /* value of roll */ /* if recursed through all dice values, compare move */ if (mvnum == mm->mvlim) { binsert(mm, bsave(mm)); return; } /* make sure dice in always same order */ if (mm->d0 == swapped) mswap(mm); /* choose value for this move */ rval = mm->dice[mvnum != 0]; /* find all legitimate moves */ for (pos = bar; pos != home; pos += cturn) { /* fix order of dice */ if (mm->d0 == swapped) mswap(mm); /* break if stuck on bar */ if (board[bar] != 0 && pos != bar) break; /* on to next if not occupied */ if (board[pos] * cturn <= 0) continue; /* set up arrays for move */ mm->p[mvnum] = pos; mm->g[mvnum] = pos + rval * cturn; if (mm->g[mvnum] * cturn >= home) { if (*offptr < 0) break; mm->g[mvnum] = home; } /* try to move */ if (makmove(mm, mvnum)) continue; else trymove(mm, mvnum + 1, 2); /* undo move to try another */ backone(mm, mvnum); } /* swap dice and try again */ if ((!swapped) && mm->D0 != mm->D1) trymove(mm, 0, 1); }
int main () { FILE *fin, *fout; fin = fopen ("gcm.in", "rt"); fout = fopen ("gcm.out", "wt"); int a, b, pr, shift; long long n; fscanf (fin, "%d %d", &a, &b); if (a > b) mswap (a, b); pr = (int)floor(sqrt (n = (long long)a * (long long)b)) + 1; shift = gcd (a, b); pr = (pr / shift + 1) * shift; while (pr > a) { if (n % pr == 0 && gcd<int>(pr, n / pr) == shift) { fprintf (fout, "%d %d", mmin<int> (pr, n / pr), mmax<int> (pr, n / pr)); return 0; } pr -= shift; } fprintf (fout, "%d %d", a, b); return 0; }
static /* No measurable speed gain with inlining */ /* ALWAYS_INLINE */ void mvswap(uint32_t* ptr, int32_t zzp1, int32_t zzp2, int32_t zzn) { while (zzn > 0) { mswap(ptr[zzp1], ptr[zzp2]); zzp1++; zzp2++; zzn--; } }
void proll(struct move *mm) { if (mm->d0) mswap(mm); if (cturn == 1) writel("Red's roll: "); else writel("White's roll: "); writec(mm->D0 + '0'); writec('\040'); writec(mm->D1 + '0'); if (tflag) cline(); }
/* * __BT_BPGIN, __BT_BPGOUT -- * Convert host-specific number layout to/from the host-independent * format stored on disk. * * Parameters: * t: tree * pg: page number * h: page to convert */ void __bt_pgin(void *t, pgno_t pg, void *pp) { PAGE *h; indx_t i, top; u_char flags; char *p; if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) return; if (pg == P_META) { mswap(pp); return; } h = pp; M_32_SWAP(h->pgno); M_32_SWAP(h->prevpg); M_32_SWAP(h->nextpg); M_32_SWAP(h->flags); M_16_SWAP(h->lower); M_16_SWAP(h->upper); top = NEXTINDEX(h); if ((h->flags & P_TYPE) == P_BINTERNAL) for (i = 0; i < top; i++) { M_16_SWAP(h->linp[i]); p = (char *)GETBINTERNAL(h, i); P_32_SWAP(p); p += sizeof(u_int32_t); P_32_SWAP(p); p += sizeof(pgno_t); if (*(u_char *)p & P_BIGKEY) { p += sizeof(u_char); P_32_SWAP(p); p += sizeof(pgno_t); P_32_SWAP(p); } } else if ((h->flags & P_TYPE) == P_BLEAF) for (i = 0; i < top; i++) { M_16_SWAP(h->linp[i]); p = (char *)GETBLEAF(h, i); P_32_SWAP(p); p += sizeof(u_int32_t); P_32_SWAP(p); p += sizeof(u_int32_t); flags = *(u_char *)p; if (flags & (P_BIGKEY | P_BIGDATA)) { p += sizeof(u_char); if (flags & P_BIGKEY) { P_32_SWAP(p); p += sizeof(pgno_t); P_32_SWAP(p); } if (flags & P_BIGDATA) { p += sizeof(u_int32_t); P_32_SWAP(p); p += sizeof(pgno_t); P_32_SWAP(p); } } } }
static void fallbackQSort3(uint32_t* fmap, uint32_t* eclass, int32_t loSt, int32_t hiSt) { int32_t unLo, unHi, ltLo, gtHi, n, m; int32_t sp, lo, hi; uint32_t med, r, r3; int32_t stackLo[FALLBACK_QSORT_STACK_SIZE]; int32_t stackHi[FALLBACK_QSORT_STACK_SIZE]; r = 0; sp = 0; fpush(loSt, hiSt); while (sp > 0) { AssertH(sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004); fpop(lo, hi); if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) { fallbackSimpleSort(fmap, eclass, lo, hi); continue; } /* Random partitioning. Median of 3 sometimes fails to * avoid bad cases. Median of 9 seems to help but * looks rather expensive. This too seems to work but * is cheaper. Guidance for the magic constants * 7621 and 32768 is taken from Sedgewick's algorithms * book, chapter 35. */ r = ((r * 7621) + 1) % 32768; r3 = r % 3; if (r3 == 0) med = eclass[fmap[lo]]; else if (r3 == 1) med = eclass[fmap[(lo+hi)>>1]]; else med = eclass[fmap[hi]]; unLo = ltLo = lo; unHi = gtHi = hi; while (1) { while (1) { if (unLo > unHi) break; n = (int32_t)eclass[fmap[unLo]] - (int32_t)med; if (n == 0) { mswap(fmap[unLo], fmap[ltLo]); ltLo++; unLo++; continue; }; if (n > 0) break; unLo++; } while (1) { if (unLo > unHi) break; n = (int32_t)eclass[fmap[unHi]] - (int32_t)med; if (n == 0) { mswap(fmap[unHi], fmap[gtHi]); gtHi--; unHi--; continue; }; if (n < 0) break; unHi--; } if (unLo > unHi) break; mswap(fmap[unLo], fmap[unHi]); unLo++; unHi--; } AssertD(unHi == unLo-1, "fallbackQSort3(2)"); if (gtHi < ltLo) continue; n = mmin(ltLo-lo, unLo-ltLo); mvswap(fmap, lo, unLo-n, n); m = mmin(hi-gtHi, gtHi-unHi); mvswap(fmap, unLo, hi-m+1, m); n = lo + unLo - ltLo - 1; m = hi - (gtHi - unHi) + 1; if (n - lo > hi - m) { fpush(lo, n); fpush(m, hi); } else { fpush(m, hi); fpush(lo, n); } }