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 59 60 61 62 63 64 65 66
| #include <iostream> #include <cstdio> #include <queue> #include <climits> #define INF LLONG_MAX using namespace std; const int N = 5e5 + 10, M = 6e6 + 10; int n, m, mi = INF, a[12]; long long l, r, ans, dis[N]; int idx, hd[N], nxt[M], edg[M], wt[M]; bool vis[N]; void spfa () { dis[0] = 0; queue <int> q; q.push(0); vis[0] = true; while (!q.empty()) { int t = q.front(); q.pop(); if (dis[t] == INF) return; vis[t] = false; for (int i = hd[t]; i; i = nxt[i]) if (dis[t] + wt[i] < dis[edg[i]]) { dis[edg[i]] = dis[t] + wt[i]; if (!vis[edg[i]]) { q.push(edg[i]); vis[edg[i]] = true; } } } } void add (int x, int y, int z) { nxt[++idx] = hd[x]; hd[x] = idx; edg[idx] = y; wt[idx] = z; } int main () { cin >> m >> l >> r >> n; l--; for (int i = 0; i < n; i++) dis[i] = INF; for (int i = 1, x; i < m; i++) { cin >> x; for (int j = 0; j < n; j++) add(j, (j + x) % n, x); } spfa(); for (int i = 0; i < n; i++) { if (r >= dis[i]) ans += (r - dis[i]) / n + 1; if (l >= dis[i]) ans -= (l - dis[i]) / n + 1; } cout << ans; return 0; }
|