2020牛客暑期多校训练营（第四场）

Contest Info

Solved A B C D E F G H I J
5 / 13 Ø O Ø - - O - O - -
• O 在比赛中通过
• Ø 赛后通过
• ! 尝试了但是失败了
• - 没有尝试

Solutions

A. Ancient Distance

Code
// Author : heyuhhh
// Created Time : 2020/07/23 10:09:48
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void err(int x) {cerr << x;}
void err(long long x) {cerr << x;}
void err(double x) {cerr << x;}
void err(char x) {cerr << '"' << x << '"';}
void err(const string &x) {cerr << '"' << x << '"';}
void _print() {cerr << "]\n";}
template<typename T, typename V>
void err(const pair<T, V> &x) {cerr << '{'; err(x.first); cerr << ','; err(x.second); cerr << '}';}
template<typename T>
void err(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), err(i); cerr << "}";}
template <typename T, typename... V>
void _print(T t, V... v) {err(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef Local
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
const int N = 2e5 + 5, M = 20;

int n;
vector<int> G[N];
int f[N][M], deep[N];
int in[N], out[N], mp[N], T;
int ans[N];

void dfs(int u, int fa) {
deep[u] = deep[fa] + 1;
mp[in[u] = ++T] = u;
f[u][0] = fa;
for (int i = 1; i < M; i++) {
f[u][i] = f[f[u][i - 1]][i - 1];
}
for (auto v : G[u]) {
if (v != fa) {
dfs(v, u);
}
}
out[u] = T;
}

int find_kth(int u, int k) {
for (int i = M - 1; i >= 0; i--) {
if (k >> i & 1) {
u = f[u][i];
}
}
if (u == 0) u = 1;
return u;
}

int maxv[N << 2], node[N << 2], cover[N << 2];

void push_up(int o) {
maxv[o] = node[o] = -1;
if (!cover[o << 1]) {
maxv[o] = maxv[o << 1];
node[o] = node[o << 1];
}
if (!cover[o << 1|1] && maxv[o << 1|1] > maxv[o]) {
maxv[o] = maxv[o << 1|1];
node[o] = node[o << 1|1];
}
cover[o] = (cover[o << 1] & cover[o << 1|1]);
}

void build(int o, int l, int r) {
if (l == r) {
maxv[o] = deep[mp[l]];
node[o] = mp[l];
cover[o] = 0;
return;
}
int mid = (l + r) >> 1;
build(o << 1, l, mid);
build(o << 1|1, mid + 1, r);
push_up(o);
}

void update(int o, int l, int r, int L, int R, int v) {
if (L <= l && r <= R) {
cover[o] = v;
return;
}
int mid = (l + r) >> 1;
if (L <= mid) update(o << 1, l, mid, L, R, v);
if (R > mid) update(o << 1|1, mid + 1, r, L, R, v);
push_up(o);
}

void run() {
for (int i = 1; i <= n; i++) {
G[i].clear();
}
T = 0;
for (int i = 1; i < n; i++) {
int x; cin >> x;
G[x].push_back(i + 1);
G[i + 1].push_back(x);
}
for (int i = 1; i <= n; i++) {
ans[i] = n - 1;
}
dfs(1, 0);
build(1, 1, T);
for (int i = n - 1; i >= 0; i--) {
int cost = 0;
vector<int> op;
while (1) {
++cost;
if (maxv[1] - 1 <= i) break;
int u = node[1];
int k = find_kth(u, i);
op.push_back(k);
update(1, 1, T, in[k], out[k], 1);
}
ans[cost] = i;
for (auto it : op) {
update(1, 1, T, in[it], out[it], 0);
}
}
for (int i = 2; i <= n; i++) {
ans[i] = min(ans[i], ans[i - 1]);
}
ll sum = 0;
for (int i = 1; i <= n; i++) {
sum += ans[i];
}
cout << sum << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while (cin >> n) run();
return 0;
}


B. Basic Gcd Problem

Code
// Author : heyuhhh
// Created Time : 2020/07/20 12:10:55
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 5, MOD = 1e9 + 7;
int qpow(ll a, ll b) {
ll res = 1;
while(b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}

int v[N], prime[N];
int num;
void Euler() {
for(int i = 2; i < N; i++) {
if(v[i] == 0) {
v[i] = i;
prime[++num] = i;
}
for(int j = 1; j <= num && prime[j] * i < N; j++) {
v[prime[j] * i] = prime[j] ;
}
}
}

void run() {
int n, c;
cin >> n >> c;
int x = 0;
for (int i = 1; 1ll * prime[i] * prime[i] <= n && i <= num; i++) {
while (n % prime[i] == 0) {
n /= prime[i];
++x;
}
}
if (n > 1) {
++x;
}
int ans = qpow(c, x);
cout << ans << '\n';
}

int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
Euler();
int T; cin >> T; while(T--)
run();
return 0;
}


C. Count New String

Code
// Author : heyuhhh
// Created Time : 2020/07/21 10:14:10
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5, M = 10;

struct SAM {
const static int MAXNODE = N * M * 2;
const static int M = 10;  //
int last, sz, root;

int newnode() {
++sz;
memset(go[sz], 0, sizeof(go[sz]));
return sz;
}

void init() {
sz = 0;
root = last = newnode();
}

int split(int p, int q, int ch) {
int clone = newnode();
memcpy(go[clone], go[q], sizeof(go[q]));
len[clone] = len[p] + 1;
for (int i = p; i && go[i][ch] == q; i = link[i]) {
go[i][ch] = clone;
}
return clone;
}

void insert(int ch) {
if (go[last][ch]) {
int q = go[last][ch];
last = len[last] + 1 == len[q] ? q : split(last, q, ch);
return;
}
// ----
int cur = newnode();
len[cur] = len[last] + 1;
int p = last;
for (; p && !go[p][ch]; p = link[p]) {
go[p][ch] = cur;
}
if (!p) {
} else {
int q = go[p][ch];
link[cur] = len[p] + 1 == len[q] ? q : split(p, q, ch);
}
last = cur;
}

ll solve() {
ll ans = 0;
for (int i = root + 1; i <= sz; ++i) {
}
return ans;
}
}sam;
int sam_id[N * M];
struct Trie {
int ch[N * M][M];
int root, cnt, last;

int newnode() {
memset(ch[++cnt], 0, sizeof(ch[cnt]));
return cnt;
}

void init() {
cnt = 0;
root = newnode();
}

int insert(int last, int x) {
if (!ch[last][x]) {
ch[last][x] = newnode();
}
return ch[last][x];
}

void bfs() {
int p = root;
sam.init();
sam_id[root] = sam.root;
queue <int> q;
q.push(p);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < M; i++) {
if (ch[u][i]) {
sam.last = sam_id[u];
sam.insert(i);
sam_id[ch[u][i]] = sam.last;
q.push(ch[u][i]);
}
}
}
}
}trie;

