显而易见,我们甚至不能 k(1k1012)k(1 \leq k \leq 10^{12}) 次模拟一遍。

不过很容易发现,在 modmod nn 这个操作下,最多 nn 次就一定会走入循环,后面就不需要模拟了。

具体怎么实现不就很简单了么。(代码能力不够就先放放,去刷几道模拟题。)

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

int T = 1;
const int N = 2e5 + 10;
int n, k;
int arr[N];
int tot;//x
int val[N];
bool vis[N];
map<int, int> mp;

void Solve() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}

	int idx = 0, cnt = 0;

	while (!vis[idx] && cnt < k) {
		tot += arr[idx];
		vis[idx] = true;
		val[++cnt] = tot;
		mp[idx] = cnt;

		idx = tot % n;
	}

	if (cnt == k) {
		cout << tot;
		return;
	}

	int lef = k - cnt;//剩下多少次
	int loop = tot - val[mp[idx] - 1];//一个循环的贡献
	int tim = cnt - mp[idx] + 1;//一个循环需要多少次
	cout << tot + (lef / tim) * loop + (val[(lef % tim) + mp[idx] - 1] - val[mp[idx] - 1]);
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	while (T--) {
		Solve();
	}
	return 0;
}

0 条评论

目前还没有评论...