# Codeforces Round #731 (Div. 3) 题解 (DEFG)

## D. Co-growing Sequence

#include <bits/stdc++.h>
#define repeat(i, a, b) for (int i = (a), ib = (b); i < ib; i++)
#define repeat_back(i, a, b) for (int i = (b) - 1, ib = (a);i >= ib; i--)
#define mst(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define int ll
using namespace std;
namespace start {
typedef long long ll; const int inf = INT_MAX >> 1; const ll INF = INT64_MAX >> 1;
ll read() { ll x; if (scanf("%lld", &x) != 1) exit(0); return x; } // will detect EOF
void print(ll x, bool e = 0) { printf("%lld%c", x, " \n"[e]); }
} using namespace start;
void Solve() {
int pre = 0;
repeat (i, 0, n) {
print(pre - (x & pre), i == n - 1);
pre |= x;
}
}
signed main() {
// freopen("data.txt", "r", stdin);
int T = 1; T = read();
repeat (ca, 1, T + 1) {
Solve();
}
return 0;
}


## E. Air Conditioners

#include <bits/stdc++.h>
#define repeat(i, a, b) for (int i = (a), ib = (b); i < ib; i++)
#define repeat_back(i, a, b) for (int i = (b) - 1, ib = (a);i >= ib; i--)
#define mst(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define int ll
using namespace std;
namespace start {
typedef long long ll; const int inf = INT_MAX >> 1; const ll INF = INT64_MAX >> 1;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
ll read() { ll x; if (scanf("%lld", &x) != 1) exit(0); return x; } // will detect EOF
void print(ll x, bool e = 0) { printf("%lld%c", x, " \n"[e]); }
const int N = 300010;
} using namespace start;
int a[N], x[N], t[N];
void Solve() {
fill(a + 1, a + n + 1, inf);
repeat (i, 0, k)
repeat (i, 0, k)
repeat (i, 0, k) {
a[x[i]] = t[i];
}
repeat (i, 2, n + 1) a[i] = min(a[i], a[i - 1] + 1);
repeat_back (i, 1, n) a[i] = min(a[i], a[i + 1] + 1);
repeat (i, 1, n + 1) print(a[i], i == n);
}
signed main() {
// freopen("data.txt", "r", stdin);
int T = 1; T = read();
repeat (ca, 1, T + 1) {
Solve();
}
return 0;
}


## F. Array Stabilization (GCD version)

#include <bits/stdc++.h>
#define repeat(i, a, b) for (int i = (a), ib = (b); i < ib; i++)
#define repeat_back(i, a, b) for (int i = (b) - 1, ib = (a);i >= ib; i--)
#define mst(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define int ll
using namespace std;
namespace start {
typedef long long ll; const int inf = INT_MAX >> 1; const ll INF = INT64_MAX >> 1;
ll read() { ll x; if (scanf("%lld", &x) != 1) exit(0); return x; } // will detect EOF
void print(ll x, bool e = 0) { printf("%lld%c", x, " \n"[e]); }
const int N = 1000010;
} using namespace start;
struct Sieve {
static const int N = 1000010;
bool vis[N]; int lpf[N]; vector<int> prime;
Sieve() {
vis[1] = 1;
repeat (i, 2, N) {
if (!vis[i]) prime.push_back(i), lpf[i] = i;
for (auto j : prime) {
if (i * j >= N) break;
vis[i * j] = 1; lpf[i * j] = j;
if (i % j == 0) break;
}
}
}
} sieve;
int a[N];
bool f[N];
int ans, n;
void calc(vector<int> &v) {
repeat (i, 0, v.size()) v.push_back(v[i] + n);
int cnt = 1;
repeat (i, 1, v.size()) {
if (v[i] == v[i - 1] + 1) cnt++; else cnt = 1;
ans = max(ans, cnt);
}
}
vector<int> rec[N], appear;
void Solve() {
int d = 1;
repeat (i, 0, n) {
d = (i == 0 ? a[i] : __gcd(d, a[i]));
}
repeat (i, 0, n) a[i] /= d;
repeat (i, 0, n) {
while (a[i] != 1) {
int t = sieve.lpf[a[i]];
rec[t].push_back(i);
appear.push_back(t);
while (sieve.lpf[a[i]] == t) a[i] /= t;
}
}
ans = 0;
for (auto i : appear) {
calc(rec[i]);
rec[i].clear();
}
appear.clear();
print(ans, 1);
}
signed main() {
// freopen("data.txt", "r", stdin);
int T = 1; T = read();
repeat (ca, 1, T + 1) {
Solve();
}
return 0;
}


