题解:AT_abc179_e [ABC179E] Sequence Sum
link
题意
定义
思路
由于
时间复杂度为
代码
// BLuemoon
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using DB = double;
const int kMaxN = 2e5 + 5;
LL n, x, m, a[kMaxN], ans, cnt = 1, s, tmp, sum, k, p, cnt_;
map<LL, bool> v;
bool flag;
vector<LL> e;
void pr(bool pr) {
cout << (pr ? "Yes" : "No") << '\n';
}
int main() {
cin >> n >> x >> m, v[x] = 1, e.push_back(x);
for (tmp = x * x % m; !v[tmp]; v[tmp] = 1, e.push_back(tmp), (tmp *= tmp) %= m) {
}
for (LL o : e) {
if (flag == 0 && o != tmp) {
ans += o, cnt_++;
if (cnt_ == n) {
cout << ans << '\n';
return 0;
}
} else if (flag == 0 && o == tmp) {
flag = 1, sum += o;
} else if (flag) {
sum += o, cnt++;
}
}
ans += sum * ((n - cnt_) / cnt);
for (LL i = cnt_ + 1 + (n - cnt_) / cnt * cnt; i <= n; i++, (tmp *= tmp) %= m) {
ans += tmp;
}
cout << ans << '\n';
return 0;
}