ZJUT2024迎新赛预赛
先看看代码吧,题解有空再补
A.宇宙校园跑(一)/ Cosmic Campus Run (Part I)
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
#define int long long
int n = 200000, m = 200000;
std::cin >> n >> m;
std::vector<int> a(m);
for (int i = 0; i < m; ++i) {
// a[i] = i % 2 ? 1 : 100000;
std::cin >> a[i];
}
int ans = 0;
for (int i = 0; i < m; ++i) {
if (a[i] >= a[(i - 1 + m) % m]) {
debug(a[i - 1] + n - a[i]);
ans += std::min(a[i] - a[(i - 1 + m) % m], a[(i - 1 + m) % m] + n - a[i]);
} else {
debug(n - a[i - 1] + a[i]);
ans += std::min(a[(i - 1 + m) % m] - a[i], n - a[(i - 1 + m) % m] + a[i]);
}
}
std::cout << ans << '\n';
}
B.小苏的回文日期 / Xiao Su's Palindromic Dates
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::vector<int> idates = {1011010, 1100110, 1111110, 1200210, 1211210, 1300310, 1400410, 1500510, 1600610, 1700710, 1800810, 1900910, 2011020, 2100120, 2111120, 2200220, 2211220, 2300320, 2400420, 2500520, 2600620, 2700720, 2800820, 2900920, 3011030, 3100130, 3111130, 3211230, 3300330, 3400430, 3500530, 3600630, 3700730, 3800830, 3900930, 10011001, 10100101, 10111101, 10200201, 10211201, 10300301, 10400401, 10500501, 10600601, 10700701, 10800801, 10900901, 11011011, 11100111, 11111111, 11200211, 11211211, 11300311, 11400411, 11500511, 11600611, 11700711, 11800811, 11900911, 12011021, 12100121, 12111121, 12200221, 12211221, 12300321, 12400421, 12500521, 12600621, 12700721, 12800821, 12900921, 13011031, 13100131, 13211231, 13300331, 13500531, 13700731, 13800831, 20011002, 20100102, 20111102, 20200202, 20211202, 20300302, 20400402, 20500502, 20600602, 20700702, 20800802, 20900902, 21011012, 21100112, 21111112, 21200212, 21211212, 21300312, 21400412, 21500512, 21600612, 21700712, 21800812, 21900912, 22011022, 22100122, 22111122, 22200222, 22211222, 22300322, 22400422, 22500522, 22600622, 22700722, 22800822, 22900922, 30011003, 30100103, 30111103, 30200203, 30211203, 30300303, 30400403, 30500503, 30600603, 30700703, 30800803, 30900903, 31011013, 31100113, 31111113, 31200213, 31211213, 31300313, 31400413, 31500513, 31600613, 31700713, 31800813, 31900913, 32011023, 32100123, 32111123, 32200223, 32211223, 32300323, 32400423, 32500523, 32600623, 32700723, 32800823, 32900923, 40011004, 40100104, 40111104, 40200204, 40211204, 40300304, 40400404, 40500504, 40600604, 40700704, 40800804, 40900904, 41011014, 41100114, 41111114, 41200214, 41211214, 41300314, 41400414, 41500514, 41600614, 41700714, 41800814, 41900914, 42011024, 42100124, 42111124, 42200224, 42211224, 42300324, 42400424, 42500524, 42600624, 42700724, 42800824, 42900924, 50011005, 50100105, 50111105, 50200205, 50211205, 50300305, 50400405, 50500505, 50600605, 50700705, 50800805, 50900905, 51011015, 51100115, 51111115, 51200215, 51211215, 51300315, 51400415, 51500515, 51600615, 51700715, 51800815, 51900915, 52011025, 52100125, 52111125, 52200225, 52211225, 52300325, 52400425, 52500525, 52600625, 52700725, 52800825, 52900925, 60011006, 60100106, 60111106, 60200206, 60211206, 60300306, 60400406, 60500506, 60600606, 60700706, 60800806, 60900906, 61011016, 61100116, 61111116, 61200216, 61211216, 61300316, 61400416, 61500516, 61600616, 61700716, 61800816, 61900916, 62011026, 62100126, 62111126, 62200226, 62211226, 62300326, 62400426, 62500526, 62600626, 62700726, 62800826, 62900926, 70011007, 70100107, 70111107, 70200207, 70211207, 70300307, 70400407, 70500507, 70600607, 70700707, 70800807, 70900907, 71011017, 71100117, 71111117, 71200217, 71211217, 71300317, 71400417, 71500517, 71600617, 71700717, 71800817, 71900917, 72011027, 72100127, 72111127, 72200227, 72211227, 72300327, 72400427, 72500527, 72600627, 72700727, 72800827, 72900927, 80011008, 80100108, 80111108, 80200208, 80211208, 80300308, 80400408, 80500508, 80600608, 80700708, 80800808, 80900908, 81011018, 81100118, 81111118, 81200218, 81211218, 81300318, 81400418, 81500518, 81600618, 81700718, 81800818, 81900918, 82011028, 82100128, 82111128, 82200228, 82211228, 82300328, 82400428, 82500528, 82600628, 82700728, 82800828, 82900928, 90011009, 90100109, 90111109, 90200209, 90211209, 90300309, 90400409, 90500509, 90600609, 90700709, 90800809, 90900909, 91011019, 91100119, 91111119, 91200219, 91211219, 91300319, 91400419, 91500519, 91600619, 91700719, 91800819, 91900919, 92011029, 92100129, 92111129, 92211229, 92300329, 92400429, 92500529, 92600629, 92700729, 92800829, 92900929};
std::vector<std::string> sdates;
for (int i = 0; i < idates.size(); ++i) {
std::stringstream ss;
ss << std::setw(8) << std::setfill('0') << idates[i];
sdates.push_back(ss.str());
}
int tt;
for (std::cin >> tt; tt--;) {
std::string date;
std::cin >> date;
std::cout << *std::lower_bound(sdates.begin(), sdates.end(), date) << '\n';
}
}
附上打表代码
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
bool isValidDate(int day, int month, int year) {
if (month < 1 || month > 12) return false;
switch (month) {
case 4: case 6: case 9: case 11:
return day >= 1 && day <= 30;
case 2:
return day >= 1 && day <= 28;
default:
return day >= 1 && day <= 31;
}
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::vector<std::string> dates;
for (int year = 0; year < 10000; ++year) {
for (int month = 1; month <= 12; ++month) {
for (int day = 1; day <= 31; ++day) {
if (isValidDate(day, month, year)) {
std::stringstream ss;
ss << std::setw(4) << std::setfill('0') << year;
ss << std::setw(2) << std::setfill('0') << month;
ss << std::setw(2) << std::setfill('0') << day;
std::string date = ss.str();
std::string rv = date;
std::reverse(rv.begin(), rv.end());
if (rv == date) {
dates.push_back(rv);
}
}
}
}
}
// debug(dates);
int tt;
for (std::cin >> tt; tt--;) {
std::string date;
std::cin >> date;
std::cout << *std::upper_bound(dates.begin(), dates.end(), date) << '\n';
}
}
C.连续自然数的和 / Sum of Consecutive Natural Numbers
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
n <<= 1;
std::vector<std::vector<int>> ans;
auto check = [&](int x, int y) {
return (y - x + 1) % 2 == 0;
};
for (int i = 2; i < n / i; ++i) {
if (n % i == 0) {
debug(i, n / i);
if (check(i, n / i)) {
int x = (n / i - i + 1) / 2;
int y = n / i - x;
std::vector<int> ord(y - x + 1);
std::iota(ord.begin(), ord.end(), x);
ans.push_back(ord);
}
}
}
std::cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); ++i) {
for (int j = 0; j < ans[i].size(); ++j) {
std::cout << ans[i][j] << " ";
}
std::cout << '\n';
}
}
D.全押 / Full-Chord
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<std::pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
int l, r;
std::cin >> l >> r;
a[i] = {l, r};
}
std::sort(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < n; ++i) {
int l, r, j = i;
std::tie(l, r) = a[i];
while (j < n && a[j].first <= r) {
r = std::min(r, a[j].second);
j++;
}
ans++;
i = j - 1;
}
std::cout << ans << '\n';
}
E.买糖 / Buying Candy
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
#define int long long
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
std::priority_queue<std::pair<int, int>> heap;
heap.push({a[n - 1], n - 1});
std::vector<int> ans(n);
for (int i = n - 2; i >= 0; --i) {
while (heap.size() && heap.top().first >= a[i]) {
auto u = heap.top();
heap.pop();
ans[u.second] += u.first / a[i];
u.first %= a[i];
if (u.first != 0) {
heap.push(u);
}
}
heap.push({a[i], i});
}
std::cout << *std::max_element(ans.begin(), ans.end()) << '\n';
}
F.灌溉 / Irrigation
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
using Edge = int;
struct HLD {
int n;
std::vector<int> siz, top, dep, parent, in, out, seq;
std::vector<std::vector<Edge>> g;
int cur;
HLD() {}
HLD(int n) { init(n); }
void init(int n) {
this->n = n;
siz.resize(n);
top.resize(n);
dep.resize(n);
parent.resize(n);
in.resize(n);
out.resize(n);
seq.resize(n);
cur = 0;
g.assign(n, {});
}
void addEdge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
void work(int root = 0) {
top[root] = root;
dep[root] = 0;
parent[root] = -1;
dfsSiz(root);
dfsHld(root);
}
void dfsSiz(int u) {
if (parent[u] != -1) {
g[u].erase(std::find(g[u].begin(), g[u].end(), parent[u]));
}
siz[u] = 1;
for (auto &v : g[u]) {
parent[v] = u;
dep[v] = dep[u] + 1;
dfsSiz(v);
siz[u] += siz[v];
if (siz[v] > siz[g[u][0]]) {
std::swap(v, g[u][0]);
}
}
}
void dfsHld(int u) {
in[u] = cur++;
seq[in[u]] = u;
for (auto v : g[u]) {
top[v] = v == g[u][0] ? top[u] : v;
dfsHld(v);
}
out[u] = cur;
}
int lca(int u, int v) {
while (top[u] != top[v]) {
dep[top[u]] > dep[top[v]] ? u = parent[top[u]] : v = parent[top[v]];
}
return dep[u] < dep[v] ? u : v;
}
int dist(int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int jump(int u, int k) {
if (dep[u] < k) return -1;
int d = dep[u] - k;
while (dep[top[u]] > d) {
u = parent[top[u]];
}
return seq[in[u] - dep[u] + d];
}
bool isAncester(int u, int v) { return in[u] <= in[v] && in[v] < out[u]; }
int rootedLca(int a, int b, int c) { return lca(a, b) ^ lca(b, c) ^ lca(a, c); }
template<typename Q>
void modifyPath(int u, int v, const Q &q, bool edge = false) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
q(in[top[u]], in[u]);
u = parent[top[u]];
}
if (dep[u] > dep[v]) std::swap(u, v);
q(in[u] + edge, in[v]);
}
template<typename Q>
void modifySubtree(int u, const Q &q) { q(in[u], out[u]); }
template<typename T, typename Q>
T queryPath(int u, int v, const Q &q, bool edge = false) {
T ret = T();
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
ret = q(in[top[u]], in[u]) + ret;
u = parent[top[u]];
}
if (dep[u] > dep[v]) std::swap(u, v);
return q(in[u] + edge, in[v]) + ret;
}
template<typename T, typename Q>
T querySubtree(int u, const Q &q) { return q(in[u], out[u]); }
template<typename T, typename Q, typename F>
T queryPathNoncommutative(int u, int v, const Q &q, const F &f, bool edge = false) {
T left = T(), right = T();
while(top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v), std::swap(left, right);
left = q(in[top[u]], in[u]) + left;
u = parent[top[u]];
}
if (dep[u] > dep[v]) std::swap(u, v), std::swap(left, right);
return f(left, q(in[u] + edge, in[v]) + right);
}
template<typename T, typename Q>
T queryPathDirection(int u, int v, const Q &q, bool edge = false) {
T left = T(), right = T();
while(top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) {
right = q(in[top[v]], in[v]) + right;
v = parent[top[v]];
} else {
left = q(in[top[u]], in[u]) + left;
u = parent[top[u]];
}
}
if (dep[u] > dep[v]) {
left = q(in[v], in[u]) + left;
} else {
right = q(in[u], in[v]) + right;
}
std::swap(left.lmx, left.rmx);
return left + right;
}
std::pair<std::unordered_map<int, std::vector<int>>, int> virtualTree(std::vector<int> v) {
auto cmp = [&](int a, int b) { return in[a] < in[b]; };
std::sort(v.begin(), v.end(), cmp);
v.erase(std::unique(v.begin(), v.end()), v.end());
const int k = (int)v.size();
for (int i = 0; i + 1 < k; ++i) {
v.push_back(lca(v[i], v[i + 1]));
}
std::sort(v.begin(), v.end(), cmp);
v.erase(std::unique(v.begin(), v.end()), v.end());
std::unordered_map<int, std::vector<int>> res;
std::vector<int> stk;
for (auto x : v) {
while (!stk.empty() && out[stk.back()] < in[x]) {
stk.pop_back();
}
if (!stk.empty()) {
res[stk.back()].push_back(x);
}
stk.push_back(x);
}
return {res, v[0]};
}
std::pair<std::vector<int>, std::vector<std::pair<int, int>>> compress(std::vector<int> v) {
auto cmp = [&](int a, int b) { return in[a] < in[b]; };
std::sort(v.begin(), v.end(), cmp);
v.erase(std::unique(v.begin(), v.end()), v.end());
const int k = (int)v.size();
for (int i = 0; i + 1 < k; ++i) {
v.push_back(lca(v[i], v[i + 1]));
}
std::sort(v.begin(), v.end(), cmp);
v.erase(std::unique(v.begin(), v.end()), v.end());
std::vector<std::pair<int, int> > edges;
std::vector<int> stk;
for (auto x : v) {
while (!stk.empty() && out[stk.back()] < in[x]) {
stk.pop_back();
}
if (!stk.empty()) {
edges.push_back({stk.back(), x});
}
stk.push_back(x);
}
return {v, edges};
}
};
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, k;
std::cin >> n >> k;
HLD hld(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
std::cin >> u >> v;
u--, v--;
hld.addEdge(u, v);
}
hld.work(0);
debug(hld.parent, hld.lca(0, 1));
std::vector<int> v(k);
for (int i = 0; i < k; ++i) {
std::cin >> v[i], v[i]--;
}
int root = v[0];
for (int i = 1; i < k; ++i) {
debug(root, v[i]);
root = hld.lca(root, v[i]);
}
std::vector<bool> used(n);
used[root] = true;
for (int i = 0; i < k; ++i) {
int dot = v[i];
debug(i, dot);
while (~dot && !used[dot]) {
used[dot] = true;
dot = hld.parent[dot];
debug(dot);
}
}
debug(used);
std::cout << std::count(used.begin(), used.end(), true) << '\n';
}
G.性质不同的数字 / Different Num
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<std::pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
int l, r;
std::cin >> l >> r;
a[i] = {l, r};
}
debug(a);
std::set<std::vector<int>> S;
for (int i = 0; i <= 301; ++i) {
std::vector<int> st;
for (int j = 0; j < n; ++j) {
debug(i, a[j].first, a[j].second);
if (i >= a[j].first && i <= a[j].second) {
st.push_back(j);
}
}
debug(i, st);
S.insert(st);
}
std::cout << S.size() << '\n';
}
H.王国法师 / The Kingdom Magicians
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
std::cin >> n >> m;
std::vector<int> d(n);
while (m--) {
int a, b;
std::cin >> a >> b;
a--, b--;
d[a]++;
}
std::cout << (std::count(d.begin(), d.end(), 0) == 1 ? "Bob" : "Alice") << '\n';
}
I.二进制拆解 / Binary Decomposition
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
using i64 = long long;
template <class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a)
if (b % 2) res *= a;
return res;
}
template <int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % P)} {}
constexpr int norm(int x) const {
if (x < 0) x += P;
if (x >= P) x -= P;
return x;
}
constexpr int val() const { return x; }
explicit constexpr operator int() const { return x; }
constexpr MInt operator-() const {
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, P - 2);
}
constexpr MInt &operator*=(MInt rhs) {
x = 1ll * x * rhs.x % P;
return *this;
}
constexpr MInt &operator+=(MInt rhs) {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) { return *this *= rhs.inv(); }
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); }
friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
};
template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
//constexpr int P = 1e9 + 7;
using Z = MInt<P>;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
constexpr int N = 1e7 + 10;
std::vector<Z> f(N);
f[0] = f[1] = 1;
for (int i = 2; i < N; ++i) {
f[i] = f[i - 1];
if (i % 2 == 0) {
f[i] += f[i >> 1];
}
}
int tt;
for (std::cin >> tt; tt--;) {
int n;
std::cin >> n;
std::cout << f[n] << '\n';
}
}
J.零点 / Zero Point
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
using i64 = long long;
template <class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a)
if (b % 2) res *= a;
return res;
}
template <int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % P)} {}
constexpr int norm(int x) const {
if (x < 0) x += P;
if (x >= P) x -= P;
return x;
}
constexpr int val() const { return x; }
explicit constexpr operator int() const { return x; }
constexpr MInt operator-() const {
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, P - 2);
}
constexpr MInt &operator*=(MInt rhs) {
x = 1ll * x * rhs.x % P;
return *this;
}
constexpr MInt &operator+=(MInt rhs) {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) { return *this *= rhs.inv(); }
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); }
friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
};
template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
//constexpr int P = 1e9 + 7;
using Z = MInt<P>;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
auto getSum = [&](const std::vector<int> &a) {
return std::accumulate(a.begin(), a.end(), 0LL);
};
auto getMul = [&](const std::vector<int> &a) {
return std::accumulate(a.begin(), a.end(), Z(1), std::multiplies<Z>());
};
auto getXor = [&](const std::vector<int> &a) {
return std::accumulate(a.begin(), a.end(), 0, std::bit_xor<int>());
};
std::vector<int> a, b;
for (int i = 0; i < 8; ++i) {
a.push_back(4);
b.push_back(2);
b.push_back(2);
}
for (int i = 0; i < 2; ++i) {
a.push_back(169);
a.push_back(11);
a.push_back(11);
a.push_back(7);
a.push_back(7);
b.push_back(49);
b.push_back(121);
b.push_back(13);
b.push_back(13);
}
for (int i = 0; i < 6; ++i) {
a.push_back(3);
a.push_back(3);
b.push_back(9);
}
int n = a.size();
std::cout << n << '\n';
for (int i = 0; i < n; ++i) {
std::cout << a[i] << " \n"[i == n - 1];
}
for (int i = 0; i < n; ++i) {
std::cout << b[i] << " \n"[i == n - 1];
}
}
M.浮点数/Float
#include <bits/stdc++.h>
// #ifdef LOCAL
// #include "algo/debug.hpp"
// #else
// #define debug(...) 42
// #endif
using i64 = long long;
template <class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a)
if (b % 2) res *= a;
return res;
}
template <int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % P)} {}
constexpr int norm(int x) const {
if (x < 0) x += P;
if (x >= P) x -= P;
return x;
}
constexpr int val() const { return x; }
explicit constexpr operator int() const { return x; }
constexpr MInt operator-() const {
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, P - 2);
}
constexpr MInt &operator*=(MInt rhs) {
x = 1ll * x * rhs.x % P;
return *this;
}
constexpr MInt &operator+=(MInt rhs) {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) { return *this *= rhs.inv(); }
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); }
friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
};
template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
//constexpr int P = 1e9 + 7;
using Z = MInt<P>;
std::vector<int> rev;
std::vector<Z> roots{0, 1};
void dft(std::vector<Z> &a) {
int n = a.size();
if (int(rev.size()) != n) {
int k = __builtin_ctz(n) - 1;
rev.resize(n);
for (int i = 0; i < n; i++) {
rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
}
}
for (int i = 0; i < n; i++) {
if (rev[i] < i) {
std::swap(a[i], a[rev[i]]);
}
}
if (int(roots.size()) < n) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = power(Z(3), (P - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[2 * i] = roots[i];
roots[2 * i + 1] = roots[i] * e;
}
k++;
}
}
for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}
void idft(std::vector<Z> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
Z inv = (1 - P) / n;
for (int i = 0; i < n; i++) {
a[i] *= inv;
}
}
struct Poly {
std::vector<Z> a;
Poly() {}
explicit Poly(int size, std::function<Z(int)> f = [](int) { return 0; }) : a(size) {
for (int i = 0; i < size; i++) {
a[i] = f(i);
}
}
Poly(const std::vector<Z> &a) : a(a) {}
Poly(const std::initializer_list<Z> &a) : a(a) {}
int size() const {
return a.size();
}
void resize(int n) {
a.resize(n);
}
Z operator[](int idx) const {
if (idx < size()) {
return a[idx];
} else {
return 0;
}
}
Z &operator[](int idx) {
return a[idx];
}
Poly mulxk(int k) const {
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
}
Poly modxk(int k) const {
k = std::min(k, size());
return Poly(std::vector<Z>(a.begin(), a.begin() + k));
}
Poly divxk(int k) const {
if (size() <= k) {
return Poly();
}
return Poly(std::vector<Z>(a.begin() + k, a.end()));
}
friend Poly operator+(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return Poly(res);
}
friend Poly operator-(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}
return Poly(res);
}
friend Poly operator-(const Poly &a) {
std::vector<Z> res(a.size());
for (int i = 0; i < int(res.size()); i++) {
res[i] = -a[i];
}
return Poly(res);
}
friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
if (a.size() < b.size()) {
std::swap(a, b);
}
if (b.size() < 128) {
Poly c(a.size() + b.size() - 1);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
c[i + j] += a[i] * b[j];
}
}
return c;
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
a.a.resize(sz);
b.a.resize(sz);
dft(a.a);
dft(b.a);
for (int i = 0; i < sz; ++i) {
a.a[i] = a[i] * b[i];
}
idft(a.a);
a.resize(tot);
return a;
}
friend Poly operator*(Z a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}
friend Poly operator*(Poly a, Z b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}
Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}
Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}
Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}
Poly &operator*=(Z b) {
return (*this) = (*this) * b;
}
Poly deriv() const {
if (a.empty()) {
return Poly();
}
std::vector<Z> res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * a[i + 1];
}
return Poly(res);
}
Poly integr() const {
std::vector<Z> res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = a[i] / (i + 1);
}
return Poly(res);
}
Poly inv(int m) const {
Poly x{a[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}
Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}
Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}
Poly pow(int k, int m) const {
int i = 0;
while (i < size() && a[i].val() == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(std::vector<Z>(m));
}
Z v = a[i];
auto f = divxk(i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
}
Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((P + 1) / 2);
}
return x.modxk(m);
}
Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).divxk(n - 1);
}
std::vector<Z> eval(std::vector<Z> x) const {
if (size() == 0) {
return std::vector<Z>(x.size(), 0);
}
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
struct Comb {
int n;
std::vector<Z> _fac, _invfac, _inv;
Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
Comb(int n) : Comb() { init(n); }
void init(int m) {
if (m <= n) return;
_fac.resize(m + 1), _invfac.resize(m + 1), _inv.resize(m + 1);
for (int i = n + 1; i <= m; ++ i) _fac[i] = _fac[i - 1] * i;
_invfac[m] = _fac[m].inv();
for (int i = m; i > n; -- i) _invfac[i - 1] = _invfac[i] * i, _inv[i] = _invfac[i] * _fac[i - 1];
n = m;
}
Z fac(int m) {
if (m > n) init(2 * m);
return _fac[m];
}
Z invfac(int m) {
if (m > n) init(2 * m);
return _invfac[m];
}
Z inv(int m) {
if (m > n) init(2 * m);
return _inv[m];
}
Z binom(int n, int m) {
if (n < m || m < 0) return 0;
return fac(n) * invfac(m) * invfac(n - m);
}
} comb;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
#define int long long
int n, m;
// Z p;
int pp;
std::cin >> n >> m >> pp;
Z p = pp, vp = Z(1) - p;
std::vector<Z> np(n), nvp(m + 1);
np[0] = nvp[0] = 1;
for (int i = 1; i < n || i <= m; ++i) {
if (i < n) {
np[i] = np[i - 1] * p;
}
if (i <= m) {
nvp[i] = nvp[i - 1] * vp;
}
}
Z e = n;
for (int i = 0; i < n; ++i) {
e += np[i] * vp * (i + 1) / power(p, n);
}
Poly f(n + m), g(n);
for (int i = 0; i < n + m; ++i) {
f[i] = comb.fac(i) * (e + i + 1);
}
for (int i = 0; i < n; ++i) {
g[(n - 1) - i] = np[i] * comb.invfac(i);
}
Poly h = f * g;
// debug(f.a, g.a, h.a);
std::vector<Z> pre(m + 1);
pre[0] = power(p, n) * n;
for (int i = 1; i <= m; ++i) {
pre[i] = pre[i - 1] + comb.binom(n - 1 + i, n - 1) * power(p, n) * nvp[i] * (n + i);
}
// debug(pre);
for (int i = 0; i <= m; ++i) {
std::cout << pre[i] + nvp[i] * vp * comb.invfac(i) * h[i + n - 1] << " \n"[i == m];
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理