xxxxxxxxxx
template <auto& Mod>
struct modint {
public:
using value_type = std::decay_t<decltype(Mod)>;
modint() : val() {}
modint(const value_type v) : val(v) { clamp_large(); }
[[nodiscard]] modint operator+(const modint rhs) const {
return modint(val + rhs.val, true);
}
[[nodiscard]] modint operator-(const modint rhs) const {
return modint(val - rhs.val, true);
}
[[nodiscard]] modint operator*(const modint rhs) const {
return modint(val * rhs.val);
}
modint& operator=(const modint rhs) {
val = rhs.val;
return *this;
}
modint& operator+=(const modint rhs) {
val += rhs.val;
clamp_small();
return *this;
}
modint& operator-=(const modint rhs) {
val -= rhs.val;
clamp_small();
return *this;
}
modint& operator*=(const modint rhs) {
val *= rhs.val;
clamp_large();
return *this;
}
[[nodiscard]] modint pow(int index) const {
modint res = 1, base = *this;
while (index) {
if (index & 1) {
res *= base;
}
base *= base;
index >>= 1;
}
return res;
}
[[nodiscard]] operator value_type() const {
return val;
}
friend std::istream& operator>>(std::istream& is, modint& x) {
return is >> x.val;
}
friend std::ostream& operator<<(std::ostream& os, const modint x) {
return os << x.val;
}
private:
value_type val;
void clamp_small() {
if (val < 0) {
val += Mod;
} else if (val >= Mod) {
val -= Mod;
}
}
void clamp_large() {
val %= Mod;
}
modint(const value_type v, bool) : val(v) { clamp_small(); }
};
int main() {
static int M;
using mint = modint<M>;
mint a, b;
int c, k;
std::cin >> M >> a >> b >> c >> k;
--k;
std::vector index(M, std::vector<int>(M, -1));
std::vector<mint> terms;
terms.reserve(M * M + 1);
terms.emplace_back(a);
terms.emplace_back(b);
int loop_starts_at, loop_length;
int calculated = 2;
while (true) {
if (index[terms[calculated - 2]][terms[calculated - 1]] != -1) {
loop_starts_at = index[terms[calculated - 2]][terms[calculated - 1]] - 2;
loop_length = calculated - loop_starts_at - 2;
terms.pop_back();
terms.pop_back();
break;
} else {
index[terms[calculated - 2]][terms[calculated - 1]] = calculated;
terms.emplace_back(terms[calculated - 2].pow(c) + terms[calculated - 1].pow(c));
++calculated;
}
}
if (k > loop_starts_at) {
k -= loop_starts_at;
k %= loop_length;
k += loop_starts_at;
}
std::cout << terms[k] << '\n';
}
提出日時 | |
ユーザー | ![]() |
言語 | C++ (GCC 9.3.0) |
結果 | AC |
実行時間 | 151 ms |
メモリ | 1528528 kb |
テストケース名 | 結果 | 実行時間 | メモリ |
---|---|---|---|
hard_01 | AC | 137 ms | 1528528 kb |
hard_02 | AC | 142 ms | 1528528 kb |
hard_03 | AC | 141 ms | 1528528 kb |
hard_04 | AC | 105 ms | 1528528 kb |
hard_05 | AC | 115 ms | 1528528 kb |
hard_06 | AC | 130 ms | 1528528 kb |
hard_07 | AC | 151 ms | 1528528 kb |
hard_08 | AC | 135 ms | 1528528 kb |
hard_09 | AC | 136 ms | 1528528 kb |
hard_10 | AC | 127 ms | 1528528 kb |
normal_01 | AC | 4 ms | 1528528 kb |
normal_02 | AC | 4 ms | 1528528 kb |
normal_03 | AC | 4 ms | 1528528 kb |
normal_04 | AC | 4 ms | 1528528 kb |
normal_05 | AC | 4 ms | 1528528 kb |
normal_06 | AC | 4 ms | 1528528 kb |
normal_07 | AC | 4 ms | 1528528 kb |
normal_08 | AC | 4 ms | 1528528 kb |
normal_09 | AC | 9 ms | 1528528 kb |
normal_10 | AC | 4 ms | 1528528 kb |
normal_11 | AC | 4 ms | 1528528 kb |
normal_12 | AC | 4 ms | 1528528 kb |
normal_13 | AC | 6 ms | 1528528 kb |
normal_14 | AC | 6 ms | 1528528 kb |
normal_15 | AC | 5 ms | 1528528 kb |
normal_16 | AC | 4 ms | 1528528 kb |
normal_17 | AC | 4 ms | 1528528 kb |
normal_18 | AC | 4 ms | 1528528 kb |
normal_19 | AC | 4 ms | 1528528 kb |
normal_20 | AC | 4 ms | 1528528 kb |
normal_21 | AC | 4 ms | 1528528 kb |
normal_22 | AC | 4 ms | 1528528 kb |
normal_23 | AC | 4 ms | 1528528 kb |
normal_24 | AC | 4 ms | 1528528 kb |
normal_25 | AC | 4 ms | 1528528 kb |
normal_26 | AC | 4 ms | 1528528 kb |
normal_27 | AC | 4 ms | 1528528 kb |
normal_28 | AC | 4 ms | 1528528 kb |
normal_29 | AC | 29 ms | 1528528 kb |
normal_30 | AC | 4 ms | 1528528 kb |
sample_01 | AC | 16 ms | 1528528 kb |
sample_02 | AC | 4 ms | 1528528 kb |