結果
| 問題 |
No.2993 冪乗乗 mod 冪乗
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-12-19 22:07:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
CE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 19,120 bytes |
| コンパイル時間 | 6,143 ms |
| コンパイル使用メモリ | 492,532 KB |
| 最終ジャッジ日時 | 2025-02-26 15:40:59 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
In file included from /usr/include/c++/13/string:43,
from /usr/include/c++/13/bitset:52,
from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
from main.cpp:13:
/usr/include/c++/13/bits/allocator.h: In destructor ‘std::__cxx11::basic_string<char>::_Alloc_hider::~_Alloc_hider()’:
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to ‘always_inline’ ‘std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = char]’: target specific option mismatch
184 | ~allocator() _GLIBCXX_NOTHROW { }
| ^
In file included from /usr/include/c++/13/string:54:
/usr/include/c++/13/bits/basic_string.h:181:14: note: called from here
181 | struct _Alloc_hider : allocator_type // TODO check __is_final
| ^~~~~~~~~~~~
ソースコード
// QCFium 法
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
using mint = modint998244353;
//using mint = static_modint<1000000007>;
//using mint = modint; // mint::set_mod(m);
string mint_to_frac(mint x, int v_max = 31595) {
repi(dnm, 1, v_max) {
int num = (x * dnm).val();
if (num == 0) {
return "0";
}
if (num <= v_max) {
if (dnm == 1) return to_string(num);
return to_string(num) + "/" + to_string(dnm);
}
if (mint::mod() - num <= v_max) {
if (dnm == 1) return "-" + to_string(mint::mod() - num);
return "-" + to_string(mint::mod() - num) + "/" + to_string(dnm);
}
}
return to_string(x.val());
}
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
#ifdef _MSC_VER
inline ostream& operator<<(ostream& os, const mint& x) { os << mint_to_frac(x); return os; }
#else
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
#endif
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif
//【素因数分解(複数)】
/*
* Osa_k(int n) : O(n log(log n))
* n 以下の自然数を高速に素因数分解する準備を行う.
*
* bool primeQ(int i) : O(1)
* i が素数かを返す.
*
* map<int, int> factor_integer(int i) : O(log n)
* i の素因数分解結果を返す.
*
* vi divisors(int i) : O(σ(n))
* i の約数の昇順リストを返す.
*
* int euler_phi(int i) : O(log n)
* オイラーのトーシェント関数 φ(i) の値を返す.
*
* vi unique_prime_factors(int i) : O(log n)
* i の重複を除去した素因数のリストを返す.
*
* int radical(int i) : O(log n)
* i の根基(重複を除去した素因数の積)を返す.
*
* vi prime_power_decomposition(int i) : O(log n)
* i を素数冪の積に分解したリストを返す.
*/
struct Osa_k {
int n;
// gpf[i] : i を割り切る最大の素数
vi gpf;
// n 以下の自然数を高速に素因数分解する準備を行う.
Osa_k(int n_) : n(n_), gpf(n + 1) {
// verify : https://yukicoder.me/problems/no/2207
iota(all(gpf), 0);
for (int p = 2; p * p <= n; p++) {
if (gpf[p] != p) continue;
// ここは d の最大性のため p^2 からにはできない.
for (int i = p; i <= n; i += p) gpf[i] = p;
}
}
Osa_k() : n(0) {}
// i が素数かを返す.
bool primeQ(int i) {
// verify : https://yukicoder.me/problems/no/1396
Assert(i <= n);
return i >= 2 && gpf[i] == i;
}
// i の素因数分解結果を返す.
map<int, int> factor_integer(int i) const {
// verify : https://yukicoder.me/problems/no/2207
Assert(i <= n);
map<int, int> pps;
while (i > 1) {
pps[gpf[i]]++;
i /= gpf[i];
}
return pps;
}
// i の約数の昇順リストを返す.
vi divisors(int i) const {
// verify : https://atcoder.jp/contests/abc368/tasks/abc368_f
Assert(i <= n);
vi divs{ 1 };
auto pps = factor_integer(i);
for (auto [p, d] : pps) {
vi powp(d);
powp[0] = p;
rep(i, d - 1) powp[i + 1] = powp[i] * p;
int m = sz(divs);
repir(j, m - 1, 0) rep(i, d) divs.push_back(divs[j] * powp[i]);
}
sort(all(divs)); // 不要なら削除可能
return divs;
}
// オイラーのトーシェント関数 φ(i) の値を返す.
int euler_phi(int i) {
// verify : https://yukicoder.me/problems/no/2849
Assert(i <= n);
int phi = 1; int pp = INF;
while (i > 1) {
int p = gpf[i];
phi *= (p == pp ? p : p - 1);
pp = p;
i /= p;
}
return phi;
}
// i の重複を除去した素因数のリストを返す.
vi unique_prime_factors(int i) const {
Assert(i <= n);
vi res; int pp = INF;
while (i > 1) {
int p = gpf[i];
if (p != pp) res.push_back(p);
pp = p;
i /= p;
}
return res;
}
// i の根基(重複を除去した素因数の積)を返す.
int radical(int i) const {
// verify : https://projecteuler.net/problem=518
Assert(i <= n);
int rad = 1; int pp = INF;
while (i > 1) {
int p = gpf[i];
if (p != pp) rad *= p;
pp = p;
i /= p;
}
return rad;
}
// i を素数冪の積に分解したリストを返す.
vi prime_power_decomposition(int i) const {
// verify : https://projecteuler.net/problem=407
Assert(i <= n);
vi res; int pp = INF;
while (i > 1) {
int p = gpf[i];
if (p != pp) res.push_back(p);
else res.back() *= p;
pp = p;
i /= p;
}
return res;
}
};
Osa_k O;
#include <boost/multiprecision/cpp_int.hpp>
using Bint = boost::multiprecision::cpp_int;
//【累乗】O(log n)
Bint pow_bint_mod(const Bint& x, Bint n, Bint MOD) {
Bint res(1), pow2 = x;
while (n > 0) {
if (n & 1) {
res *= pow2;
res %= MOD;
}
pow2 *= pow2;
pow2 %= MOD;
n /= 2;
}
return res;
}
int n_max = 0; ll B_max, N_max, M_max;
// WA7() がとても惜しかった.カスの解法に走ったの勿体なさすぎる・・・
ll TLE(ll B, ll N, ll M) {
int n = 0;
auto pps = O.factor_integer((int)B);
for (auto [p, e] : pps) {
int ord = -1;
vi divs = O.divisors(p - 1);
repe(d, divs) if (pow_mod(M, d, p) == 1) {
ord = d;
break;
}
if (ord == -1) return -1;
auto pps2 = O.factor_integer(ord);
for (auto [p2, e2] : pps2) {
if (!pps.count(p2)) return -1;
int e = pps[p2];
chmax(n, (e2 + e - 1) / e);
}
}
// ここがダメだった.
// n++;
chmax(n, (int)N);
// dump("n:", n);
//if (chmax(n_max, n)) {
// B_max = B, N_max = N, M_max = M;
//}
ll MOD = powi(B, (int)N);
// dump(MOD);
Bint Bn = 1;
rep(hoge, n) Bn *= B;
// dump(Bn);
Bint num = pow_bint_mod(M, Bn, MOD * Bn) - 1;
// dump(num);
ll a = smod((ll)(num / Bn), MOD);
// if (a != 0) {
// if (8 <= n && n <= 9) {
// dump("n:", n, "a:", a, "B N M:", B, N, M);
// }
// }
return a;
}
/*
やばいケースの一例:
n: 12 a: 73734 B N M: 221202 1 938642470387890019
n: 12 a: 73734 B N M: 663606 1 569887344029755735
n: 13 a: 491532 B N M: 737298 1 652303410774024499
n: 12 a: 147468 B N M: 221202 1 144724915998203809
n: 11 a: 491560 B N M: 614450 1 332370521262624211
n: 12 a: 294936 B N M: 663606 1 602909229475107151
n: 11 a: 184330 B N M: 921650 1 615098227504156881
n: 11 a: 110598 B N M: 995382 1 824077417941371765
n: 13 a: 245766 B N M: 737298 1 87066598109952973
n: 10 a: 221202 B N M: 663606 1 464220951965924615
n: 10 a: 460830 B N M: 829494 1 131118901202347681
n: 11 a: 884784 B N M: 995382 1 894449835844946105
n: 13 a: 491532 B N M: 737298 1 813782825271406865
n: 12 a: 147468 B N M: 221202 1 700059672256021501
n: 11 a: 491560 B N M: 614450 1 710782717101345239
n: 10 a: 221202 B N M: 663606 1 605086375262543389
n: 10 a: 614440 B N M: 768050 1 394351613178697109
n: 11 a: 221196 B N M: 331794 1 83978138233245947
n: 11 a: 663588 B N M: 995382 1 396400066665083675
n: 11 a: 147468 B N M: 221202 1 663033641839565567
n: 10 a: 491560 B N M: 614450 1 687439741701148489
n: 12 a: 368670 B N M: 663606 1 239196527398219495
*/
// 毎回 16 乗しないといけないケース
// 2024/12/18 21:30 時点での writer 解以外の全てを落とせる.
void make_worst_case() {
mt19937_64 mt(0);
uniform_int_distribution<ll> rnd(0, (ll)1e18);
ll B0 = 2 * 65537;
auto check = [&](ll B, ll N, ll M) {
auto pps = O.factor_integer((int)B);
int n = 0;
for (auto [p, e] : pps) {
int ord = -1;
vi divs = O.divisors(p - 1);
repe(d, divs) if (pow_mod(M, d, p) == 1) {
ord = d;
break;
}
// dump("ord:", ord);
if (ord == -1) return false;
auto pps2 = O.factor_integer(ord);
// dump("pps2:", pps2);
for (auto [p2, e2] : pps2) {
if (!pps.count(p2)) return false;
int e = pps[p2];
chmax(n, (e2 + e - 1) / e);
}
}
return n >= 16;
};
int T = 100000;
// T = 10;
cout << T << endl;
while (T > 0) {
// dump("---------- T:", T);
ll B = B0 * (rnd(mt) % 7 + 1);
ll M = rnd(mt);
ll N = rnd(mt) % 2;
if (!check(B, N, M)) continue;
cout << B << " " << N << " " << M << endl;
T--;
}
exit(0);
}
void make_worst_case2() {
mt19937_64 mt(0);
uniform_int_distribution<ll> rnd(0, (ll)1e18);
auto check = [&](ll B, ll N, ll M, int n0) {
auto pps = O.factor_integer((int)B);
int n = 0;
for (auto [p, e] : pps) {
int ord = -1;
vi divs = O.divisors(p - 1);
repe(d, divs) if (pow_mod(M, d, p) == 1) {
ord = d;
break;
}
if (ord == -1) return false;
auto pps2 = O.factor_integer(ord);
for (auto [p2, e2] : pps2) {
if (!pps.count(p2)) return false;
int e = pps[p2];
chmax(n, (e2 + e - 1) / e);
}
}
return n >= n0;
};
int T = 100000;
cout << T << endl;
repir(n0, 20, 1) { // n0=3 くらいまでいってしまう.
dump("-------- n0:", n0, "--------");
repi(p, 2, (int)1e6) {
if (!O.primeQ(p)) continue;
ll p_pow = powi(p, n0);
if (p_pow + 1 > (ll)1e6) break;
repi(k, 1, (int)1e6) {
ll B0 = k * p_pow + 1;
if (B0 * p > (ll)1e6) break;
if (!O.primeQ((int)B0)) continue;
dump("p:", p, "k:", k, "k p^n0 + 1:", B0);
B0 *= p;
repi(i, 1, (int)1e6) {
ll B = B0 * i;
if (B > (ll)1e6) break;
repi(N, 0, 1) {
ll M = rnd(mt);
rep(hoge, 10) {
if (check(B, N, M, n0)) break;
M = rnd(mt);
}
if (!check(B, N, M, n0)) continue;
cout << B << " " << N << " " << M << endl;
T--;
if (T == 0) exit(0);
}
}
}
}
}
}
// 毎回 13 乗しないといけなくて,しかも a!=0 のケース
// 2024/12/19 17:00 時点での writer 解以外の全てを落とせる.
void make_worst_case3() {
mt19937_64 mt(0);
uniform_int_distribution<ll> rnd(0, (ll)1e18);
ll B = 737298;
auto check = [&](ll B, ll N, ll M) {
auto pps = O.factor_integer((int)B);
int n = 0;
for (auto [p, e] : pps) {
int ord = -1;
vi divs = O.divisors(p - 1);
repe(d, divs) if (pow_mod(M, d, p) == 1) {
ord = d;
break;
}
if (ord == -1) return false;
auto pps2 = O.factor_integer(ord);
for (auto [p2, e2] : pps2) {
if (!pps.count(p2)) return false;
int e = pps[p2];
chmax(n, (e2 + e - 1) / e);
}
}
return n >= 13;
};
int T = 100000;
// T = 10;
cout << T << endl;
while (T > 0) {
// dump("---------- T:", T);
ll M = rnd(mt);
ll N = rnd(mt) % 2;
if (!check(B, N, M)) continue;
cout << B << " " << N << " " << M << endl;
T--;
}
exit(0);
}
void make_worst_case_all_same() {
int T = 100000;
cout << T << endl;
rep(t, T) cout << "989450 1 265534344666945059" << endl; // n=8
// rep(t, T) cout << "944808 1 351481058622028685" << endl; // n=9
exit(0);
}
map<tuple<ll, ll, int, ll>, ll> memo;
ll solve(ll B, ll N, ll M) {
int n = 0;
auto pps = O.factor_integer((int)B);
for (auto [p, e] : pps) {
int ord = -1;
vi divs = O.divisors(p - 1);
repe(d, divs) if (pow_mod(M, d, p) == 1) {
ord = d;
break;
}
if (ord == -1) return -1;
auto pps2 = O.factor_integer(ord);
for (auto [p2, e2] : pps2) {
if (!pps.count(p2)) return -1;
int e = pps[p2];
chmax(n, (e2 + e - 1) / e);
}
}
chmax(n, (int)N + 1);
dump("n:", n);
if (N == 0) return 0;
vl rs, ms;
for (auto [p, e] : pps) {
dump("- - - - - p:", p, "e:", e, "- - - - -");
ll pe = powi(p, e);
ll k = B / pe;
ll m = powi(pe, (int)N);
//ll phi = (p - 1) * powi(p, e * N - 1);
//ll kn = pow_mod(k, n, phi);
//ll r = pow_mod(M, kn, m);
ll r = M;
rep(hoge, n) r = pow_mod(r, k, (int)m);
rs.push_back(r);
ms.push_back(m);
}
dump(rs); dump(ms);
auto [y, z] = crt(rs, ms);
dump("y:", y);
ll MOD = powi(B, (int)N);
if (memo.count({ B, N, n, y })) return memo[{B, N, n, y}];
Bint Bn = 1;
rep(hoge, n) Bn *= B;
Bint num = pow_bint_mod(M, Bn, MOD * Bn) - 1;
ll a = smod((ll)(num / Bn), MOD);
memo[{B, N, n, y}] = a;
return a;
}
/*
------------------------------
34 2 5
952
y: 205
952
------------------------------
34 2 25
748
y: 205
*/
//【桁の数の取得】O(log n)(の改変)
/*
* n を b 進表記したときの桁の数字を上位桁から順に並べたリストを返す.
*
* 制約:b ≧ 2
*/
vi integer_digits(Bint n, ll b = 10) {
// verify : https://atcoder.jp/contests/abc105/tasks/abc105_c
Assert(abs(b) >= 2);
// n = 0 の場合の例外処理
if (n == 0) return vi{ 0 };
// mod |b| を取れば最下位桁から順に決定していく.
vi ds;
while (n != 0) {
int d = (int)(n % b);
// int d = (int)smod(n, abs(b)); // 負数の可能性があるならこっち
ds.push_back(d);
n = (n - d) / b;
}
// 上位桁から順になるように並べ直す.
reverse(all(ds));
return ds;
}
ll solve2(ll B, ll N, ll M) {
int n = 0;
auto pps = O.factor_integer((int)B);
for (auto [p, e] : pps) {
int ord = -1;
vi divs = O.divisors(p - 1);
repe(d, divs) if (pow_mod(M, d, p) == 1) {
ord = d;
break;
}
if (ord == -1) return -1;
auto pps2 = O.factor_integer(ord);
for (auto [p2, e2] : pps2) {
if (!pps.count(p2)) return -1;
int e = pps[p2];
chmax(n, (e2 + e - 1) / e);
}
}
chmax(n, (int)N);
if (N == 0) return 0;
vl rs, ms;
for (auto [p, e] : pps) {
dump("- - - - - p:", p, "e:", e, "- - - - -");
ll pe = powi(p, e);
ll k = B / pe;
ll m = powi(pe, (int)N);
//ll phi = (p - 1) * powi(p, e * N - 1);
//ll kn = pow_mod(k, n, phi);
//ll r = pow_mod(M, kn, m);
ll Mkn = M;
rep(hoge, n) Mkn = pow_mod(Mkn, k, (int)m);
Bint NUM = Mkn;
Bint DNM = 1;
Bint MOD = m;
rep(hoge, e * n) {
dump("id:", integer_digits(NUM, p));
MOD *= p;
Bint nNUM = pow_bint_mod(NUM, p, MOD);
DNM *= p;
if ((nNUM - 1) == (NUM - 1) * p) {
NUM = nNUM;
break;
}
NUM = nNUM;
}
ll r = (ll)((NUM - 1) / DNM);
rs.push_back(r);
ms.push_back(m);
}
auto [y, z] = crt(rs, ms);
dump("y:", y);
return y;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
O = Osa_k((int)1e6);
// make_worst_case_all_same();
int t = 1;
cin >> t; // マルチテストケースの場合
while (t--) {
dump("------------------------------");
ll B, N, M;
cin >> B >> N >> M;
// auto res_TLE = TLE(B, N, M); dump(res_TLE); //dump("----");
auto res = solve(B, N, M);
// assert(res == res_TLE);
cout << res << "\n";
}
}