A、九峰与签到题
思路:
按题意模拟即可
代码:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 2e5 + 5;
int a[N], st1[N], st2[N];
string op;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
cin >> op;
if (op == "AC") {
a[x]++;
st1[x] = 1;
} else if (op == "UNAC") {
a[x]--;
st1[x] = 1;
}
if (a[x] < 0)
st2[x] = 1;
}
int cnt = 0;
for (int i = 0; i <= m; i++) {
if (a[i] >= 0 && st1[i] && !st2[i]) {
printf("%d ", i);
cnt++;
}
}
if (!cnt)
printf("-1");
return 0;
}
B、武辰延的字符串
思路:
先确定 s 串与 t 串中的一段满足条件的相同前缀(在 t 串中该前缀后面的第一个字母与 s 串中第一个字母相同),然后求出在 t 串中该前缀后面的连续 x 个字母,其也是 s 串中长度为 x 的前缀,这个 x 就是该种枚举方式的方案数。
对于 x 的值可以用字符串哈希 + 二分来确定
代码:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int N = 1e5 + 5, B = 131;
char s[N], t[N];
ULL h1[N], h2[N], p[N];
ULL get1(int l, int r) {
return h1[r] - h1[l-1] * p[r-l+1];
}
ULL get2(int l, int r) {
return h2[r] - h2[l-1] * p[r-l+1];
}
int main() {
scanf("%s%s", s+1, t+1);
p[0] = 1;
int n1 = strlen(s+1), n2 = strlen(t+1);
for (int i = 1; i <= max(n1, n2); i++) {
if (i <= n1)
h1[i] = h1[i-1] * B + s[i];
if (i <= n2)
h2[i] = h2[i-1] * B + t[i];
p[i] = p[i-1] * B;
}
ULL ans = 0;
for (int i = 1; i < n2; i++) {
if (s[i] != t[i])
break;
if (t[i+1] != s[1])
continue;
int l = 1, r = n1;
while (l < r) {
int mid = l + r + 1 >> 1;
ULL t1 = get1(1, mid);
ULL t2 = get2(i+1, i+mid);
if (t1 == t2) {
l = mid;
} else {
r = mid - 1;
}
}
ans += l;
}
cout << ans << "\n";
return 0;
}
H、吴楚月的表达式
思路:
按照题意建树然后遍历即可,注意四则运算的优先级与减法和除法的的取模方式
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5, p = 1e9 + 7;
int n, m;
int fa[N], num[N];
char c[N];
vector<int> son[N];
struct node{
LL a, b;
}ans[N];
LL qpow(LL a, LL b) {
LL ans = 1;
while (b) {
if (b & 1)
ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
}
void dfs(int x) {
for (int i = 0; i < son[x].size(); i++) {
if ((c[son[x][i]]) == '+') {
ans[son[x][i]].a = (ans[x].a + ans[x].b) % p;
ans[son[x][i]].b = num[son[x][i]] % p;
}
if ((c[son[x][i]]) == '-') {
ans[son[x][i]].a = (ans[x].a + ans[x].b) % p;
ans[son[x][i]].b = (-num[son[x][i]] % p + p) % p;
}
if ((c[son[x][i]]) == '*') {
ans[son[x][i]].a = ans[x].a;
ans[son[x][i]].b = ans[x].b * num[son[x][i]] % p;
}
if ((c[son[x][i]]) == '/') {
ans[son[x][i]].a = ans[x].a;
ans[son[x][i]].b = ans[x].b * qpow(num[son[x][i]], p-2) % p;
}
dfs(son[x][i]);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d", &num[i]);
for (int i = 2; i <= n; i++) {
scanf("%d", &fa[i]);
son[fa[i]].push_back(i);
}
for (int i = 2; i <= n; i++)
scanf(" %c", &c[i]);
ans[1].a = 0, ans[1].b = num[1];
dfs(1);
for (int i = 1; i <= n; i++)
cout << (ans[i].a+ans[i].b) % p << ' ';
return 0;
}
J、邬澄瑶的公约数
思路:
要求多个数的最大公约数,我们可以先将这些分解质因数,然后比较这些数中所有的质因子,选取每个质因子最大出现的最小指数相乘即可得到答案
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 1e4 + 5, MOD = 1e9 + 7;
LL n, k, ans = 1;
LL a[N], p[N];
LL prime[N];
bool st[N];
void init(LL m) {
for (int i = 2; i <= m; i++) {
if (!st[i])
prime[++k] = i;
for (int j = 1; prime[j] <= m / i; j++) {
st[prime[j] * i] = true;
if (i % prime[j] == 0)
break;
}
}
}
LL qpow(int a, int b, int m) {
LL ans = 1 % m;
while (b) {
if (b & 1)
ans = ans * a % m;
a = (LL)a * a % m;
b >>= 1;
}
return ans;
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%lld", &p[i]);
if (n == 1) {
printf("%lld\n", qpow(a[1], p[1], MOD));
return 0;
}
init(n);
LL temp = 0x3f3f3f3f;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
LL cnt = 0;
while (a[j] % prime[i] == 0) {
cnt++;
}
cnt = (cnt * p[j]) % MOD;
temp = min(temp, cnt);
}
ans = (ans * qpow(prime[i], temp, MOD)) % MOD;
}
cout << ans;
return 0;
}