2020 Multi-University Training Contest 9(待补
2020 Multi-University Training Contest 9
1001 Tree
-
思路:把叶子节点和根节点连接起来形成一个环即可满足题意 先用dfs求出每个节点子树大小 再用dfs求出到叶子节点路径取个最大值$
-
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 5e5 + 10;
int t, n, tot;
int edge[N], nxt[N], head[N], sz[N];
ll ans;
inline void add(int u, int v){
edge[ ++ tot ] = v;
nxt[tot] = head[u];
head[u] = tot;
}
inline int dfs1(int u){
sz[u] = 1;
for (int i = head[u]; i; i = nxt[i])
sz[u] += dfs1(edge[i]);
return sz[u];
}
inline void dfs2(int u, ll res){
ans = max(ans, res);
for (int i = head[u]; i; i = nxt[i])
dfs2(edge[i], res + n - sz[edge[i]]);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
freopen("my_out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t -- ){
ans = 0, tot = 0;
cin >> n;
for (int i = 0; i <= n; i ++ )
head[i] = 0;
for (int i = 2; i <= n; i ++ ){
int u;
cin >> u;
add(u, i);
}
dfs1(1);
dfs2(1, 0);
for (int i = 1; i <= n; i ++ )
ans += sz[i];
cout << ans << "\n";
}
return 0;
}
1003 Slime and Stones
- 思路:
\[\frac{1}{a} + \frac{1}{a+k+1}=1 \\
a^2+(k-1)a-(k+1)=0
\]
得到必败态
\[a_n=\frac{\sqrt{k^2+2k+5}+1-k}{2}*n \quad b_n=a_n+(k+1)*n \\
\Longrightarrow \quad \frac{\sqrt{k^2+2k+5}+1-k}{2} * \frac{b-a}{k+1}=a
\]
- AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
// const double tmp = (sqrt(5.0) + 1) / 2.0;
int t, a, b, k;
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t -- ){
cin >> a >> b >> k;
if (a > b)
swap(a, b);
if (b - a - k <= 0){
cout << "1\n";
continue;
}
// cout << (b - a - k) * tmp << " " << a << "\n";
// if (int(double(b - a - k) * tmp) == a)
// cout << "0\n";
// else
// cout << "1\n";
if ((b - a) % (k + 1) == 0){
double tmp = (sqrt(1.0 * k * k + 2.0 * k + 5.0) + 1.0 - 1.0 * k) / 2.0;
// cout << (b - a) / (k + 1) << " " << tmp << " " << a << "\n";
if (int(tmp * ((b - a) / (k + 1))) == a)
cout << "0\n";
else
cout << "1\n";
}
else
cout << "1\n";
}
return 0;
}