Advertisement
999ms

Untitled

Nov 8th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #define GCC Optimaze("Ofast")
  2. #include <bits/stdc++.h>
  3. #define all(x) (x).begin(),(x).end()
  4.  
  5. using namespace std;
  6. using ll = long long;
  7.  
  8. const int LOG = 18;
  9. const int N = 100100;
  10. const ll mod = 65537;
  11.  
  12. int normalize(ll x) {
  13.     x %= mod;
  14.     x += mod;
  15.     x %= mod;
  16.     return x;
  17. }
  18.  
  19. int add(ll a, ll b) {
  20.     return normalize(a + b);
  21. }
  22. int sub(ll a, ll b) {
  23.     return normalize(a - b);
  24. }
  25.  
  26. ll mult(ll a, ll b) {
  27.     a = normalize(a);
  28.     b = normalize(b);
  29.     return normalize(a * b);
  30. }
  31.  
  32. ll fastPow(ll a, ll p) {
  33.     ll ans = 1;
  34.     a = normalize(a);
  35.     while (p) {
  36.         if (p & 1) {
  37.             ans *= a;
  38.             ans %= mod;
  39.         }
  40.         a *= a;
  41.         a %= mod;
  42.         p /= 2;
  43.     }
  44.     return normalize(ans);
  45. }
  46.  
  47. ll inv(ll a) {
  48.     return fastPow(a, mod - 2);
  49. }
  50.  
  51.  
  52. namespace {
  53. template<int n, typename T = int>
  54. void mult(const T *__restrict a, const T *__restrict b, T *__restrict res) {
  55.     if (n <= 64) {
  56.         for (int i = 0; i < n; i++) {
  57.             for (int j = 0; j < n; j++) {
  58.                 res[i + j] += a[i] * 1ll * b[j] % mod;
  59.                 res[i + j] = normalize(res[i + j]);
  60.             }
  61.         }
  62.     } else {
  63.         const int mid = n / 2;
  64.         alignas(64) T btmp[n], E[n] = {};
  65.         auto atmp = btmp + mid;
  66.         for (int i = 0; i < mid; i++) {
  67.             atmp[i] = (a[i] + a[i + mid]) % mod;
  68.             btmp[i] = (b[i] + b[i + mid]) % mod;
  69.         }
  70.         mult<mid>(atmp, btmp, E);
  71.         mult<mid>(a + 0, b + 0, res);
  72.         mult<mid>(a + mid, b + mid, res + n);
  73.         for (int i = 0; i < mid; i++) {
  74.             const auto tmp = res[i + mid];
  75.             res[i + mid] += E[i] - res[i] - res[i + 2 * mid];
  76.             res[i + 2 * mid] += E[i + mid] - tmp - res[i + 3 * mid];
  77.             res[i + mid] = normalize(res[i + mid]);
  78.             res[i + 2 * mid] = normalize(res[i + 2 * mid]);
  79.         }
  80.     }
  81. }
  82. }
  83.  
  84. static alignas(64) int A[N], B[N], ans[N], fact[N], invfact[N];
  85.  
  86.  
  87. void Solve() {
  88.     int n, k;
  89.     cin >> n >> k;
  90.     k = normalize(k);
  91.     vector<ll> b(n);
  92.     memset(A, 0, sizeof A);
  93.     memset(B, 0, sizeof B);
  94.     memset(ans, 0, sizeof ans);
  95.     for (int i = 0; i < n; i++) cin >> b[i];
  96.     for (int i = 0; i < n; i++) {
  97.         b[i] = normalize(b[i]);
  98.     }
  99.     for (int i = 0; i < n; i++) {
  100.         B[i] = mult(b[n - 1 - i], invfact[i]);
  101.         A[i] = mult(fastPow(normalize(-k), i), invfact[i]);
  102.     }
  103.     const int NMAX = 1 << LOG;
  104.     mult<NMAX>(A, B, ans);
  105.     for (int i = 0; i < n; i++) {
  106.         cout << mult(ans[i], fact[i]) << " ";
  107.     }
  108.     cout << "\n";
  109. }
  110.  
  111. int main() {
  112.     ios_base::sync_with_stdio(false);
  113.     cin.tie(nullptr);
  114.     cout.tie(nullptr);
  115.     fact[0] = 1;
  116.     invfact[0] = 1;
  117.     for (int i = 1; i < N; i++) {
  118.         fact[i] = mult(fact[i - 1], i);
  119.         invfact[i] = inv(fact[i]);
  120.     }
  121.     int t;
  122.     cin >> t;
  123.     while (t--) {
  124.         Solve();
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement