[LOJ # 162] template title - Quick power of 2

< Topic link >

Note: This may also question a template.

Note 2: $ p = 998224352 $

Note 3: For $ 100 \% $ data, $ n \ leq 5 \ times 10 ^ 6 $

This question is inspired by the idea that if power should be directly and quickly fly T (but still often see the master card had $ 997ms $ ......).

and so

Act One: direct and rapid power

Complexity: $ \ Theta (N \ log p) $

Not much to say directly and quickly to power.

Method two: Magic block ideas

Because asking more, we consider pretreatment.

Suppose we deal with the $ k $.

We of persimmon in the index.

Have:

$$\large x^y=x^{y\, \mod\, k }\times x^{\left\lfloor\frac{y}{k}\right\rfloor \times k}$$

You can then $ \ Theta (1) $ answered

Pretreatment $ \ Theta (k + \ frac {p} {k}) $ of

Then take $ k = p ^ {\ frac {1} {2}} + 1 $ can achieve optimal complexity $ \ Theta (p ^ {\ frac {1} {2}} + N) $ ($ + 1 is to prevent $ $ \ sqrt {p} $ kneeling precision rounding off)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
const int Mod = 998244352, Sqrt = 31596;
long long val[32000], van[32000], vd, qn;
int main() {
    long long q;
    scanf("%lld%lld", &vd, &qn);
    val[0] = 1;
    val[1] = vd % Mod;
    for (int i = 2; i <= Sqrt; i++) val[i] = val[i - 1] * vd % Mod;  // cout<<val[i]<<" ";
    van[0] = 1;
    van[1] = val[Sqrt];
    for (int i = 2; i <= Sqrt; i++) van[i] = van[i - 1] * val[Sqrt] % Mod;
    for (int i = 1; i <= qn; i++) {
        scanf("%lld", &q);
        // cout<<q%Sqrt<<" "<<q/Sqrt<<endl;
        // cout<<val[q%Sqrt]<<" "<<van[q/Sqrt]<<endl;
        printf("%lld ", val[q % Sqrt] * van[q / Sqrt] % Mod);
    }
    puts("");
}

 I have to say formatting codes exciting ......

Guess you like

Origin www.cnblogs.com/kalginamiemeng/p/11647132.html