EntropyIncreaser

HDU 6755

Jul 21st, 2020
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <cctype>
  7.  
  8. #include <algorithm>
  9. #include <random>
  10. #include <bitset>
  11. #include <queue>
  12. #include <functional>
  13. #include <set>
  14. #include <map>
  15. #include <vector>
  16. #include <chrono>
  17. #include <iostream>
  18. #include <limits>
  19. #include <numeric>
  20.  
  21. #define LOG(FMT...) fprintf(stderr, FMT)
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27.  
  28. // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  29.  
  30. template <class T>
  31. istream& operator>>(istream& is, vector<T>& v) {
  32.   for (T& x : v)
  33.     is >> x;
  34.   return is;
  35. }
  36.  
  37. template <class T>
  38. ostream& operator<<(ostream& os, const vector<T>& v) {
  39.   if (!v.empty()) {
  40.     os << v.front();
  41.     for (int i = 1; i < v.size(); ++i)
  42.       os << ' ' << v[i];
  43.   }
  44.   return os;
  45. }
  46.  
  47. typedef long long ll;
  48.  
  49. const int P = 1000000009;
  50.  
  51. int mpow(int x, ll k, int p = P) {
  52.   int ret = 1;
  53.   if (k >= p - 1) k = k % (p - 1) + (p - 1);
  54.   while (k) {
  55.     if (k & 1)
  56.       ret = ret * (ll) x % p;
  57.     x = x * (ll) x % p;
  58.     k >>= 1;
  59.   }
  60.   return ret;
  61. }
  62.  
  63. int norm(int x) { return x >= P ? x - P : x; }
  64.  
  65. struct NumberTheory {
  66.  
  67.   typedef pair<int, int> _P2_Field;
  68.  
  69.   mt19937 rng;
  70.  
  71.   NumberTheory() : rng(chrono::steady_clock::now().time_since_epoch().count()) {}
  72.  
  73.   void _exGcd(int a, int b, int &x, int &y) {
  74.     if (!b) {
  75.       x = 1;
  76.       y = 0;
  77.       return;
  78.     }
  79.     _exGcd(b, a % b, y, x);
  80.     y -= a / b * x;
  81.   }
  82.  
  83.   int inv(int a, int p = P) {
  84.     int x, y;
  85.     _exGcd(a, p, x, y);
  86.     if (x < 0)
  87.       x += p;
  88.     return x;
  89.   }
  90.  
  91.   template<class Integer>
  92.   bool quadRes(Integer a, Integer b) {
  93.     if (a <= 1)
  94.       return true;
  95.     while (a % 4 == 0)
  96.       a /= 4;
  97.     if (a % 2 == 0)
  98.       return (b % 8 == 1 || b % 8 == 7) == quadRes(a / 2, b);
  99.     return ((a - 1) % 4 == 0 || (b - 1) % 4 == 0) == quadRes(b % a, a);
  100.   }
  101.  
  102.   // assume p in prime, x in quadratic residue
  103.   int sqrt(int x, int p = P) {
  104.     if (p == 2 || x <= 1)
  105.       return x;
  106.     int w, v, k = (p + 1) / 2;
  107.     do {
  108.       w = rng() % p;
  109.     } while (quadRes(v = int((w * (ll) w - x + p) % p), p));
  110.     _P2_Field res(1, 0), a(w, 1);
  111.     while (k) {
  112.       if (k & 1)
  113.         res = _P2_Field((res.first * (ll) a.first + res.second * (ll) a.second % p * v) % p,
  114.                         (res.first * (ll) a.second + res.second * (ll) a.first) % p);
  115.       if (k >>= 1)
  116.         a = _P2_Field((a.first * (ll) a.first + a.second * (ll) a.second % p * v) % p,
  117.                       (a.first * (ll) a.second << 1) % p);
  118.     }
  119.     return min(res.first, p - res.first);
  120.   }
  121.  
  122. } nt;
  123. struct Simple {
  124.   int n;
  125.   vector<int> fac, ifac, inv;
  126.  
  127.   void build(int n) {
  128.     this->n = n;
  129.     fac.resize(n + 1);
  130.     ifac.resize(n + 1);
  131.     inv.resize(n + 1);
  132.     fac[0] = 1;
  133.     for (int x = 1; x <= n; ++x)
  134.       fac[x] = fac[x - 1] * (ll) x % P;
  135.     inv[1] = 1;
  136.     for (int x = 2; x <= n; ++x)
  137.       inv[x] = -(P / x) * (ll) inv[P % x] % P + P;
  138.     ifac[0] = 1;
  139.     for (int x = 1; x <= n; ++x)
  140.       ifac[x] = ifac[x - 1] * (ll) inv[x] % P;
  141.   }
  142.  
  143.   Simple() {
  144.     build(1);
  145.   }
  146.  
  147.   void check(int k) {
  148.     int nn = n;
  149.     if (k > nn) {
  150.       while (k > nn)
  151.         nn <<= 1;
  152.       build(nn);
  153.     }
  154.   }
  155.  
  156.   int gfac(int k) {
  157.     check(k);
  158.     return fac[k];
  159.   }
  160.  
  161.   int gifac(int k) {
  162.     check(k);
  163.     return ifac[k];
  164.   }
  165.  
  166.   int ginv(int k) {
  167.     check(k);
  168.     return inv[k];
  169.   }
  170.  
  171.   int binom(int n, int m) {
  172.     if (m < 0 || m > n)
  173.       return 0;
  174.     return gfac(n) * (ll) gifac(m) % P * gifac(n - m) % P;
  175.   }
  176. } simp;
  177.  
  178. int main() {
  179. #ifdef ELEGIA
  180.   freopen("test.in", "r", stdin);
  181.   int nol_cl = clock();
  182. #endif
  183.   ios::sync_with_stdio(false);
  184.   cin.tie(nullptr);
  185.  
  186.   int rt = nt.sqrt(5), irt = nt.inv(rt);
  187.   int a = (rt + 1) * (ll)nt.inv(2) % P, b = (P + 1 - rt) * (ll)nt.inv(2) % P;
  188.  
  189.   int t;
  190.   cin >> t;
  191.   while (t--) {
  192.     ll n, c;
  193.     int k;
  194.     cin >> n >> c >> k;
  195.     int x = mpow(a, c), y = mpow(b, c);
  196.     int ans = 0;
  197.     int prd = mpow(x, k), q = y * (ll)nt.inv(x) % P;
  198.     for (int i = 0; i <= k; ++i) {
  199.       int coe = simp.binom(k, i);
  200.       if (i & 1) coe = norm(P - coe);
  201.       int res = 0;
  202.       if (prd == 1)
  203.         res = (n + 1) % P;
  204.       else
  205.         res = (mpow(prd, n + 1) + P - 1) * (ll)nt.inv(norm(prd + P - 1)) % P;
  206.       ans = (ans + res * (ll)coe) % P;
  207.       prd = prd * (ll)q % P;
  208.     }
  209.     ans = ans * (ll)mpow(irt, k) % P;
  210.     cout << ans << '\n';
  211.   }
  212.  
  213. #ifdef ELEGIA
  214.   LOG("Time: %dms\n", int ((clock()
  215.           -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
  216. #endif
  217.   return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment