## ComWin’ round 11部分题解

https://vjudge.net/contest/325913#overview

A.Threehouses

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1000 + 10;

int fa[maxn];
double x[maxn], y[maxn], dist[maxn][maxn];
int tot;
struct node {
int u, v;
double val;
bool operator < (const node &rhs) const {
return val < rhs.val;
}
}edge[maxn * maxn];

int fr(int x) {
if(fa[x] == x) return x;
return fa[x] = fr(fa[x]);
}
void uni(int x, int y) {
x = fr(x), y = fr(y);
if(x != y) fa[x] = y;
}
void init(int n) {
tot = 0;
for(int i = 0; i <= n; i++) {
fa[i] = i;
}
}
double cal(int i, int j) {
double ret = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
return ret;
}
int main() {
int n, e, p;
scanf("%d%d%d", &n, &e, &p);
init(n + 1);
for(int i = 1; i <= n; i++) {
scanf("%lf%lf", &x[i], &y[i]);
}
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
dist[i][j] = dist[j][i] = cal(i, j);
node now;
now.u = i, now.v = j, now.val = dist[i][j];
edge[tot++] = now;
}
}
sort(edge, edge + tot);
for(int i = 1; i <= e; i++) {
uni(0, i);
}
double ans = 0.0;
for(int i = 0; i < p; i++) {
int u, v;
scanf("%d%d", &u, &v);
uni(u, v);
}
for(int i = 0; i < tot; i++) {
node now = edge[i];
int fu = fr(now.u), fv = fr(now.v);
if(fu != fv) {
ans += now.val;
fa[fu] = fv;
}
}
printf("%.6f\n", ans);
return 0;
}

C.Cops ans Robbers

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 1000 + 5;
const int inf = 0x3f3f3f3f;
const LL linf = 0x3f3f3f3f3f3f3f3f;

int n, m, c, T;
LL val[26];
char mp[N][N];
struct Dinic {
static const int maxn = 1e6 + 5;
static const int maxm = 4e6 + 5;

struct Edge {
int u, v, next;
LL flow, cap;
} edge[maxm];

void addedge(int u, int v, LL cap) {
}

void init() {
eg = 0;
}

bool makeLevel(int s, int t, int n) {
for(int i = 0; i < n; i++) level[i] = 0, cur[i] = head[i];
queue<int> q; q.push(s);
level[s] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = edge[i].next) {
Edge &e = edge[i];
if(e.flow < e.cap && level[e.v] == 0) {
level[e.v] = level[u] + 1;
if(e.v == t) return 1;
q.push(e.v);
}
}
}
return 0;
}

LL findpath(int s, int t, LL limit = linf) {
if(s == t || limit == 0) return limit;
for(int i = cur[s]; ~i; i = edge[i].next) {
cur[edge[i].u] = i;
Edge &e = edge[i], &rev = edge[i^1];
if(e.flow < e.cap && level[e.v] == level[s] + 1) {
LL flow = findpath(e.v, t, min(limit, e.cap - e.flow));
if(flow > 0) {
e.flow += flow;
rev.flow -= flow;
return flow;
}
}
}
return 0;
}

LL max_flow(int s, int t, int n) {
LL res = 0;
while(makeLevel(s, t, n)) {
LL flow;
while((flow = findpath(s, t)) > 0) {
if(res >= linf) return res;
res += flow;
}
}
return res;
}
} di;

int id(int r, int c) {
if(r < 1 || r > n || c < 1 || c > m) return T;
return (r - 1) * m + c;
}

LL getVal(int r, int c) {
if(r < 1 || r > n || c < 1 || c > m) return linf;
if(mp[r][c] == '.' || mp[r][c] == 'B') return linf;
else return val[mp[r][c] - 'a'];
}

int main() {
scanf("%d%d%d", &m, &n, &c);
for(int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
for(int i = 0; i < c; i++) scanf("%lld", &val[i]);
T = 2*n*m + 5;
di.init();
di.addedge(id(1, 1) + n*m, T, linf);
di.addedge(id(1, m) + n*m, T, linf);
di.addedge(id(n, 1) + n*m, T, linf);
di.addedge(id(n, m) + n*m, T, linf);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
di.addedge(id(i, j), id(i, j) + n*m, getVal(i, j));
if((i == 1 || i == n) && (j == 1 || j == m)) continue;
di.addedge(id(i, j) + n*m, id(i+1, j), linf);
di.addedge(id(i, j) + n*m, id(i-1, j), linf);
di.addedge(id(i, j) + n*m, id(i, j+1), linf);
di.addedge(id(i, j) + n*m, id(i, j-1), linf);
}
}
LL ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(mp[i][j] == 'B') {
ans = di.max_flow(id(i, j), T, T+10);
}
if(ans) break;
}
if(ans) break;
}
printf("%lld\n", ans >= linf ? -1 : ans);
return 0;
}
/*
10 10 1
..........
..........
..........
...a.a....
..a.a.a...
.a..B.a...
..aaaaa...
..........
..........
..........
1
*/

E.Coprime Integers

$\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)=1]$

$=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{t|i,t|j}u(t)$

$=\sum_{t=1}^{min(n,m)}u(t)\sum_{i=1}^{\left \lfloor \frac{n}{t} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{t} \right \rfloor}1$

$=\sum_{t=1}^{min(n,m)}u(t)\left \lfloor \frac{n}{t} \right \rfloor\left \lfloor \frac{m}{t} \right \rfloor$

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 1e7 + 5;

int p[N / 10], cnt, mu[N];
bool tag[N];

void getPrime() {
mu[1] = 1;
for(int i = 2; i < N; i++) {
if(!tag[i]) {
mu[i] = -1;
p[cnt++] = i;
}
for(int j = 0; j < cnt && 1LL * p[j] * i < N; j++) {
tag[i * p[j]] = 1;
if(i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
}
mu[i * p[j]] = -mu[i];
}
}
for(int i = 1; i < N; i++) mu[i] += mu[i - 1];
}

LL solve(LL n, LL m) {
LL res = 0;
for(LL l = 1, r; l <= min(n, m); l = r + 1) {
r = min(n / (n / l), m / (m / l));
res += (mu[r] - mu[l - 1]) * (n / l) * (m / l);
}
return res;
}

int main() {
getPrime();
LL a, b, c, d;
scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
printf("%lld\n", solve(b, d) - solve(a - 1, d) - solve(c - 1, b) + solve(a - 1, c - 1));
return 0;
}

H.Heir's Dilemma

#include <bits/stdc++.h>

using namespace std;

bool ck(int x) {
bool vis[10];
for(int i = 0; i < 10; i++) vis[i] = false;
int xx = x, xxx = x, xxxx = x;
while(xxx) {
if(xxx % 10 == 0) return false;
xxx /= 10;
}
while(xxxx) {
if(vis[xxxx % 10] == true) return false;
vis[xxxx % 10] = true;
xxxx /= 10;
}
while(xx) {
int now = xx % 10;
if(x % now != 0) return false;
xx /= 10;
}
return true;
}

int main() {
int cnt = 0, a, b;
cin >> a >> b;
for(int i = a; i <= b; i++) cnt += (int)ck(i);
cout << cnt << endl;
}