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
| ll n, yue; ll mi[30], ri[30]; ll aa[30], bb[30], dd[30]; void ex_gcd(ll a, ll b, ll &xx, ll &yy) { if (b == 0) { xx = 1; yy = 0; yue = a; } else { ex_gcd(b, a%b, xx, yy); ll t = xx; xx = yy; yy = t - (a / b)*yy; } } void solve() { ll i, j, k; ll cur_m = mi[1], cur_r = ri[1]; ll f = 0; repp(i, 2, n) { ll xx, yy; ex_gcd(cur_m, mi[i], xx, yy); ll c = ri[i] - cur_r; if (c % yue != 0) { puts("Creation August is a SB!"); return; } else { xx = ((xx* (c / yue)) % (mi[i] / yue) + (mi[i] / yue)) % (mi[i] / yue); cur_r = xx*cur_m + cur_r; cur_m = mi[i] / yue*cur_m; } } printf("%lld\n", cur_r > 0 ? cur_r : cur_m); } void input() { ll i, j, k; ll x, y; scanf("%lld", &n); repp(i, 1, n) { aa[i] = i; scanf("%lld", &k); bb[k] = i; } memset(dd, 0, sizeof(dd)); for (y = 1, i = n; i >= 1; i--) { x = bb[n - i + 1]; mi[n - i + 1] = i, ri[n - i + 1] = ((aa[x] - aa[y] + 1) % i + i) % i; for (dd[x] = 1, k = 0, j = 1; j <= n; j++) { if (!dd[j]) { aa[j] = ++k; } } y = x; } } int main() { int t; sa(t); while (t--) { input(); solve(); } return 0; }
|