牛可乐发红包脱单ACM赛$
A:统计两颗生成树有多少条不相同的边即可
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
int N;
map<Pair, bool> mp;
int main() {
scanf("%d", &N);
for(int i = 1, x, y; i <= N - 1; i++) {
scanf("%d %d", &x, &y);
mp[MP(x, y)] = 1; mp[MP(y, x)] = 1;
}
int ans = 0;
for(int i = 1, x, y; i <= N - 1; i++) {
scanf("%d %d", &x, &y);
if(mp.find(MP(x, y)) == mp.end() && mp.find(MP(y, x)) == mp.end()) ans++;
}
printf("%d", ans);
return 0;
}
B题:
暴力前几项 然后oeis。。。
#include<iostream>
#include<cstdio>
#define LL long long
const LL mod = 100000007, inv = 50000004;
using namespace std;
inline LL read() {
char c = getchar(); LL 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;
}
LL N;
LL f(LL a, LL p) {
LL base = 1;
while(p) {
if(p & 1) base = (base * a) % mod;
a = (a * a) % mod; p >>= 1;
}
return base % mod;
}
int main() {
cin >> N;
LL ans = ((-3 * f(3, N) + mod) % mod + f(4, N) - 1 + mod) * inv + f(2, N - 1) * 3 % mod;
cout << ans % mod << endl;
return 0;
}
C题:
不会写。。。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const LL MAXN = 1e5 + 10;
LL T, N;
LL a[MAXN], q[MAXN];
LL solve() {
LL h = 1, t = 0, ans = 0, sum = 0;
for(LL i = 1; i <= N; i++) {
while(h <= t && a[i] > a[q[t]]) sum -= a[q[t]] * (q[t] - q[t - 1]), t--;
q[++t] = i;
ans += a[i] * (q[t] - q[t - 1]) + sum;
sum += a[i] * (q[t] - q[t - 1]);
}
return ans;
}
int main() {
scanf("%lld", &T);
while(T--) {
scanf("%lld", &N);
for(LL i = 1; i <= N; i++) scanf("%lld", &a[i]);
LL ans = solve();
for(LL i = 1; i <= N; i++) a[i] = -a[i];
LL ans2 = solve();
cout << ans + ans2 << endl;
}
return 0;
}