#include <iostream> #include <vector> #include <deque> typedef long long ll; ll vector_sum(const std::vector<ll>& v, ll p) { ll total = 0; for (auto value : v) { total = (total + value) % p; } return total; } int main() { ll l, h, p; std::cin >> l >> h >> p; std::vector<ll> starts; std::vector<ll> new_beginnings; starts.resize(h); new_beginnings.resize(h); for (ll i = 0; i < h; ++i) { new_beginnings[i] = (h - i) % p; } for (int i = 1; i < l; ++i) { std::deque<ll> running_sums; new_beginnings.swap(starts); ll running_sum = 0; ll grand_total = 0; for (int i = 0; i < h; i++) { running_sum += starts[i]; running_sum %= p; running_sums.push_back(running_sum); grand_total += running_sum; grand_total %= p; } ll loss = 0; for (int i = 0; i < h; i++) { new_beginnings[i] = grand_total; grand_total += loss - running_sums.front(); running_sums.pop_front(); loss += starts[h - i - 1]; ll gt_loss = starts[h - i - 1] * running_sums.size() % p; grand_total -= gt_loss; grand_total %= p; grand_total += p; grand_total %= p; } } std::cout << vector_sum(new_beginnings, p) << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> #include <vector> #include <deque> typedef long long ll; ll vector_sum(const std::vector<ll>& v, ll p) { ll total = 0; for (auto value : v) { total = (total + value) % p; } return total; } int main() { ll l, h, p; std::cin >> l >> h >> p; std::vector<ll> starts; std::vector<ll> new_beginnings; starts.resize(h); new_beginnings.resize(h); for (ll i = 0; i < h; ++i) { new_beginnings[i] = (h - i) % p; } for (int i = 1; i < l; ++i) { std::deque<ll> running_sums; new_beginnings.swap(starts); ll running_sum = 0; ll grand_total = 0; for (int i = 0; i < h; i++) { running_sum += starts[i]; running_sum %= p; running_sums.push_back(running_sum); grand_total += running_sum; grand_total %= p; } ll loss = 0; for (int i = 0; i < h; i++) { new_beginnings[i] = grand_total; grand_total += loss - running_sums.front(); running_sums.pop_front(); loss += starts[h - i - 1]; ll gt_loss = starts[h - i - 1] * running_sums.size() % p; grand_total -= gt_loss; grand_total %= p; grand_total += p; grand_total %= p; } } std::cout << vector_sum(new_beginnings, p) << std::endl; return 0; } |