Advertisement
cyberjab

asd

Sep 13th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <unordered_map>
  10. #include <unordered_set>
  11. #include <queue>
  12. #include <deque>
  13. #include <cmath>
  14. #include <numeric>
  15. #include <algorithm>
  16. #include <ctime>
  17. #include <chrono>
  18. #include <random>
  19. #include <functional>
  20. #include <stack>
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include <limits.h>
  24.  
  25. using namespace std;
  26.  
  27. class Num {
  28. public:
  29. typedef uint64_t word;
  30.  
  31. std::vector<word> words;
  32. bool neg;
  33.  
  34. static word word_mask() {
  35. return (word)-1;
  36. }
  37.  
  38. static size_t word_bits() {
  39. return sizeof(word) * CHAR_BIT;
  40. }
  41.  
  42. static word word_half_mask() {
  43. return word_mask() >> word_bits() / 2;
  44. }
  45.  
  46. static word char_to_word(char c) {
  47. switch (c) {
  48. case '0': return 0;
  49. case '1': return 1;
  50. case '2': return 2;
  51. case '3': return 3;
  52. case '4': return 4;
  53. case '5': return 5;
  54. case '6': return 6;
  55. case '7': return 7;
  56. case '8': return 8;
  57. case '9': return 9;
  58. case 'a': case 'A': return 10;
  59. case 'b': case 'B': return 11;
  60. case 'c': case 'C': return 12;
  61. case 'd': case 'D': return 13;
  62. case 'e': case 'E': return 14;
  63. case 'f': case 'F': return 15;
  64. case 'g': case 'G': return 16;
  65. case 'h': case 'H': return 17;
  66. case 'i': case 'I': return 18;
  67. case 'j': case 'J': return 19;
  68. case 'k': case 'K': return 20;
  69. case 'l': case 'L': return 21;
  70. case 'm': case 'M': return 22;
  71. case 'n': case 'N': return 23;
  72. case 'o': case 'O': return 24;
  73. case 'p': case 'P': return 25;
  74. case 'q': case 'Q': return 26;
  75. case 'r': case 'R': return 27;
  76. case 's': case 'S': return 28;
  77. case 't': case 'T': return 29;
  78. case 'u': case 'U': return 30;
  79. case 'v': case 'V': return 31;
  80. case 'w': case 'W': return 32;
  81. case 'x': case 'X': return 33;
  82. case 'y': case 'Y': return 34;
  83. case 'z': case 'Z': return 35;
  84. default: return word_mask();
  85. }
  86. }
  87.  
  88. static word word_gcd(word a, word b) {
  89. while (1) {
  90. if (a == 0) return b;
  91. b %= a;
  92. if (b == 0) return a;
  93. a %= b;
  94. }
  95. }
  96.  
  97. Num() : neg(false) {}
  98.  
  99. Num(size_t n, word w, bool neg = false) : words(n, w), neg(neg) {}
  100.  
  101. Num(const word* a, const word* b, bool neg = false) : words(a, b), neg(neg) {
  102. truncate();
  103. }
  104.  
  105. Num(const Num& a) {
  106. words = a.words;
  107. neg = a.neg;
  108. }
  109.  
  110. Num& operator = (const Num& a) {
  111. words = a.words;
  112. neg = a.neg;
  113. return *this;
  114. }
  115.  
  116. Num(int i) : neg(i < 0) {
  117. unsigned u = (i < 0) ? -(unsigned)i : (unsigned)i;
  118. if (sizeof(u) <= word_bits()) {
  119. if (u > 0) push_back(u);
  120. }
  121. else {
  122. for (; u; u >>= word_bits()) push_back(u);
  123. }
  124. }
  125.  
  126. Num(const char* c, word base = 10, char** endptr = NULL) : neg(false) {
  127. // read sign
  128. if (*c == '-') {
  129. c++;
  130. neg = true;
  131. }
  132. // read digits
  133. for (; *c; c++) {
  134. mul_word(base);
  135. word b = char_to_word(*c);
  136. if (b >= base) break;
  137. add_word(b);
  138. }
  139. if (endptr) *endptr = (char*)c;
  140. }
  141.  
  142. void resize(size_t n) {
  143. words.resize(n);
  144. }
  145.  
  146. void pop_back() {
  147. words.pop_back();
  148. }
  149.  
  150. void push_back(word b) {
  151. words.push_back(b);
  152. }
  153.  
  154. word& back() {
  155. return words.back();
  156. }
  157.  
  158. const word& back() const {
  159. return words.back();
  160. }
  161.  
  162. size_t size() const {
  163. return words.size();
  164. }
  165.  
  166. word& operator [] (size_t i) {
  167. return words[i];
  168. }
  169.  
  170. const word& operator [] (size_t i) const {
  171. return words[i];
  172. }
  173.  
  174. Num& set_neg(bool neg) {
  175. this->neg = neg;
  176. return *this;
  177. }
  178.  
  179. Num& truncate() {
  180. while (size() > 0 && words.back() == 0) pop_back();
  181. return *this;
  182. }
  183.  
  184. size_t bitlength() const {
  185. if (size() == 0) return 0;
  186. size_t last = size() - 1;
  187. size_t result = word_bitlength((*this)[last]) + last * word_bits();
  188. return result;
  189. }
  190.  
  191. size_t count_trailing_zeros() const {
  192. for (size_t i = 0; i < size(); i++) {
  193. word w = (*this)[i];
  194. if (w) return word_count_trailing_zeros(w) + i * word_bits();
  195. }
  196. return 0;
  197. }
  198.  
  199. static int cmp_abs(const Num& a, const Num& b) {
  200. size_t na = a.size(), nb = b.size();
  201. if (na != nb) {
  202. return na < nb ? -1 : +1;
  203. }
  204. for (size_t i = na; i-- > 0;) {
  205. word wa = a[i], wb = b[i];
  206. if (wa != wb) {
  207. return wa < wb ? -1 : +1;
  208. }
  209. }
  210. return 0;
  211. }
  212.  
  213. static int cmp(const Num& a, const Num& b) {
  214. if (a.size() == 0 && b.size() == 0) return 0;
  215. if (!a.neg && !b.neg) return +cmp_abs(a, b);
  216. if (a.neg && b.neg) return -cmp_abs(a, b);
  217. return a.neg && !b.neg ? -1 : +1;
  218. }
  219.  
  220. static size_t word_bitlength(word a) {
  221. for (int i = word_bits() - 1; i >= 0; i--) if ((a >> i) & 1) return i + 1;
  222. return 0;
  223. }
  224.  
  225. static size_t word_count_trailing_zeros(word a) {
  226. for (int i = 0; i < (int)word_bits(); i++) if ((a >> i) & 1) return i;
  227. return word_bits();
  228. }
  229.  
  230. static word add_carry(word* a, word b) {
  231. return (*a += b) < b;
  232. }
  233.  
  234. static word sub_carry(word* a, word b) {
  235. word tmp = *a;
  236. return (*a = tmp - b) > tmp;
  237. }
  238.  
  239. static word word_mul_hi(word a, word b) {
  240. size_t n = word_bits() / 2;
  241. word a_hi = a >> n;
  242. word a_lo = a & word_half_mask();
  243. word b_hi = b >> n;
  244. word b_lo = b & word_half_mask();
  245. word tmp = ((a_lo * b_lo) >> n) + a_hi * b_lo;
  246. tmp = (tmp >> n) + ((a_lo * b_hi + (tmp & word_half_mask())) >> n);
  247. return tmp + a_hi * b_hi;
  248. }
  249.  
  250. static Num& add_unsigned_overwrite(Num& a, const Num& b) {
  251. size_t i, na = a.size(), nb = b.size(), n = std::max(na, nb);
  252. a.resize(n);
  253. word carry = 0;
  254. for (i = 0; i < nb; i++) {
  255. carry = add_carry(&a[i], carry);
  256. carry += add_carry(&a[i], b[i]);
  257. }
  258. for (; i < n && carry; i++) carry = add_carry(&a[i], carry);
  259. if (carry) a.push_back(carry);
  260. return a.truncate();
  261. }
  262.  
  263. static Num& sub_unsigned_overwrite(Num& a, const Num& b) {
  264. //assert(cmp_abs(a, b) >= 0);
  265. size_t i, na = a.size(), nb = b.size();
  266. word carry = 0;
  267. for (i = 0; i < nb; i++) {
  268. carry = sub_carry(&a[i], carry);
  269. carry += sub_carry(&a[i], b[i]);
  270. }
  271. for (; i < na && carry; i++) carry = sub_carry(&a[i], carry);
  272. //assert(!carry);
  273. return a.truncate();
  274. }
  275.  
  276. static Num mul_long(const Num& a, const Num& b) {
  277. size_t na = a.size(), nb = b.size(), nc = na + nb + 1;
  278. Num c(nc, 0, a.neg ^ b.neg), carries(nc, 0);
  279. for (size_t ia = 0; ia < na; ia++) {
  280. for (size_t ib = 0; ib < nb; ib++) {
  281. size_t i = ia + ib, j = i + 1;
  282. // WARNING: Might overflow if word size is chosen too small
  283. carries[i + 1] += add_carry(&c[i], a[ia] * b[ib]);
  284. carries[j + 1] += add_carry(&c[j], word_mul_hi(a[ia], b[ib]));
  285. }
  286. }
  287. return add_unsigned_overwrite(c, carries).truncate();
  288. }
  289.  
  290. static Num mul_karatsuba(const Num& a, const Num& b) {
  291. size_t na = a.size(), nb = b.size(), n = std::max(na, nb), m2 = n / 2 + (n & 1);
  292. Num a_parts[2], b_parts[2];
  293. split(a, a_parts, 2, m2);
  294. split(b, b_parts, 2, m2);
  295. m2 *= word_bits();
  296. Num z0 = a_parts[0] * b_parts[0];
  297. Num z1 = (a_parts[0] + a_parts[1]) * (b_parts[0] + b_parts[1]);
  298. Num z2 = a_parts[1] * b_parts[1];
  299. Num result = z2;
  300. result <<= m2;
  301. result += z1 - z2 - z0;
  302. result <<= m2;
  303. result += z0;
  304. return result;
  305. }
  306.  
  307. static Num mul(const Num& a, const Num& b) {
  308. size_t karatsuba_threshold = 20;
  309. if (a.size() > karatsuba_threshold && b.size() > karatsuba_threshold) {
  310. return mul_karatsuba(a, b);
  311. }
  312. return mul_long(a, b);
  313. }
  314.  
  315. static Num add_signed(const Num& a, bool a_neg, const Num& b, bool b_neg) {
  316. if (a_neg == b_neg) return add_unsigned(a, b).set_neg(a_neg);
  317. if (cmp_abs(a, b) >= 0) return sub_unsigned(a, b).set_neg(a_neg);
  318. return sub_unsigned(b, a).set_neg(b_neg);
  319. }
  320.  
  321. Num& operator >>= (size_t n_bits) {
  322. if (n_bits == 0) return *this;
  323. size_t n_words = n_bits / word_bits();
  324. if (n_words >= size()) {
  325. resize(0);
  326. return *this;
  327. }
  328. n_bits %= word_bits();
  329. if (n_bits == 0) {
  330. for (size_t i = 0; i < size() - n_words; i++) {
  331. (*this)[i] = (*this)[i + n_words];
  332. }
  333. }
  334. else {
  335. word hi, lo = (*this)[n_words];
  336. for (size_t i = 0; i < size() - n_words - 1; i++) {
  337. hi = (*this)[i + n_words + 1];
  338. (*this)[i] = (hi << (word_bits() - n_bits)) | (lo >> n_bits);
  339. lo = hi;
  340. }
  341. (*this)[size() - n_words - 1] = lo >> n_bits;
  342. }
  343. resize(size() - n_words);
  344. return truncate();
  345. }
  346.  
  347. Num& operator <<= (size_t n_bits) {
  348. if (n_bits == 0) return *this;
  349. size_t n_words = n_bits / word_bits();
  350. n_bits %= word_bits();
  351. size_t old_size = size();
  352. size_t n = old_size + n_words + (n_bits != 0);
  353. resize(n);
  354. if (n_bits == 0) {
  355. for (size_t i = n; i-- > n_words;) {
  356. (*this)[i] = (*this)[i - n_words];
  357. }
  358. }
  359. else {
  360. word lo, hi = 0;
  361. for (size_t i = n - 1; i > n_words; i--) {
  362. lo = (*this)[i - n_words - 1];
  363. (*this)[i] = (hi << n_bits) | (lo >> (word_bits() - n_bits));
  364. hi = lo;
  365. }
  366. (*this)[n_words] = hi << n_bits;
  367. }
  368. for (size_t i = 0; i < n_words; i++) (*this)[i] = 0;
  369. return truncate();
  370. }
  371.  
  372. static void div_mod(const Num& numerator, Num denominator, Num& quotient, Num& remainder) {
  373. quotient = 0;
  374. remainder = numerator;
  375. if (cmp_abs(remainder, denominator) >= 0) {
  376. int n = numerator.bitlength() - denominator.bitlength();
  377. denominator <<= n;
  378. for (; n >= 0; n--) {
  379. if (cmp_abs(remainder, denominator) >= 0) {
  380. sub_unsigned_overwrite(remainder, denominator);
  381. quotient.set_bit(n);
  382. }
  383. denominator >>= 1;
  384. }
  385. }
  386. quotient.set_neg(numerator.neg ^ denominator.neg);
  387. remainder.set_neg(numerator.neg);
  388. }
  389.  
  390. static void div_mod_half_word(const Num& numerator, word denominator, Num& quotient, word& remainder) {
  391. remainder = 0;
  392. Num dst(numerator.size(), 0);
  393.  
  394. for (size_t i = numerator.size(); i-- > 0;) {
  395. word dst_word = 0;
  396. word src_word = numerator[i];
  397. word parts[2];
  398. parts[0] = src_word >> word_bits() / 2;
  399. parts[1] = src_word & word_half_mask();
  400.  
  401. for (size_t j = 0; j < 2; j++) {
  402. remainder <<= word_bits() / 2;
  403. remainder |= parts[j];
  404.  
  405. word div_word = remainder / denominator;
  406. word mod_word = remainder % denominator;
  407. remainder = mod_word;
  408.  
  409. dst_word <<= word_bits() / 2;
  410. dst_word |= div_word;
  411. }
  412.  
  413. dst[i] = dst_word;
  414. }
  415.  
  416. quotient = dst.truncate().set_neg(numerator.neg);
  417. }
  418.  
  419. static void split(const Num& a, Num* parts, size_t n_parts, size_t n) {
  420. size_t i = 0;
  421. for (size_t k = 0; k < n_parts; k++) {
  422. Num& part = parts[k];
  423. part.resize(n);
  424. for (size_t j = 0; j < n && i < a.size(); j++) part[j] = a[i++];
  425. part = part.truncate();
  426. }
  427. }
  428.  
  429. static Num div(const Num& numerator, const Num& denominator) {
  430. Num quotient, remainder;
  431. div_mod(numerator, denominator, quotient, remainder);
  432. return quotient;
  433. }
  434.  
  435. static Num mod(const Num& numerator, const Num& denominator) {
  436. Num quotient, remainder;
  437. div_mod(numerator, denominator, quotient, remainder);
  438. return remainder;
  439. }
  440.  
  441. static Num add_unsigned(const Num& a, const Num& b) {
  442. Num result(a);
  443. return add_unsigned_overwrite(result, b);
  444. }
  445.  
  446. static Num sub_unsigned(const Num& a, const Num& b) {
  447. Num result(a);
  448. return sub_unsigned_overwrite(result, b);
  449. }
  450.  
  451. static Num add(const Num& a, const Num& b) {
  452. Num result = add_signed(a, a.neg, b, b.neg);
  453. return result;
  454. }
  455.  
  456. static Num sub(const Num& a, const Num& b) {
  457. Num result = add_signed(a, a.neg, b, !b.neg);
  458. return result;
  459. }
  460.  
  461. static Num gcd(const Num& a0, const Num& b0) {
  462.  
  463. if (a0.size() == 1 && b0.size() == 1) {
  464. return Num(1, word_gcd(a0[0], b0[0]));
  465. }
  466.  
  467. Num a(a0), b(b0);
  468. a.neg = b.neg = false;
  469.  
  470. if (a.size() == 0) return b0;
  471. if (b.size() == 0) return a0;
  472.  
  473. size_t n = a.count_trailing_zeros();
  474. size_t m = b.count_trailing_zeros();
  475. if (n > m) {
  476. std::swap(n, m);
  477. a.words.swap(b.words);
  478. }
  479.  
  480. a >>= n;
  481. b >>= n;
  482.  
  483. do {
  484. b >>= b.count_trailing_zeros();
  485. if (cmp_abs(a, b) > 0) a.words.swap(b.words);
  486. sub_unsigned_overwrite(b, a);
  487. } while (b.size() > 0);
  488.  
  489. a <<= n;
  490.  
  491. return a;
  492. }
  493.  
  494. typedef void (*random_func)(uint8_t* bytes, size_t n_bytes);
  495.  
  496. static Num random_bits(size_t n_bits, random_func func) {
  497. if (n_bits == 0) return 0;
  498. size_t partial_bits = n_bits % word_bits();
  499. size_t n_words = n_bits / word_bits() + (partial_bits > 0);
  500. size_t n_bytes = n_words * sizeof(word);
  501. Num result(n_words, 0);
  502. uint8_t* bytes = (uint8_t*)&result[0];
  503. func(bytes, n_bytes);
  504. if (partial_bits) {
  505. size_t too_many_bits = word_bits() - partial_bits;
  506. result.back() >>= too_many_bits;
  507. }
  508. return result;
  509. }
  510.  
  511. static Num random_inclusive(const Num& inclusive, random_func func) {
  512. size_t n_bits = inclusive.bitlength();
  513. while (true) {
  514. Num result = random_bits(n_bits, func);
  515. if (result <= inclusive) return result;
  516. }
  517. }
  518.  
  519. static Num random_exclusive(const Num& exclusive, random_func func) {
  520. size_t n_bits = exclusive.bitlength();
  521. while (true) {
  522. Num result = random_bits(n_bits, func);
  523. if (result < exclusive) return result;
  524. }
  525. }
  526.  
  527. static Num random_second_exclusive(const Num& inclusive_min_val, const Num& exclusive_max_val, random_func func) {
  528. return inclusive_min_val + random_exclusive(exclusive_max_val - inclusive_min_val, func);
  529. }
  530.  
  531. static Num random_both_inclusive(const Num& inclusive_min_val, const Num& inclusive_max_val, random_func func) {
  532. return inclusive_min_val + random_inclusive(inclusive_max_val - inclusive_min_val, func);
  533. }
  534.  
  535. Num& set_bit(size_t i) {
  536. size_t i_word = i / word_bits();
  537. size_t i_bit = i % word_bits();
  538. if (size() <= i_word) resize(i_word + 1);
  539. (*this)[i_word] |= ((word)1) << i_bit;
  540. return *this;
  541. }
  542.  
  543. word get_bit(size_t i) const {
  544. size_t i_word = i / word_bits();
  545. size_t i_bit = i % word_bits();
  546. if (i_word >= size()) return 0;
  547. return ((*this)[i_word] >> i_bit) & 1;
  548. }
  549.  
  550. void clr_bit(size_t i) {
  551. size_t i_word = i / word_bits();
  552. size_t i_bit = i % word_bits();
  553. if (i_word >= size()) return;
  554. word mask = 1;
  555. mask <<= i_bit;
  556. (*this)[i_word] &= ~mask;
  557. }
  558.  
  559. Num& mul_word(word b) {
  560. word carry = 0;
  561. for (size_t i = 0; i < size(); i++) {
  562. word a = (*this)[i];
  563. word tmp = a * b;
  564. carry = add_carry(&tmp, carry);
  565. carry += word_mul_hi(a, b);
  566. (*this)[i] = tmp;
  567. }
  568. if (carry) push_back(carry);
  569. return truncate();
  570. }
  571.  
  572. Num& add_word(word carry, size_t i0 = 0) {
  573. if (i0 >= size()) resize(i0 + 1);
  574. for (size_t i = i0; i < size() && carry; i++) {
  575. carry = add_carry(&(*this)[i], carry);
  576. }
  577. if (carry) push_back(carry);
  578. return truncate();
  579. }
  580.  
  581. void print(
  582. std::vector<char>& text,
  583. word base = 10,
  584. const char* alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
  585. ) const {
  586. if (size() == 0) {
  587. text.push_back('0');
  588. }
  589. else {
  590. Num tmp(*this);
  591. while (tmp.size() > 0) {
  592. word remainder;
  593. div_mod_half_word(tmp, base, tmp, remainder);
  594. text.push_back(alphabet[remainder]);
  595. }
  596. if (neg) text.push_back('-');
  597. std::reverse(text.begin(), text.end());
  598. }
  599. text.push_back('\0');
  600. }
  601.  
  602. friend std::ostream& operator << (std::ostream& out, const Num& num) {
  603. std::vector<char> tmp;
  604. num.print(tmp);
  605. out << &tmp[0];
  606. return out;
  607. }
  608.  
  609. double to_double() const {
  610. if (size() == 0) return 0.0;
  611. double d = 0.0, base = ::pow(2.0, word_bits());
  612. for (size_t i = size(); i-- > 0;) d = d * base + (*this)[i];
  613. return neg ? -d : d;
  614. }
  615.  
  616. bool can_convert_to_int(int* result) {
  617. if (*this < Num(INT_MIN) || *this > Num(INT_MAX)) return false;
  618.  
  619. unsigned temp = 0;
  620.  
  621. if (word_bits() >= sizeof(temp) * CHAR_BIT) {
  622. if (words.size() > 0) {
  623. temp = (*this)[0];
  624. }
  625. }
  626. else {
  627. for (size_t i = words.size(); i-- > 0;) {
  628. temp <<= word_bits();
  629. temp += (*this)[i];
  630. }
  631. }
  632.  
  633. *result = neg ? -temp : temp;
  634.  
  635. return true;
  636. }
  637.  
  638. Num pow(size_t exponent) const {
  639. Num result(1), p(*this);
  640. for (; exponent; exponent >>= 1) {
  641. if (exponent & 1) {
  642. result = result * p;
  643. exponent--;
  644. }
  645. p = p * p;
  646. }
  647. return result;
  648. }
  649.  
  650. Num mod_pow(Num exponent, const Num& modulus) const {
  651. Num result(1), base = (*this) % modulus;
  652. for (; exponent.size() > 0; exponent >>= 1) {
  653. if (exponent.get_bit(0)) {
  654. result = (result * base) % modulus;
  655. }
  656. base = (base * base) % modulus;
  657. }
  658. return result;
  659. }
  660.  
  661. Num sqrt() const {
  662. Num n = *this;
  663. int bit = bitlength();
  664. if (bit & 1) bit ^= 1;
  665. Num result = 0;
  666. for (; bit >= 0; bit -= 2) {
  667. Num tmp = result;
  668. tmp.set_bit(bit);
  669. if (n >= tmp) {
  670. n -= tmp;
  671. result.set_bit(bit + 1);
  672. }
  673. result >>= 1;
  674. }
  675. return result;
  676. }
  677.  
  678. Num& operator ++() {
  679. add_word(1);
  680. return *this;
  681. }
  682.  
  683. Num& operator += (const Num& b) { return *this = add(*this, b); }
  684. Num& operator -= (const Num& b) { return *this = sub(*this, b); }
  685. Num& operator *= (const Num& b) { return *this = mul(*this, b); }
  686. Num& operator /= (const Num& b) { return *this = div(*this, b); }
  687. Num& operator %= (const Num& b) { return *this = mod(*this, b); }
  688.  
  689. bool operator == (const Num& b) const { return cmp(*this, b) == 0; }
  690. bool operator != (const Num& b) const { return cmp(*this, b) != 0; }
  691. bool operator <= (const Num& b) const { return cmp(*this, b) <= 0; }
  692. bool operator >= (const Num& b) const { return cmp(*this, b) >= 0; }
  693. bool operator < (const Num& b) const { return cmp(*this, b) < 0; }
  694. bool operator > (const Num& b) const { return cmp(*this, b) > 0; }
  695.  
  696. Num operator + (const Num& b) const { return add(*this, b); }
  697. Num operator - (const Num& b) const { return sub(*this, b); }
  698. Num operator * (const Num& b) const { return mul(*this, b); }
  699. Num operator / (const Num& b) const { return div(*this, b); }
  700. Num operator % (const Num& b) const { return mod(*this, b); }
  701. Num operator - () const { return Num(*this).set_neg(!neg); }
  702.  
  703. Num operator >> (size_t n_bits) const { return Num(*this) >>= n_bits; }
  704. Num operator << (size_t n_bits) const { return Num(*this) <<= n_bits; }
  705. };
  706. //#include <windows.h>
  707. //void* operator new(size_t size) {
  708. // if (void* ptr = HeapAlloc(GetProcessHeap(), 0, size ? size : 1)) return ptr;
  709. // throw std::bad_alloc{};
  710. //}
  711. //void operator delete(void* ptr) {
  712. // HeapFree(GetProcessHeap(), 0, ptr);
  713. //}
  714.  
  715.  
  716. using ll = long long;
  717. using db = double;
  718. using ldb = long double;
  719. using pii = pair<int, int>;
  720. using pll = pair<ll, ll>;
  721. using vint = vector<int>;
  722. using vll = vector<ll>;
  723. using vst = vector<string>;
  724. #define all(x) x.begin(), x.end()
  725. #define size(x) int(x.size())
  726. #define rep(x) for(int rep_i = 0; rep_i < x; ++rep_i)
  727. #define forp(i, s, f) for(int i = s; i < f; ++i)
  728. #define form(i, s, f) for(int i = s; i > f; --i)
  729. #define PB push_back
  730. #define MP make_pair
  731. #define F first
  732. #define S second
  733. #define elif else if
  734. #define dprint(x) for (auto now: x) cout << now << " "
  735.  
  736. const int MOD = 1e9 + 7;
  737. const int MOD2 = 998244353;
  738. const int INF = 2e9 + 7;
  739. const ll LINF = 1e18 + 7;
  740. const double EPS = 1e-9;
  741. const long double PI = acosl(-1.0);
  742.  
  743. vector<vll> MatrixMult(vector<vll>& a, vector<vll>& b, int MOD) {
  744. vector<vll> c(size(a), vll(size(b[0])));
  745. forp(i, 0, size(a)) {
  746. forp(j, 0, size(b[0])) {
  747. forp(k, 0, size(a[0])) {
  748. c[i][j] += a[i][k] * b[k][j];
  749. c[i][j] %= MOD;
  750. }
  751. }
  752. }
  753. return c;
  754. }
  755.  
  756. vector<vll> MatrixBinPow(vector<vll>& a, Num p, int MOD) {
  757. int N = size(a);
  758. vector<vll> c(N, vll(N));
  759. forp(i, 0, N) {
  760. c[i][i] = 1;
  761. }
  762. while (p > 0) {
  763. if (p % 2 == 1) {
  764. c = MatrixMult(c, a, MOD);
  765. }
  766. a = MatrixMult(a, a, MOD);
  767. p >>= 1;
  768. }
  769. return c;
  770. }
  771.  
  772. vector<vll> makeT(int N) {
  773. int n = 1 << N;
  774. vector<vll> t(n, vll(n, 0));
  775. forp(i, 0, n) {
  776. forp(j, 0, n) {
  777. int now = i & j;
  778. if (now & (now << 1)) {
  779. continue;
  780. }
  781. now = i | j;
  782. bool f = false;
  783. forp(i, 0, N - 1) {
  784. if ((now & 1) == 0 and ((now >> 1) & 1) == 0) {
  785. f = true;
  786. break;
  787. }
  788. now >>= 1;
  789. }
  790. if (f) {
  791. continue;
  792. }
  793. t[i][j] = 1;
  794. }
  795. }
  796. return t;
  797. }
  798.  
  799. ll powmod(ll base, Num exp, ll modulo)
  800. {
  801. ll res = 1;
  802.  
  803. while (exp != 0)
  804. {
  805. if ((exp % 2) == 1)
  806. {
  807. res = (1ll * res * base) % modulo;
  808. }
  809.  
  810. base = (1ll * base * base) % modulo;
  811. exp >>= 1;
  812. }
  813.  
  814. return res;
  815. }
  816.  
  817. void solve() {
  818. string s1;
  819. cin >> s1;
  820. Num m(s1.c_str());
  821. int N, p;
  822. cin >> N >> p;
  823.  
  824. if (N == 1) {
  825. cout << powmod(2, m, p);
  826. return;
  827. }
  828. vector<vll> t = makeT(N);
  829. int n = 1 << N;
  830. vector<vll> dp(1, vll(n, 1));
  831. ll ans = 0;
  832. t = MatrixBinPow(t, m - 1, p);
  833. dp = MatrixMult(dp, t, p);
  834. forp(i, 0, n) {
  835. ans += dp[0][i]; ans %= p;
  836. };
  837. cout << ans;
  838. }
  839.  
  840. int main() {
  841. ios_base::sync_with_stdio(0);
  842. cin.tie(0);
  843. /*cout << setprecision(x)*/
  844. cout << fixed;
  845. int t;
  846. t = 1;
  847. while (t > 0) {
  848. solve();
  849. t--;
  850. }
  851. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement