# SDOI 2018二轮题解(除Day2T1)

Day2T1没时间做了咕咕咕

## 物理实验

#include<bits/stdc++.h>
#define fi first
#define se second
#define LL long long
#define pb push_back
#define double long double
using namespace std;
const int MAXN = 1e6 + 10;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
};
int N, opt[MAXN];
double x[MAXN][2], y[MAXN][2], pos[MAXN], bx, by, ex, ey, L;
double nx, val[MAXN], sec[MAXN];
struct Segment {
int id;
bool operator < (const Segment &rhs) const {
if(id == rhs.id) return 0;
int td = rhs.id;
double va = (y[id][1] - y[id][0]) / (x[id][1] - x[id][0]) * (nx - x[id][0]) + y[id][0];
double vb = (y[td][1] - y[td][0]) / (x[td][1] - x[td][0]) * (nx - x[td][0]) + y[td][0];
return abs(va) < abs(vb);
}
};
set<Segment> su, sd;
double GouGu(double x, double y) {
return sqrt(x * x + y * y);
}
int comp(int a, int b) {
return (a < 0 ? x[-a][1] : x[a][0]) < (b < 0 ? x[-b][1] : x[b][0]);
}
void init() {
nx = 0; memset(val, 0, sizeof(val));
}
void solve() {
init();
for(int i = 1; i <= N; i++) {
}
double dx = ex - bx, dy = ey - by, dis = GouGu(dx, dy);
double bl = (bx == by) ? 0 : by - dy / dx * bx;
for(int i = 1; i <= N; i++) y[i][0] -= bl, y[i][1] -= bl;
dx /= dis; dy /= dis;//dx cos dy sin
//(x, y)
for(int i = 1; i <= N; i++) {
double px1 = x[i][0], px2 = x[i][1], py1 = y[i][0], py2 = y[i][1];
x[i][0] = dx * px1 + dy * py1; //tag
y[i][0] = dx * py1 - dy * px1;
x[i][1] = dx * px2 + dy * py2;
y[i][1] = dx * py2 - dy * px2;
if(x[i][0] > x[i][1]) swap(x[i][0], x[i][1]), swap(y[i][0], y[i][1]);
sec[i] = GouGu(x[i][1] - x[i][0], y[i][1] - y[i][0]) / (x[i][1] - x[i][0]);
}
for(int i = 1; i <= 2 * N; i++) opt[i] = (i <= N ? -i : i - N);
sort(opt + 1, opt + 2 * N + 1, comp);
for(int i = 1; i <= 2 * N; i++) {
int u;
if(opt[i] > 0) {//insert
u = opt[i];
nx = pos[i] = x[u][0];
(y[u][0] > 0 ? su : sd).insert({u});
} else {//delet
u = -opt[i];
nx = pos[i] = x[u][1];
(y[u][0] > 0 ? su : sd).erase({u});
}
if(!su.empty()) val[i] += sec[su.begin()->id];
if(!sd.empty()) val[i] += sec[sd.begin()->id];
}
for(int i = 2 * N; i >= 1; i--) val[i] = val[i- 1];
double ret = 0, rl = pos[1] - L, rr = pos[1], ans = 0;
int pl = 1, pr = 2;
while(pr <= 2 * N) {
double dl = pos[pl] - rl, dr = pos[pr] - rr;
if(dl > dr)      ret += (val[pr] - val[pl]) * dr, pr++, rl += dr, rr += dr;
else if(dr > dl) ret += (val[pr] - val[pl]) * dl, pl++, rl += dl, rr += dl;
else ret += (val[pr] - val[pl]) * dl, pr++, pl++, rl += dl, rr += dl;
ans = max(ans, ret);
}
printf("%.15Lf\n", ans);
}
signed main() {
for(int T = read(); T--; solve());
return 0;
}


