/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))
#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';
const int MAXN = 1005;
int n, dp[MAXN][5];
pii intervals[MAXN];
void init() {
cin >> n;
for (int i = 1; i <= n; ++i) {
pii &cur = intervals[i];
cin >> cur.fi >> cur.se;
}
}
int floor_div(int a, int b) {
if (b < 0) a = -a, b = -b;
if (a >= 0) return a / b;
return - ((-a + b - 1) / b);
}
int ceil_div(int a, int b) {
if (b < 0) a = -a, b = -b;
if (a >= 0) return (a + b - 1) / b;
return - ((-a) / b);
}
// Count numer of a_i in [l, r] such that a_i % m = k
int cnt_div(int l, int r, int m, int k) {
assert(k < m && l <= r);
int t_min = ceil_div(l - k, m);
int t_max = floor_div(r - k, m);
if (t_min > t_max) return 0;
return t_max - t_min + 1;
}
void solve() {
for (int j = 0; j < 5; ++j)
dp[1][j] = cnt_div(intervals[1].fi, intervals[1].se, 5, j);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < 5; ++j) {
for (int cur_rem = 0; cur_rem < 5; ++cur_rem) {
int nxt_rem = (j + cur_rem) % 5;
int mul = cnt_div(intervals[i + 1].fi, intervals[i + 1].se, 5, cur_rem);
dp[i + 1][nxt_rem] += dp[i][j] * mul;
}
}
}
// debug(dp[n], 0, 4);
int res = 0;
for (int i = 0; i < 5; ++i) {
int mul = i * i * i * i + 2 * i * i;
mul %= 5;
res += (mul + 1) * dp[n][i];
}
cout << res;
}
signed main() {
#ifdef NCTHANH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
init();
solve();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |