diff --git a/742a.cpp b/742a.cpp new file mode 100644 index 0000000..68e480c --- /dev/null +++ b/742a.cpp @@ -0,0 +1 @@ +cp \ No newline at end of file diff --git a/757c.cpp b/757c.cpp new file mode 100644 index 0000000..481f73b --- /dev/null +++ b/757c.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll mod = 1e9 + 7; + +ll binpow(ll x, ll n, ll m) { + assert(n >= 0); + + x %= m; // note: m * m must be less than 2^63 to avoid ll overflow + + ll res = 1; + + while (n > 0) { + if (n % 2 == 1) // if n is odd + + res = res * x % m; + + x = x * x % m; + + n /= 2; // divide by two + } + + return res; +} + +void solve() { + ll n, m; + cin >> n >> m; + + ll orsum = 0; + for (ll i = 0; i < m; i++) { + ll x, y, a; + cin >> x >> y >> a; + orsum |= a; + } + + ll ans = 0; + ll i = 0; + while (orsum > 0) { + if (orsum & 1) ans += binpow(2, n - 1 + i, mod); + ans %= mod; + orsum >>= 1; + i++; + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/arena1.cpp b/arena1.cpp new file mode 100644 index 0000000..e8415ac --- /dev/null +++ b/arena1.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + const int mod = 998244353; + + ll n, x; + cin >> n >> x; + + vector power(x + 1, vi(n + 1)); + vector c(n + 1, vi(n + 1)); + + for (int i = 0; i <= x; i += 1) + for (int j = 0; j <= n; j += 1) + power[i][j] = j ? power[i][j - 1] * i % mod : 1; + for (int i = 0; i <= n; i += 1) + for (int j = 0; j <= i; j += 1) + c[i][j] = j ? (c[i - 1][j - 1] + c[i - 1][j]) % mod : 1; + + vector dp(n + 1, vi(x + 1)); + + for (int i = 0; i < x + 1; i++) { + dp[1][i] = x - i + 1; + } + + for (int i = 2; i <= n; i++) { + for (int j = x; j >= 1; j--) { + for (int k = 0; k <= i; k++) { + if (j + i - 1 < x + 1) dp[i][j] += ((dp[k][j + i - 1] * c[i][i - k]) % mod * power[i - 1][i - k]) % mod; + dp[i][j] %= mod; + } + } + } + + cout << (power[x][n] + mod - dp[n][1]) % mod << endl; + + return 0; +} \ No newline at end of file diff --git a/dela.cpp b/dela.cpp new file mode 100644 index 0000000..73770f8 --- /dev/null +++ b/dela.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i]; + } + + ll mx = 0; + ll cnt = 0; + ll sum = 0; + for (ll i = 0; i < n; i++) { + ll cur = a[i]; + while (cur % 2 == 0) cur /= 2, cnt++; + mx = max(mx, cur); + sum += cur; + } + + cout.precision(62); + cout << ((sum - mx) + mx * pow(2, cnt)) << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/delb.cpp b/delb.cpp new file mode 100644 index 0000000..5019ed6 --- /dev/null +++ b/delb.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, q; + cin >> n >> q; + + string s; + cin >> s >> ws; + + ll sum = 0; + + for (ll i = 0; i < n - 2; i++) { + if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') sum++; + } + + for (ll j = 0; j < q; j++) { + ll i; + char c; + cin >> i >> c; + i--; + + ll bsum = 0; + + for (ll k = i - 2; k <= i; k++) { + if (k + 2 >= n || k < 0) continue; + if (s[k] == 'a' && s[k + 1] == 'b' && s[k + 2] == 'c') bsum++; + } + + s[i] = c; + + ll nsum = 0; + + for (ll k = i - 2; k <= i; k++) { + if (k + 2 >= n || k < 0) continue; + if (s[k] == 'a' && s[k + 1] == 'b' && s[k + 2] == 'c') nsum++; + } + + sum += nsum - bsum; + cout << sum << endl; + } + + return 0; +} \ No newline at end of file diff --git a/delc.cpp b/delc.cpp new file mode 100644 index 0000000..65b4c2c --- /dev/null +++ b/delc.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +vb prime(1e6 + 8, true); + +void solve() { + ll n, e; + cin >> n >> e; + + vi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i]; + } + + vb all1(n); + vb works(n); + vi sum(n); + vi sum2(n); + + ll tot = 0; + + for (ll i = 0; i < n; i++) { + ll last = i - e; + + if(last < 0) { + if (a[i] == 1) { + all1[i] = true; + sum[i] = 1; + } else if (prime[a[i]]) { + works[i] = true; + sum[i] = 1; + } + } else { + if(all1[last]) { + if(a[i] == 1) { + all1[i] = true; + sum[i] = sum[last] + 1; + } else if (prime[a[i]]) { + works[i] = true; + sum[i] = sum[last] + 1; + } + } else if(works[last]) { + if(a[i] == 1) { + works[i] = true; + sum[i] = sum[last]; + sum2[i] = sum2[last] + 1; + } + } + + } + if(!works[i] && !all1[i]) { + if (a[i] == 1) { + all1[i] = true; + sum[i] = 1; + } else if (prime[a[i]]) { + works[i] = true; + sum[i] = max((ll)1, sum2[last] + (ll)1); + tot += sum[i] - 1; + } + } else { + if(works[i] && last >= 0) tot += sum[last]; + } + } + + cout << tot << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + prime[0] = prime[1] = false; + for (ll i = 2; i < (1e6+8); i++) { + if (prime[i] && (long long)i * i < (1e6+8)) { + for (ll j = i * i; j < (1e6+8); j += i) + prime[j] = false; + } + } + + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/delcbad.cpp b/delcbad.cpp new file mode 100644 index 0000000..8328a20 --- /dev/null +++ b/delcbad.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +vb prime(1e6 + 8, true); + +void solve() { + ll n, e; + cin >> n >> e; + + vi a(n+1); + for (ll i = 1; i <= n; i++) { + cin >> a[i]; + } + + ll tot = 0; + + for (int i = 1; i <= n; i++) { + for (int k = 1; k <= n; k++) { + if (i + e * k > n) continue; + ll prod = 1; + for (int j = 0; j <= k; j++) { + prod *= a[i + e * j]; + } + + if (prime[prod]) tot++; + } + } + + cout << tot << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + prime[0] = prime[1] = false; + for (int i = 2; i < (1e6 + 8); i++) { + if (prime[i] && (long long)i * i < (1e6 + 8)) { + for (int j = i * i; j < (1e6 + 8); j += i) + prime[j] = false; + } + } + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/delcbetter.cpp b/delcbetter.cpp new file mode 100644 index 0000000..00c89dd --- /dev/null +++ b/delcbetter.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +vb prime(1e6 + 8, true); + +void solve() { + ll n, e; + cin >> n >> e; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + ll ans = 0; + for (ll i = 0; i < n; i++) { + if (!prime[a[i]]) continue; + ll l, r; + l = r = i; + ll cnt0, cnt1; + cnt0 = cnt1 = 0; + while (l >= 0 && (a[l] == 1 || l == i)) { + cnt0++; + l -= e; + } + while (r < n && (a[r] == 1 || r == i)) { + cnt1++; + r += e; + } + ans += cnt0 * cnt1 - 1; + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + prime[0] = prime[1] = false; + for (ll i = 2; i < (1e6 + 8); i++) { + if (prime[i] && (long long)i * i < (1e6 + 8)) { + for (ll j = i * i; j < (1e6 + 8); j += i) + prime[j] = false; + } + } + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/deld.cpp b/deld.cpp new file mode 100644 index 0000000..c07a0b5 --- /dev/null +++ b/deld.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +struct DSU { + vi par; + vi sz; + + void init(ll n) { + for (ll i = 0; i < n; i++) { + par.push_back(i); + sz.push_back(1); + } + } + ll get_rep(ll node) { + if (par[node] == node) return node; + return get_rep(par[node]); + } + bool check_con(ll a, ll b) { + return get_rep(a) == get_rep(b); + } + void unite(ll a, ll b) { + ll x = get_rep(a); + ll y = get_rep(b); + if (x == y) return; + if (sz[x] < sz[y]) { + par[x] = y; + sz[y] += sz[x]; + } else { + par[y] = x; + sz[x] += sz[y]; + } + } +}; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, d; + cin >> n >> d; + + DSU dsu; + dsu.init(n); + + ll u = 0; + for (ll i = 0; i < d; i++) { + ll x, y; + cin >> x >> y; + x--, y--; + + if(dsu.check_con(x, y)) { + u++; + } else { + dsu.unite(x, y); + } + + multiset> szs; + + for (ll j = 0; j < n; j++) { + if (dsu.get_rep(j) == j) szs.insert(dsu.sz[j]); + } + + ll k = u + 1; + ll tot = 0; + + for(ll j : szs) { + if (k == 0) break; + tot += j; + k--; + } + + cout << tot - 1 << endl; + } + + return 0; +} \ No newline at end of file diff --git a/gen.cpp b/gen.cpp new file mode 100644 index 0000000..c68fd93 --- /dev/null +++ b/gen.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; +int rnd(int a, int b) { + return a + rand() % (b - a + 1); +} +int main() { + srand(time(NULL)); + + int a = rnd(1, 10); + int b = rnd(1, 10); + cout << a << " " << b << endl; +} \ No newline at end of file diff --git a/messages.cpp b/messages.cpp new file mode 100644 index 0000000..35b5436 --- /dev/null +++ b/messages.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll N = 2e5 + 8; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi m(n); + vi k(n); + for (ll i = 0; i < n; i++) { + cin >> m[i] >> k[i]; + } + + ll bestt = 1; + ll bestans = 0; + vi bestpath; + + for (ll t = 1; t <= 20; t++) { + vpi coeff(N); + for (ll i = 0; i < N; i++) { + coeff[i] = {0, i}; + } + for (ll i = 0; i < n; i++) { + coeff[m[i]].first += min(t, k[i]); + } + + sort(coeff.rbegin(), coeff.rend()); + + ll cans = 0; + vi path; + for (ll i = 0; i < t; i++) { + cans += coeff[i].first; + path.push_back(coeff[i].second); + } + + if (cans * bestt > bestans * t) { + bestans = cans; + bestt = t; + bestpath = path; + } + } + + cout << bestt << endl; + + for (ll i = 0; i < bestt; i++) { + cout << bestpath[i] << " "; + } + + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/mmoo.cpp b/mmoo.cpp new file mode 100644 index 0000000..c57a28e --- /dev/null +++ b/mmoo.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vector a(n, vi(n)); + for (int i = 0; i < n; i++) { + for(int j = 0; j < n; j++) { + cin >> a[i][j]; + } + } + + vector> visited(n, vector(n, 0)); + + + function ff = [&](ll x, ll y) { + if (x < 0 || x >= n || y < 0 || y >= n) return; + if (visited[x][y]) return; + + visited[x][y] = true; + + ff(x + 1, y); + ff(x - 1, y); + ff(x, y + 1); + ff(x, y - 1); + } + + + + return 0; +} \ No newline at end of file diff --git a/nekoc.cpp b/nekoc.cpp new file mode 100644 index 0000000..763b356 --- /dev/null +++ b/nekoc.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll INF = 1e9; + +long long gcd(ll a, ll b) { + if (b == 0) + return a; + return gcd(b, a % b); +} + +long long lcm(ll a, ll b) { + return (a / gcd(a, b)) * b; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll a, b; + cin >> a >> b; + + if (a > b) swap(a, b); + ll d = b - a; + ll best = lcm(a, b); + ll bestk = 0; + + for (ll i = 1; i * i <= d; i++) { + if (d % i != 0) continue; + for (ll j = 0; j < 2; j++) { + ll k = i - (a % i); + ll l = lcm(a + k, b + k); + if (l < best) best = l, bestk = k; + if (l == best && k < bestk) bestk = k; + if (i * i == d) break; + i = d / i; + } + } + + cout << bestk << endl; + + return 0; +} \ No newline at end of file diff --git a/nekocbad.cpp b/nekocbad.cpp new file mode 100644 index 0000000..f1b0129 --- /dev/null +++ b/nekocbad.cpp @@ -0,0 +1,85 @@ +// C++ program for the above approach + +#include "bits/stdc++.h" +using namespace std; + +// Function for finding all divisors +// of a given number +vector getDivisors(int diff) { + // Stores the divisors of the + // number diff + vector divisor; + + for (int i = 1; i * i <= diff; i++) { + // If diff is a perfect square + if (i == diff / i) { + divisor.push_back(i); + continue; + } + + // If i divides diff then + // diff / i also a divisor + if (diff % i == 0) { + divisor.push_back(i); + divisor.push_back(diff / i); + } + } + + // Return the divisors stored + return divisor; +} + +// Function to find smallest integer x +// such that LCM of (A + X) and (B + X) +// is minimized +int findTheSmallestX(int a, int b) { + int diff = b - a; + + // Find all the divisors of diff + vector divisor = getDivisors(diff); + + // Find LCM of a and b + int lcm = (a * b) / __gcd(a, b); + + int ans = 0; + + for (int i = 0; + i < (int)divisor.size(); i++) { + // From equation x = M - a % M + // here M = divisor[i] + int x = (divisor[i] - (a % divisor[i])); + + // If already checked for x == 0 + if (!x) + continue; + + // Find the product + int product = (b + x) * (a + x); + + // Find the GCD + int tempGCD = __gcd(a + x, b + x); + int tempLCM = product / tempGCD; + + // If current lcm is minimum + // than previous lcm, update ans + if (lcm > tempLCM) { + ans = x; + lcm = tempLCM; + } + } + + // Print the number added + cout << ans << endl; +} + +// Driver Code +int main() { + // Given A & B + int A, B; + cin >> A >> B; + + // Function Call + findTheSmallestX(A, B); + + return 0; +} diff --git a/nekocbadf.cpp b/nekocbadf.cpp new file mode 100644 index 0000000..8c1d31d --- /dev/null +++ b/nekocbadf.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const int INF = 1e9; + +// Recursive function to return gcd of a and b +long long gcd(long long int a, long long int b) { + if (b == 0) + return a; + return gcd(b, a % b); +} + +// Function to return LCM of two numbers +long long lcm(int a, int b) { + return (a / gcd(a, b)) * b; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll a, b; + cin >> a >> b; + + int best = INF; + int bestk = -1; + cout << lcm(a, b) - max(a, b) << endl; + for (int k = 0; k < lcm(a, b) - max(a, b); k++) { + if (lcm(a + k, b + k) < best) best = lcm(a + k, b + k), bestk = k; + cout << k << " " << a + k << " " << b + k << " " << lcm(a + k, b + k) << endl; + } + + cout << best << " " << bestk << endl; + + return 0; +} \ No newline at end of file diff --git a/nql.cpp b/nql.cpp new file mode 100644 index 0000000..49bd1ab --- /dev/null +++ b/nql.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll mod = 1e9+7; +ll binpow(ll x, ll n, ll m) { + assert(n >= 0); + + x %= m; // note: m * m must be less than 2^63 to avoid ll overflow + + ll res = 1; + + while (n > 0) { + if (n % 2 == 1) // if n is odd + + res = res * x % m; + + x = x * x % m; + + n /= 2; // divide by two + } + + return res; +} +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi a(n); + ll esum = 0; + + vi cnt(n); + + for (ll i = 0; i < n; i++) { + cin >> a[i]; + if (a[i] % 2 == 0) esum++; + while (a[i] % 2 == 0) a[i] /= 2, cnt[i]++; + } + + vi r(31); + for (ll i = 0; i < n; i++) { + r[cnt[i]]++; + } + + ll ans = 0; + + ans += binpow(2, n, mod); + ans %= mod; + ans -= binpow(2, esum, mod); + ans %= mod; + ll sum = 1; + + for (ll i = 30; i >= 1; i--) { + if(r[i] > 1) { + ans += (sum * (binpow(2, r[i] - 1, mod) - 1)); + } + ans %= mod; + + sum *= binpow(2, r[i], mod); + sum %= mod; + } + + ans %= mod; + + cout << ans << endl; + + return 0; +} \ No newline at end of file diff --git a/painta.cpp b/painta.cpp new file mode 100644 index 0000000..56fc325 --- /dev/null +++ b/painta.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll INF = 1e9; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + a[i]--; + } + + vi last(n, INF); + vi nxt(n, INF); + for (ll i = n - 1; i >= 0; i--) { + nxt[i] = last[a[i]]; + last[a[i]] = i; + } + + if (n < 2) { + cout << 1 << endl; + return 0; + } + + ll l = 0; + ll r = 1; + + if (a[l] == a[r]) l = r; + + ll ans = 2; + + for (ll i = 2; i < n; i++) { + if (a[i] == a[l] && a[i] == a[r]) { + l = r = i; + } else if (a[i] == a[l] || a[i] == a[r]) { + ans++; + l = r = i; + } else { + if (nxt[l] > nxt[r]) { + assert(l < i && nxt[l] > i); + r = i; + } else { + assert(r < i && nxt[r] > i); + l = i; + } + ans++; + } + } + + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/paintabad.cpp b/paintabad.cpp new file mode 100644 index 0000000..d7b6307 --- /dev/null +++ b/paintabad.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +const int MAXN = 100010; + +int n; +int a[MAXN]; +int pos[MAXN], nxt[MAXN]; + +void solve() { + scanf("%d", &n); + for (int i = 1; i <= n; ++i) + scanf("%d", &a[i]); + for (int i = 0; i <= n; ++i) + pos[i] = n + 1; + for (int i = n; i >= 0; --i) { + nxt[i] = pos[a[i]]; + pos[a[i]] = i; + } + int x = 0, y = 0; // the last elements of the two subarrays + int res = 0; + for (int z = 1; z <= n; ++z) { + // Greedy Strategy I + if (a[x] == a[z]) { + res += a[y] != a[z]; + y = z; + } else if (a[y] == a[z]) { + res += a[x] != a[z]; + x = z; + } + // Greedy Strategy II + else if (nxt[x] < nxt[y]) { + res += a[x] != a[z]; + x = z; + } else { + res += a[y] != a[z]; + y = z; + } + } + printf("%d\n", res); +} + +int main() { + solve(); + return 0; +} \ No newline at end of file diff --git a/paintb.cpp b/paintb.cpp new file mode 100644 index 0000000..7439dce --- /dev/null +++ b/paintb.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll m; + cin >> m; + + vi a(m); + for(ll i = 0; i < m; i++) { + cin >> a[i]; + } + + auto it = unique(a.begin(), a.end()); + ll n = distance(a.begin(), it); + a.resize(n); + + set ac; + + ll ans = 0; + for (ll i = 0; i < n; i++) { + if (i < 2) ac.insert(a[i]); + } + + if(ac.size() < 2) { + cout << 1 << endl; + return 0; + } + + ans = 2; + + for (ll i = 2; i < n; i++) { + if(ac.find(a[i]) != ac.end()) { + ac.clear(); + ac.insert(a[i - 1]); + } else + ans++; + ac.insert(a[i]); + } + + cout << ans << endl; + + return 0; +} \ No newline at end of file diff --git a/playlist.cpp b/playlist.cpp new file mode 100644 index 0000000..e762934 --- /dev/null +++ b/playlist.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +ll gcd(ll a, ll b) { + if (b == 0) + return a; + return gcd(b, a % b); +} + +void solve() { + ll n; + cin >> n; + + vi a(n); + + priority_queue pq; + set s; + + for (ll i = 0; i < n; i++) { + cin >> a[i]; + s.insert(i); + } + + for (ll i = 0; i < n; i++) { + if (gcd(a[i], a[(i + 1) % n]) == 1) pq.push({-i, i}); + } + + vi path; + + while (!pq.empty()) { + if (s.size() <= 1) { + path.push_back(*s.begin() + 1); + break; + } + pi top = pq.top(); + pq.pop(); + + ll v = a[top.second]; + + auto it = next(s.find(top.second)); + if (it == s.end()) it = s.begin(); + if (!pq.empty()) { + pi nxt = pq.top(); + if (nxt.second == *it) pq.pop(); + } + auto it2 = it; + it = next(it); + if (it == s.end()) it = s.begin(); + if (gcd(v, a[*it]) == 1) pq.push({top.first - n, top.second}); + path.push_back(*it2 + 1); + s.erase(it2); + } + + cout << path.size() << " "; + + for (ll i : path) { + cout << i << " "; + } + + cout << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/reconperm.cpp b/reconperm.cpp new file mode 100644 index 0000000..6a6db04 --- /dev/null +++ b/reconperm.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +using ll = int; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +class ReconstructPermutation { + public: + vector reconstruct(int n, vector partial) { + set left; + for(int i = 0; i < n; i++) { + left.insert(i); + } + + for(int i : partial) { + left.erase(i); + } + + int j = 0; + int k = 0; + vi path(n, -1); + for (int i : left) { + cout << i << endl; + if (j == partial.size() || i < partial[j]) { + path[k++] = i; + continue; + } + while(!(i < partial[j])) { + path[k++] = partial[j++]; + if (j == partial.size()) break; + } + } + + return path; + } +}; \ No newline at end of file diff --git a/struct.cpp b/struct.cpp index 047e177..5d229b0 100644 --- a/struct.cpp +++ b/struct.cpp @@ -35,5 +35,4 @@ int main() { a.x; return 0; -} - +} \ No newline at end of file