## 战略游戏

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define pb push_back
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7;
const LL INF = 1e18 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, nd;
vector<int> v[MAXN], E[MAXN];
int dfn[MAXN], low[MAXN], times, st[MAXN], tp;
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++times;
st[++tp] = x;
for(auto &to : v[x]) {
if(to == fa) continue;
if(!dfn[to]) {
tarjan(to, x), chmin(low[x], low[to]);
if(low[to] >= dfn[x]) {
E[++nd].pb(x); E[x].pb(nd);
do {
E[nd].pb(st[tp]); E[st[tp]].pb(nd);
}while(st[tp--] != to);
}
}
else chmin(low[x], dfn[to]);

}
}
int dis[MAXN], siz[MAXN], son[MAXN], top[MAXN], dep[MAXN], ffa[MAXN];
void dfs(int x, int fa) {
siz[x] = 1; ffa[x] = fa;
for(auto &to : E[x]) {
if(to == fa) continue;
dis[to] = dis[x] + (to <= N);
dep[to] = dep[x] + 1;
dfs(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf; dfn[x] = ++times;
if(!son[x]) return ;
dfs2(son[x], topf);
for(auto &to : E[x]) {
if(top[to]) continue;
dfs2(to, to);
}
}
int LCA(int x, int y) {
while(top[x] ^ top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = ffa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
int Dis(int x, int y) {
return dis[x] + dis[y] - 2 * dis[LCA(x, y)];
}
int comp(int a, int b) {
return dfn[a] < dfn[b];
}
void init() {
for(int i = 1; i <= N; i++) v[i].clear();
for(int i = 1; i <= nd; i++) E[i].clear();
#define m0(x) memset(x, 0, sizeof(x))
m0(top); m0(dfn); m0(low); m0(dis); m0(dep); m0(son); m0(ffa); m0(siz); m0(dis);
#undef m0
tp = 0; times = 0; nd = 0;

}
int ggg = 0;
void solve() {
init();
nd = N;
for(int i = 1; i <= M; i++) {
v[x].pb(y); v[y].pb(x);
}
tarjan(1, 0);
dfs(1, 0);
dfs2(1, 1);
for(int k = 1; k <= Q; k++) {
int num = read(); LL ans = 0;
vector<int> po;
for(int i = 1; i <= num; i++) po.pb(read());
sort(po.begin(), po.end(), comp);
for(int i = 0; i < po.size() - 1; i++) {
ans += Dis(po[i], po[i + 1]);
}
ans += Dis(po[num - 1], po[0]);
ans = ans  / 2 + (LCA(po[0], po[num - 1]) <= N) - num;
cout << ans << '\n';

}
}

signed main() {
for(int T = read(); T--; solve());
return 0;
}


## 反回文串

$\begin{cases} h(i) = &i &(i \equiv 1 \pmod 2)\\ &\frac{i}{2} &(Otherwise) \end{cases}$

$f(i)$表示最小循环节为$i$的字符串数量，显然有

$Ans(n) = \sum_{d|n} h(d) f(d)$

$f(d)$很难直接求，但是我们可以枚举循环节得到一个等式

$g(n) = \sum_{d|n} f(d)$

$f(n) = \sum_{d|n} \mu(d) g(\frac{n}{d})$

$Ans(n) = \sum_{d|n} h(d) \sum_{k |d} \mu(k) g(\frac{d}{k})$

$Ans(n) = \sum_{k|n} g(k) \sum_{k|d\text{且}d|n} h(d) \mu(\frac{d}{k})$

$d$拆为$d = d' * K$，我们去枚举新的$d'$

$Ans(n) = \sum_{k|n} g(k) \sum_{d|\frac{n}{k}} h(k d) \mu(d)$

$Ans(n) = \sum_{k|n} g(k)h(k) \sum_{d|\frac{n}{k}} d\mu(d)$

#include<bits/stdc++.h>
#define fi first
#define se second
#define int long long
#define LL long long
#define pb push_back
using namespace std;
const int MAXN = 1e6 + 10;
const LL INF = 1e18 + 10;
int mod;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
LL mul(LL a, LL b, int mod = mod) {
//a %= mod, b %= mod;
return ((a * b - (LL)((LL)((long double)a / mod * b + 1e-3) * mod)) % mod + mod) % mod;
}
int fp(int a, int p, int mod = mod) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a, mod);
a = mul(a, a, mod); p >>= 1;
}
return base;
}
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, K;
int st, ans;
vector<int> ds;
unordered_map<LL, LL> t, vds;
const int Tn = 9;
int Cbase[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
bool CheckP(int p) {
if(p == 1) return 0;
for(int i = 0; i < Tn; i++) if(p == Cbase[i]) return 1;
for(int id = 0; id < Tn; id++) {
int v = Cbase[id];
int lim = 0, t = p - 1, pre = 1;
while(t && (!(t & 1))) lim++, t>>= 1; t = fp(v, t, p);
while(lim--) {
t = mul(t, t, p);
if(t == 1 && (pre != p - 1 && pre != 1)) return 0;
pre = t;
}
if(t != 1) return 0;
}
return 1;
}

int rnd() {
return rand();
}
int gcd(int a, int b) {
return (!b) ? a : gcd(b, a % b);
}
int rd(int base, int mod) {
return (mul(base, base, mod) + st) % mod;
}
int myabs(int x, int y) {
return (x > y) ? x - y : y - x;
}

void Rho(int x) {
if(x == 1) return ;
if(CheckP(x)) {
vds[x]++;
return ;
}
while(1) {
st = rand() % x + 1;
int v1 = rnd() % x + 1, g = gcd(x, v1);
if(g != 1 && g != x) {Rho(g); Rho(x / g); return ;}
int v2 = v1; v1 = rd(v1, x);
for(int k = 0, tr = 1; v1 - v2; v1 = rd(v1, x), k++) {
g = gcd(x, myabs(v1, v2));
if(g != 1 && g != x) {Rho(g); Rho(x / g); return ;}
if(k == tr) {v2 = v1; tr <<= 1;}
}
}
}
void Clear() {
ds.clear();
vds.clear();
ans = 0;
}
int g(int x) {
return fp(K, (x + 1) / 2);
}
int h(int x) {
return (x & 1) ? x : x / 2;
}

void dfs(int n, int num, int cur, unordered_map<LL, LL>::iterator now) {
if(now == vds.end()) {
ds.pb(num);
t[num] = cur;
return ;
}
int nn = now->se, vv = now->fi, vvd = (1 - now->fi), nv = vv, nvd = vvd; auto nxt = ++now;
dfs(n + 1, num, cur, nxt);
for(int i = 1; i <= nn; i++) {
dfs(n + 1, num * nv, cur * vvd, nxt);
nv *= vv; nvd *= vvd;
}
}
void print(int x) {
if(x > 9) print(x / 10);
putchar('0' + x % 10);
}
void solve() {
Clear();
Rho(N);
dfs(0, 1, 1, vds.begin());
for(auto &num: ds) {
if((num & 1) && (!((N / num) & 1))) continue;
add2(ans, mul(g(num), mul(h(num), t[N / num])));
}
print(ans); putchar('\n');
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
#endif
srand(20020113);
for(int T = read(); T--; solve());
return 0;
}


## 旧式题

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
#define LL long long

const int MAXN = 2e5 + 10, mod = 1e9 + 7;
using namespace std;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int mu[MAXN], prime[MAXN], vis[MAXN], tot, A, B, C, num, deg[MAXN];
int fa[MAXN], fb[MAXN], fc[MAXN];
vector<LL> di[MAXN];
vector<Pair> v[MAXN];//每个数的质因数分解
struct Edge {
LL u, v, w;
}E[MAXN * 10];
void GetPrime(int N) {
vis[1] = 1; mu[1] = 1;
for(int i = 2; i <= N; i++) {
if(!vis[i]) prime[++tot] = i, mu[i] = -1;
for(int j = 1; j <= tot && i * prime[j] <= N; j++) {
vis[i * prime[j]] = 1;
if(i % prime[j]) mu[i * prime[j]] = -mu[i];
else {mu[i * prime[j]] = 0; break;}
}
}
for(int i = 1; i <= tot; i++)
for(int j = 1; j * prime[i] <= N; j++)
di[j * prime[i]].push_back(prime[i]);

}

void Get(int *a, int N, int X) {
for(int i = 1; i <= N; i++)
for(int j = i; j <= N; j += i) a[i] += X / j;
}
LL lcm(int a, int b) {
return 1ll * a / __gcd(a, b) * b;
}
void init() {
memset(fa, 0, sizeof(fa));
memset(fb, 0, sizeof(fb));
memset(fc, 0, sizeof(fc));
memset(deg, 0, sizeof(deg));
num = 0;
for(int i = 1; i <= A; i++) v[i].clear();
}
void Build() {
for(int w = 1; w <= A; w++) {//lcm(u, v) = w;
if(!mu[w]) continue;
int n = di[w].size();
//for(auto x : di[w]) printf("%d ", x); puts("");
for(int sta = 0; sta < (1 << n); sta++) {
LL i = 1;
for(int b = 0; b < n; b++)
if(sta >> b & 1) i *= di[w][b];
for(int s = sta; ; s = sta & (s - 1)) {//tag
LL g = 1;
for(int b = 0; b < n; b++)
if(s >> b & 1)
g *= di[w][b];
int j = w * g / i;
if(i < j) E[++num] = {i, j, w};// printf("%d\n", num);
if(!s) break;
}
}
}
}

LL fuck(int x, int y, int w) {
if(mu[x] == 1)
else
return (-add(add(mul(mul(fa[w], fb[w]), fc[y]), mul(mul(fa[w], fb[y]), fc[w])), mul(mul(fa[y], fb[w]), fc[w])) + mod) % mod;
}

LL calc() {
//	for(int i = 1; i <= A; i++) for(auto &x : v[i])printf("%d %d %d\n", i, x.fi, x.se);
for(int i = 1; i <= num; i++) {
int x = E[i].u, y = E[i].v;
if(deg[x] > deg[y]) swap(x, y);
v[y].push_back(MP(x, E[i].w));
}
LL ans = 0;
for(int a = 1; a <= A; a++) {
for(auto &t1 : v[a]) {
LL b = t1.fi, w1 = t1.se;
for(auto &t2 : v[b]) {
LL c = t2.fi, w2 = t2.se, xi = mu[a] * mu[b] * mu[c];
LL w3 = lcm(a, c);
if(w3 > A) continue;
if(xi == 1) {
} else if(xi == -1) {
}
//	cout << ans << endl;
}
}
}

for(int i = 1; i <= num; i++) {//有两个一样
}
for(int i = 1; i <= C; i++) {//全都一样
if(mu[i] == 1) add2(ans, mul(mul(fa[i], fb[i]), fc[i]));
else if(mu[i] == -1) add2(ans, -mul(mul(fa[i], fb[i]), fc[i]) + mod);
}

return ans;
}
void solve() {
init();
if(A < B) swap(A, B); if(C > B) swap(B, C); if(A < B) swap(A, B);
Get(fa, A, A); Get(fb, A, B); Get(fc, A, C);
Build();
cout << calc() << '\n';
}
signed main() {
GetPrime(2e5);
for(int T = read(); T; T--, solve());
return 0;
}


## 荣誉称号

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define pb push_back
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e7 + 10, mod = 1e9 + 7;
const LL INF = 1e18 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int ch(int x, int l, int r) {
return x >= l && x <= r;
}
int N, K, M, P, a[MAXN], mx[MAXN], n, jump, fa[MAXN], b[MAXN];
vector<int> bel[MAXN];
unsigned int SA, SB, SC;int p, A, B;
unsigned int rng61(){
SA ^= SA << 16;
SA ^= SA >> 5;
SA ^= SA << 1;
unsigned int t = SA;
SA = SB;
SB = SC;
SC ^= t ^ SA;
return SC;
}
void gen(){
for(int i = 1; i <= P; i++) a[i] = read() % M, b[i] = read();
for(int i = P + 1; i <= N; i++){
a[i] = rng61() % A + 1;
a[i] %= M;
b[i] = rng61() % B + 1;
}
}

LL w[(1 << 12) - 1][201];//w[i][j] 当i为j时，所有受i限制的点的代价
LL f[(1 << 12) - 1][201], num[211];

void dfs(int x, int dep) {
mx[x] = dep;
if(dep > K + 1) bel[fa[x]].pb(x);
int base = 2 * x;
for(int i = 0; i <= 1; i++) {
int to = 2 * x + i; if(to > N) continue;
dfs(to, dep + 1);
chmax(mx[x], mx[to]);
}
}
LL get(int val, LL target, LL inc) {//在%M意义下把val变为target的最小花费
if(target >= val) return 1l * (target - val) * inc;
return 1ll * ((M - val) + target) * inc;
}
void dfs2(int x) {
if(x > n || mx[x] <= K) return ;
if(x >= (1 << K)) {//叶节点
for(int i = 0; i < M; i++) f[x][i] = w[x][i];
return ;
}
for(int i = 0; i < 2; i++) if(2 * x + i <= n) dfs2(2 * x + i);
for(int i = 0; i < M; i++) {//与叶子节点的路径和%M = j
f[x][i] = INF;
for(int j = 0; j < M; j++) {//该节点的增量
LL sv = 0;
for(int gg = 0; gg < 2; gg++) {
int to = 2 * x + gg;
if(to <= n)
sv += f[to][(i - a[x] - j + 2 * M) % M];
}
chmin(f[x][i], sv + w[x][(a[x] + j) % M]);
}
}
}

void solve() {
gen();
memset(f, 0, sizeof(f));
memset(w, 0, sizeof(w));
jump = 1; n = (1 << (K + 1)) - 1;
for(int i = 1; i <= K + 1; i++) jump *= 2;
for(int i = 1; i <= N; i++) {
bel[i].clear();
}

for(int i = 1; i <= N; i++) fa[i] = (i <= n ? i : fa[i / jump]);
dfs(1, 1);

for(int i = 1; i < (1 << (K + 1)); i++) {
memset(num, 0, sizeof(num));
for(auto &son: bel[i]) num[a[son]] += b[son];
for(int j = 0; j < M; j++) {
w[i][j] += get(a[i], j, b[i]);
for(int k = 0; k < M; k++) w[i][j] += get(k, j, num[k]);
}
}
dfs2(1);
cout << f[1][0] << '\n';
}
signed main() {
#ifndef ONLINE_JUDGE