[ABC270G] Sequence in mod P
https://www.luogu.com.cn/problem/AT_abc270_g
考虑暴力推式子:
以此类推,有:
感觉用等比数列求和搞搞:
代入,有:
然后主体就是 BSGS 了,剩下有几个 corner case:
- 若 那么输出 。
- 若 判断 是否等于 。
- 若 那么 ,可以推出 。
鉴定为玩 corner case 玩的。
代码再写。
upd 2024.5.2 22:40:
一个是 __gnu_pbds :: gp_hash_table 的,230 ms。一个是 std :: map 的,848 ms。肉眼可见的效率差距。
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/rope>
bool Mbe;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
std :: mt19937 rnd (std :: chrono :: steady_clock :: now ().time_since_epoch ().count ());
template < typename T > T gen (const T l, const T r) {
return rnd () % (r - l + 1) + l;
}
const double pi = std :: acos (-1);
template < typename T > inline void read (T &val) {
T x = 0;
bool f = false;
char c = std :: getchar ();
for ( ; c < '0' || c > '9' ; c = std :: getchar ()) {
f |= (c == '-');
}
for ( ; c >= '0' && c <= '9' ; c = std :: getchar ()) {
x = (x << 1) + (x << 3) + (c & 15);
}
val = (!f) ? (x) : (-x);
return ;
}
#define debug(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
const i64 hashmod1 = 2147483579, hashmod2 = 1610612741, hashmod3 = 805306457;
template < typename T, typename I > void chkmin (T &p, I q) {
T q_ = q;
p = std :: min (p, q_);
return ;
}
template < typename T, typename I > void chkmax (T &p, I q) {
T q_ = q;
p = std :: max (p, q_);
return ;
}
i64 pow_mod (i64 a, i64 b, i64 p) {
i64 res = 1;
for (; b > 0; b >>= 1, a = a * a % p) {
if (b & 1) res = res * a % p;
}
return res;
}
i64 solve (i64 base, i64 goal, i64 mod) {
__gnu_pbds :: gp_hash_table < i64, i64 > mp;
mp.clear ();
i64 t = static_cast < i64 > (sqrt (mod)) + 5;
i64 cur = 1;
for (int i = 0;i <= t; ++ i) {
if (cur == goal) return i;
if (i < t) cur = cur * base % mod;
}
i64 cur_ = 1;
for (int i = 1;i <= t; ++ i) {
cur_ = cur_ * cur % mod;
if (mp.find (cur_) == mp.end ()) {
mp[cur_] = static_cast < i64 > (i);
}
}
i64 inv = pow_mod (base, mod - 2, mod);
cur = 1;
i64 ans = -1;
for (i64 i = 0;i < t; ++ i) {
if (mp.find (goal * cur % mod) != mp.end ()) {
i64 x = mp[goal * cur % mod];
if (ans == -1) {
ans = x * t + i;
}
else chkmin (ans, x * t + i);
}
cur = cur * inv % mod;
}
return ans;
}
void solve () {
i64 mod, a, b, s, g;
read (mod), read (a), read (b), read (s), read (g);
if (s == g) printf ("0\n");
else if (!a) {
if (b == g) printf ("1\n");
else printf ("-1\n");
}
else if (a == 1) {
if (!b) printf ("-1\n");
else printf ("%lld\n", (g + mod - s) % mod * pow_mod (b, mod - 2, mod) % mod);
}
else {
i64 b_ = b * pow_mod ((a + mod - 1) % mod, mod - 2, mod) % mod;
i64 goal = (g + b_) % mod;
goal = goal * pow_mod ((s + b_) % mod, mod - 2, mod) % mod;
printf ("%lld\n", solve (a, goal, mod));
}
return ;
}
bool Med;
signed main () {
debug ("%.8lf MB\n", (&Mbe - &Med) / 1048576.0);
int _;
read (_);
while (_ --) {
solve ();
}
debug ("%.8lf ms\n", 1e3 * clock () / CLOCKS_PER_SEC);
return 0;
}
// g++ "[ABC270G] Sequence in mod P.cpp" -o ABC270G -std=c++14 -O2 -Wall -Wextra -Wshadow
// check! no inline! (except fastIO)std :: map 版本:https://atcoder.jp/contests/abc270/submissions/53030277