1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
#include <bits/stdc++.h> #include <bits/stdtr1c++.h> #include <ext/algorithm> #include <ext/rb_tree> #include <ext/rope> #include <ext/vstring.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> template <class Tp> using vc = std::vector<Tp>; namespace NdVector { template <size_t N, class Tp> struct ndvector: public std::vector<ndvector<N - 1, Tp>> { static_assert(N > 0, "N should be positive"); using base_tp = ndvector<N - 1, Tp>; using base = std::vector<base_tp>; using self = ndvector<N, Tp>; template <class T, typename... Ts> ndvector(T &&n, Ts &&...args): base(n, base_tp(args...)) {} constexpr size_t dim() const { return N; } template <class T> void fill(T &&x) { for (auto &i : *this) i.fill(x); } }; template <class Tp> struct ndvector<1, Tp>: public std::vector<Tp> { using base = std::vector<Tp>; using self = ndvector<1, Tp>; template <class T> ndvector(T &&n): base(n) {} constexpr size_t dim() const { return 1; } template <class T> void fill(T &&x) { std::fill(this->begin(), this->end(), x); } }; } struct CustomHash { static constexpr uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } static constexpr size_t append(size_t x, size_t y) { return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1)); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } template <class Tp, class Up> size_t operator()(std::pair<Tp, Up> const &p) const { return append((*this)(p.first), (*this)(p.second)); } template <typename... Ts> size_t operator()(std::tuple<Ts...> const &tp) const { size_t ret = 0; std::apply( [&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); }, tp); return ret; } template < class Tp, std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()), typename Tp::iterator>::value && std::is_same<decltype(std::declval<Tp>().end()), typename Tp::iterator>::value> * = nullptr> size_t operator()(Tp const &tp) const { size_t ret = 0; for (auto &&i : tp) ret = append(ret, (*this)(i)); return ret; } }; using u32 = uint32_t; using i64 = int64_t; #define for_(i, l, r, vars...) \ for (std::make_signed_t<decltype(l + r)> i = (l), i##end = (r), ##vars; \ i <= i##end; \ ++i) #define for_step_(i, l, r, s, vars...) \ for (std::make_signed_t<decltype(l + r)> i = (l), i##end = (r), ##vars; \ i <= i##end; \ i += s) template <class Tp> constexpr auto chkmin(Tp &a, Tp b) -> bool { return b < a ? a = b, true : false; } template <class Tp> constexpr auto chkmax(Tp &a, Tp b) -> bool { return a < b ? a = b, true : false; } template <class Tp> constexpr auto ispow2(Tp i) -> bool { return i && (i & -i) == i; } #define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>> namespace tuple_detail_ { template <std::size_t Begin, class Tuple, std::size_t... Is> constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) { return std::make_tuple(std::get<Is + Begin>(t)...); } template <class Tuple, class BinOp, std::size_t... Is> constexpr auto apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) { return std::make_tuple( std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...); } } template <std::size_t Begin, std::size_t Len, class Tuple> constexpr auto subtuple(Tuple &&t) { static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) && Begin + Len <= TPL_SIZE_(Tuple), "Out of range"); return tuple_detail_::subtuple_impl_<Begin>(t, std::make_index_sequence<Len>()); } template <std::size_t Pos, class Tp, class Tuple> constexpr auto tuple_push(Tp &&v, Tuple &&t) { static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple"); return std::tuple_cat(subtuple<0, Pos>(t), std::make_tuple(v), subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t)); } template <class Tp, class Tuple> constexpr auto tuple_push_front(Tp &&v, Tuple &&t) { return tuple_push<0>(v, t); } template <class Tp, class Tuple> constexpr auto tuple_push_back(Tp &&v, Tuple &&t) { return tuple_push<TPL_SIZE_(Tuple)>(v, t); } template <std::size_t Pos, class Tuple> constexpr auto tuple_pop(Tuple &&t) { static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple"); return std::tuple_cat(subtuple<0, Pos>(t), subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t)); } template <class Tuple> constexpr auto tuple_pop_front(Tuple &&t) { return tuple_pop<0>(t); } template <class Tuple> constexpr auto tuple_pop_back(Tuple &&t) { return tuple_pop<TPL_SIZE_(Tuple) - 1>(t); } template <class Tuple, class BinOp> constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) { return tuple_detail_::apply2_impl_( f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>()); } #define OO_PTEQ_(op) \ template <class Tp, class Up> \ constexpr auto operator op(std::pair<Tp, Up> lhs, \ const std::pair<Tp, Up> &rhs) { \ return std::pair<Tp, Up>{lhs.first op rhs.first, \ lhs.second op rhs.second}; \ } \ template <class... Ts> \ constexpr auto operator op(std::tuple<Ts...> const &lhs, \ std::tuple<Ts...> const &rhs) { \ return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \ } \ template <class Tp, class Up> \ constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \ const std::pair<Tp, Up> &rhs) { \ lhs.first op## = rhs.first; \ lhs.second op## = rhs.second; \ return lhs; \ } \ template <class... Ts> \ constexpr auto operator op##=(std::tuple<Ts...> &lhs, \ const std::tuple<Ts...> &rhs) { \ return lhs = lhs op rhs; \ } OO_PTEQ_(+) OO_PTEQ_(-) OO_PTEQ_(*) OO_PTEQ_(/) OO_PTEQ_(%) OO_PTEQ_(&) OO_PTEQ_(|) OO_PTEQ_(^) OO_PTEQ_(<<) OO_PTEQ_(>>) #undef OO_PTEQ_ #undef TPL_SIZE_ template <class Tp, class Up> std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) { return is >> p.first >> p.second; } template <class Tp, class Up> std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) { return os << p.first << ' ' << p.second; } template <typename... Ts> std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) { std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p); return is; } template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) { std::apply( [&](Ts const &...targs) { std::size_t n{0}; ((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...); }, p); return os; } template < class Ch, class Tr, class Ct, std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()), typename Ct::iterator>::value && std::is_same<decltype(std::declval<Ct>().end()), typename Ct::iterator>::value> * = nullptr> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Ct &x) { if (x.begin() == x.end()) return os; for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' '; os << x.back(); return os; } const u32 OFFSET = 5; const u32 N = 1e6 + OFFSET; const i64 MOD = 32465177; using namespace std; namespace MODINT { constexpr int64_t safe_mod(int64_t x, int64_t m) { return (x %= m) < 0 ? x + m : x; } constexpr std::pair<int64_t, int64_t> invgcd(int64_t a, int64_t b) { if ((a = safe_mod(a, b)) == 0) return {b, 0}; int64_t s = b, m0 = 0; for (int64_t q = 0, _ = 0, m1 = 1; a;) { _ = s - a * (q = s / a); s = a; a = _; _ = m0 - m1 * q; m0 = m1; m1 = _; } return {s, m0 + (m0 < 0 ? b / s : 0)}; } template <uint32_t MOD> class Mint { static_assert(MOD >= 1); using self = Mint<MOD>;
protected: uint32_t v_;
public: constexpr static uint32_t mod() { return MOD; } constexpr static self raw(uint32_t v) { self x; x.v_ = v; return x; } constexpr Mint(): v_(0) {} template <class T, std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value> * = nullptr> constexpr Mint(T v): Mint() { int64_t x = (int64_t)(v % (int64_t)mod()); v_ = (uint32_t)(x + (x < 0 ? mod() : 0)); } template <class T, std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value> * = nullptr> constexpr Mint(T v): v_((uint32_t)(v % mod())) {} friend std::istream &operator>>(std::istream &is, self &x) { int64_t xx; is >> xx; xx %= mod(); x.v_ = (uint32_t)(xx + (xx < 0 ? mod() : 0)); return is; } friend std::ostream &operator<<(std::ostream &os, const self &x) { return os << x.v_; } constexpr const uint32_t &val() const { return v_; } constexpr explicit operator uint32_t() const { return val(); } constexpr uint32_t &data() { return v_; } constexpr self &operator++() { if (++v_ == mod()) v_ = 0; return *this; } constexpr self &operator--() { if (!v_) v_ = mod(); --v_; return *this; } constexpr self operator++(int) { self result = *this; ++*this; return result; } constexpr self operator--(int) { self result = *this; --*this; return result; } constexpr self &operator+=(const self &rhs) { v_ += rhs.v_; if (v_ >= mod()) v_ -= mod(); return *this; } constexpr self &operator-=(const self &rhs) { v_ -= rhs.v_; if (v_ >= mod()) v_ += mod(); return *this; } constexpr self &operator*=(const self &rhs) { v_ = (uint32_t)((uint64_t)v_ * rhs.v_ % mod()); return *this; } constexpr self &operator/=(const self &rhs) { return *this = *this * inverse(rhs); } constexpr self operator+() const { return *this; } constexpr self operator-() const { return self() - *this; } constexpr friend self pow(self x, uint64_t y) { self res(1); for (; y; y >>= 1, x *= x) if (y & 1) res *= x; return res; } constexpr friend self inverse(const self &x) { auto &&_ = invgcd(x.v_, self::mod()); if (_.first != 1) throw std::runtime_error("Inverse not exist"); return _.second; } constexpr friend self operator+(self lhs, const self &rhs) { return lhs += rhs; } constexpr friend self operator-(self lhs, const self &rhs) { return lhs -= rhs; } constexpr friend self operator*(self lhs, const self &rhs) { return lhs *= rhs; } constexpr friend self operator/(self lhs, const self &rhs) { return lhs /= rhs; } constexpr friend bool operator==(const self &lhs, const self &rhs) { return lhs.v_ == rhs.v_; } constexpr friend bool operator!=(const self &lhs, const self &rhs) { return lhs.v_ != rhs.v_; } }; } using mint = MODINT::Mint<MOD>; using phimint = MODINT::Mint<MOD - 1>; namespace primes { vc<u32> vis; vc<u32> prime; vc<phimint> h, mu; void init_prime(u32 n) { h.resize(n); mu.resize(n); vis.resize(n); mu[1] = h[1] = 1; for (i64 i = 2; i < n; ++i) { if (!vis[i]) { vis[i] = i; prime.push_back(i); h[i] = 1 - i; mu[i] = -1; } for (auto &&j : prime) { i64 x = i * j; if (x >= n) break; vis[x] = j; if (i % j) { h[x] = h[i] * h[j]; mu[x] = -mu[i]; } else { h[x] = h[i]; break; } } } } } using primes::h, primes::mu; constexpr mint qpow(mint a, phimint b_) { mint res(1); for (i64 b = b_.val(); b; b >>= 1, a *= a) if (b & 1) res *= a; return res; } vc<phimint> sumhl; vc<mint> hh, pdhdkd; void init_(u32 n) { sumhl.resize(n); sumhl[1] = h[1]; for_(i, 2, n - 1) sumhl[i] = sumhl[i - 1] + i * h[i]; hh.resize(n); hh[1] = 1; for_(i, 2, n - 1) hh[i] = hh[i - 1] * qpow(i, i); vc<mint> dhd(n); dhd[1] = 1; for_(i, 2, n - 1) dhd[i] = qpow(i, h[i]); vc<mint> k(n, 1); for_(i, 2, n - 1) { mint _ = qpow(i, i * mu[i]); for_step_(j, i, n - 1, i) k[j] *= _; } pdhdkd.resize(n); pdhdkd[0] = 1; for_(i, 1, n - 1) pdhdkd[i] = pdhdkd[i - 1] * qpow(dhd[i] * k[i], i); } constexpr phimint s(i64 n) { return n * (n + 1) / 2; } mint calc(i64 n, i64 m) { if (n > m) swap(n, m); mint _ = 1; for (i64 l = 1, r; l <= n; l = r + 1) { r = min(n / (n / l), m / (m / l)); _ *= qpow(hh[n / l], s(m / l) * (sumhl[r] - sumhl[l - 1])) * qpow(hh[m / l], s(n / l) * (sumhl[r] - sumhl[l - 1])) * qpow(pdhdkd[r] / pdhdkd[l - 1], s(n / l) * s(m / l)); } return _; } auto solve([[maybe_unused]] int t_ = 0) -> void { int t, n; cin >> t >> n; primes::init_prime(n + OFFSET); init_(n + OFFSET); while (t--) { int l, r; cin >> l >> r; mint _1 = calc(r, r), _2 = calc(l - 1, l - 1), _3 = calc(r, l - 1); cout << _1 * _2 / (_3 * _3) << '\n'; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int i_ = 0; solve(i_); return 0; }
|