string s;
int n;
bool valid[N][M];
int trie_id[N][M];

void run() {
cin >> s;
int n = s.length();
for (int i = 0; i < n; i++) {
valid[i][s[i] - 'a'] = true;
if (i > 0) {
for (int j = 0; j < 10; j++) {
if (valid[i - 1][j]) {
valid[i][max(j, s[i] - 'a')] = true;
}
}
}
}
trie.init();
for (int i = 0; i < 10; i++) {
if (valid[n - 1][i]) {
trie_id[n - 1][i] = trie.insert(trie.root, i);
}
}
for (int i = n - 1; i > 0; i--) {
for (int j = 0; j < 10; j++) {
if (valid[i - 1][j]) {
for (int k = 0; k < 10; k++) {
if (valid[i][k] && max(j, s[i] - 'a') == k) {
trie_id[i - 1][j] = trie.insert(trie_id[i][k], j);
}
}
}
}
}
trie.bfs();
ll ans = sam.solve();
cout << ans << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}


F. Finding the Order

Code
// Author : heyuhhh
// Created Time : 2020/07/20 12:28:20
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int a, b, c, d;
vector<int> v(4);
cin >> a >> b >> c >> d;
v[0] = a, v[1] = b, v[2] = c, v[3] = d;
sort(all(v));
if (v[3] != v[2]) {
if (b == v[3] || c == v[3]) {
cout << "AB//CD" << '\n';
} else {
cout << "AB//DC" << '\n';
}
} else {
if (a == d && a == v[3]) {
cout << "AB//DC" << '\n';
} else {
cout << "AB//CD" << '\n';
}
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


H. Harder Gcd Problem

Code
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define LC k<<1
#define RC k<<1|1

typedef long long LL;
const int N=210000;
const int M=1100000;
const LL mod=1e9+7;

int T,n;
int prim[1100000],primm;
bool valid[11000000];
void getprime(int n)
{
memset(valid,1,sizeof(valid));
valid[1]=false;
for(int i=2;i<=n;i++)
{
if(valid[i]) prim[++primm]=i;
for(int j=1;j<=n&&i*prim[j]<=n;j++)
{
valid[i*prim[j]]=false;
if(i%prim[j]==0)break;
}
}
}
int num;
int pp[N];
vector<int> f[N];
vector<pair<int,int> > ans;
int main()
{
getprime(400000);
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
pp[i]=0,f[i].clear();
int l=1;
while (prim[l+1]<=n) l++;
for (int i=l;i>=1;i--)
{
int p=prim[i];
while (p<=n)
{
if (!pp[p]) f[i].pb(p),pp[p]=1;
p+=prim[i];
}
}
ans.clear();
for (int i=l;i>=1;i--)
{
if (f[i].size()==1) continue;
if (f[i].size()%2==0)
{
for (int j=0;j<f[i].size();j+=2)
ans.pb(mp(f[i][j],f[i][j+1]));
}
else
{
ans.pb(mp(f[i][0],f[i][2]));
for (int j=3;j<f[i].size();j+=2)
ans.pb(mp(f[i][j],f[i][j+1]));
if (i!=1) f[1].pb(f[i][1]);
}
}
printf("%d\n",ans.size());
for (int i=0;i<ans.size();i++)
printf("%d %d\n",ans[i].fi,ans[i].se);
}
return 0;
}

posted @ 2020-07-24 22:57  heyuhhh  阅读(160)  评论(0编辑  收藏  举报