## G. How Many Paths?

#include <bits/stdc++.h>
#define repeat(i, a, b) for (int i = (a), ib = (b); i < ib; i++)
#define repeat_back(i, a, b) for (int i = (b) - 1, ib = (a);i >= ib; i--)
#define mst(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define int ll
using namespace std;
namespace start {
typedef long long ll; const int inf = INT_MAX >> 1; const ll INF = INT64_MAX >> 1;
typedef pair<int, int> pii;
ll read() { ll x; if (scanf("%lld", &x) != 1) exit(0); return x; } // will detect EOF
void print(ll x, bool e = 0) { printf("%lld%c", x, " \n"[e]); }
const int N = 1000010;
} using namespace start;
vector<int> a[N];
stack<int> stk;
bool vis[N], instk[N];
int dfn[N], low[N], co[N], w[N];
vector<int> sz;
int n, dcnt;
void dfs(int x) { // Tarjan
vis[x] = instk[x] = 1; stk.push(x);
dfn[x] = low[x] = ++dcnt;
for(auto p : a[x]) {
if (!vis[p]) dfs(p);
if (instk[p]) low[x] = min(low[x], low[p]);
}
if (low[x] == dfn[x]) {
int t; sz.push_back(0);
do {
t = stk.top();
stk.pop();
instk[t] = 0;
sz.back() += w[x];
co[t] = sz.size() - 1;
} while (t != x);
}
}
void getscc() {
fill(vis, vis + n, 0);
sz.clear();
repeat (i, 0, n) if (!vis[i]) dfs(i);
}
void shrink() { // result: a, n (inplace)
static set<pii> eset;
eset.clear();
getscc();
repeat (i, 0, n)
for (auto p : a[i])
if (co[i] != co[p])
eset.insert({co[i], co[p]});
n = sz.size();
repeat (i, 0, n){
a[i].clear();
w[i] = sz[i];
}
for(auto i : eset){
a[i.fi].push_back(i.se);
// a[i.se].push_back(i.fi);
}
}
int ans[N];
void ddfs(int x) {
vis[x] = 1; ans[x] = 0;
if (x == co[0]) { ans[x] = (w[x] >= 2 ? -1 : 1); return; }
int infty = 0, cnt = 0;
for (auto p : a[x]) {
if (!vis[p]) ddfs(p);
if (ans[p] == -1) infty = 1;
if (ans[p] == 2) cnt = 2;
if (ans[p] == 1) cnt++;
}
if (cnt && w[x] >= 2) infty = 1;
if (infty) ans[x] = -1; else ans[x] = min(cnt, 2ll);
}
void Solve() {
fill (w, w + n, 1);
repeat (i, 0, n) a[i].clear();
repeat (i, 0, m) {
if (x == y) w[x] = 2;
else a[y].push_back(x);
}
shrink();
fill (vis, vis + n, 0);
repeat (i, 0, n) if (!vis[i]) ddfs(i);
repeat (i, 0, n0) print(ans[co[i]], i == n0 - 1);
}
signed main() {
// freopen("data.txt", "r", stdin);
int T = 1; T = read();
repeat (ca, 1, T + 1) {
Solve();
}
return 0;
}

posted @ 2021-07-11 01:48  axiomofchoice  阅读(97)  评论(0编辑  收藏  举报