题解:P15359 「WYZOI R2」春运

· · 题解

不难发现,nm 次操作后,序列 a 循环右移了 n 次,回到初始状态;同时初始时是第 0 天,nm 次操作后时间到达第 nm 天,而 nm\bmod n=0,也回到初始状态,因此每 nm 次操作形成一个循环。

火车最后停留的站点即为火车前进的步数模 d 的值。我们可以先模拟出一次循环后火车前进的步数,而我们知道这个循环会重复 \left\lfloor\frac{t}{nm}\right\rfloor 次,剩余的 t\bmod (nm) 天可以直接模拟完成。

时间复杂度 O(nm)。

::::info[AC 代码]

#include <bits/stdc++.h>
using namespace std;
long long d, n, m, t, ans, D, a[4000];
void push() { for (int i = n - 1; i > 0; i--) swap(a[i], a[i - 1]); };
int main() {
    cin >> d >> n >> m >> t;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n * m; i++) {
        if (i % m == 0) push();
        D = (D + a[i % n]) % d;
        if (i < t % (n * m)) ans = (ans + a[i % n]) % d;
    }
    cout << (ans + t / (n * m) % d * D) % d;
    return 0;
}

::::