AtCoder Beginner Contest 152
比赛链接:https://atcoder.jp/contests/abc152/tasks
A - AC or WA
题意
有 $n$ 个测试样例,通过了 $m$ 个,判断是否 AC 。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; cout << (n == m ? "Yes" : "No"); }
B - Comparing Strings
题意
给出两个一位数 $a$ 和 $b$,可以用 $a$ 个 $b$ 或 $b$ 个 $a$ 拼成字符串,输出二者中的字典序最小者。
代码
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; cout << string(max(a, b), min(a, b) + '0'); }
C - Low Elements
题意
给出一个大小为 $n$ 的排列,问有多少数小于左边所有的数。
题解
记录左边数的最小值,如果当前数小于该值则满足条件,计入答案并更新最小值。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ans = 0; int mi = INT_MAX; for (int i = 0; i < n; i++) { int x; cin >> x; if (x < mi) mi = x, ++ans; } cout << ans << "\n"; }
D - Handstand 2
题意
给出一个正整数 $n$,找出有多少对不超过 $n$ 的正整数满足一个数的首端数字和尾端数字是另一个数的尾端数字和首端数字。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<pair<int, int>, int> mp; for (int i = 1; i <= n; i++) { string s = to_string(i); ++mp[{s[0] - '0', s.back() - '0'}]; } int ans = 0; for (auto i : mp) { int x = i.first.first; int y = i.first.second; ans += mp[{x, y}] * mp[{y, x}]; } cout << ans << "\n"; }
E - Flatten
题意
给出一个大小为 $n$ 的数组 $a$,构造一个数组 $b$ 使得 $a_ib_i = a_jb_j$,问 $\sum_{i = 1}^n b_i$ 的最小值。
($1 \le n \le 10^4, 1 \le a_i \le 10^6$)
题解
思路很简单,求出 $lcm_a$,计算 $\sum_{i = 1}^n \frac{lcm_a}{a_i}$ 即可。
需要注意的是因为 $a_i$ 较大,所以需要用质因数在模的意义下求 $lcm$,同理,$\frac{lcm_a}{a_i} = lcm_a \times inv_{a_i}$ 。
代码
#include <bits/stdc++.h> using ll = long long; using namespace std; const ll N = 1e6 + 10; const ll mod = 1e9 + 7; map<int, int> tot; int mi_p[N]; ll binpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } ll inv(ll x) { return binpow(x, mod - 2); } void init() { for (int i = 2; i < N; i++) { if (mi_p[i]) continue; for (int j = i; j < N; j += i) mi_p[j] = i; } } void update_tot(ll x) { while (x != 1) { int p = mi_p[x]; int cnt = 0; while (x % p == 0) x /= p, ++cnt; tot[p] = max(tot[p], cnt); } } int main() { init(); int n; cin >> n; int a[n] = {}; for (int i = 0; i < n; i++) cin >> a[i], update_tot(a[i]); ll L = 1; for (auto i : tot) L *= binpow(i.first, i.second), L %= mod; ll ans = 0; for (int i = 0; i < n; i++) ans += L * inv(a[i]), ans %= mod; cout << ans << "\n"; }

浙公网安备 33010602011771号