LOJ #162. 快速幂 2

题意

要求一个 \(O(\sqrt P) - O(1)\) 的快速幂。

幂可以用扩展欧拉定理规约到 \([1,P-1]\) 中。

分析

分块。定个阈值 \(B = \sqrt P\) + 1。

\(a^t = a^{t\bmod B} \cdot a^{B \times \lfloor\dfrac{t}{B}\rfloor}\)

\(\dfrac{t}{B} \le \dfrac{P}{B} = \mathcal{O}(\sqrt n)\)

这样预处理 \(a^i, (a^B)^i\) 即可。

#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
inline int read(){
	register int x=0,f=0,ch=getchar();
	while('0'>ch||ch>'9')f^=ch=='-',ch=getchar();
	while('0'<=ch&&ch<='9')x=x*10+(ch^'0'),ch=getchar();
	return f?-x:x;
}
const int P = 998244352, MAX = sqrt(P) + 10;
int x,n;
int a[MAX], b[MAX];
signed main(){
	int B = sqrt(P) + 1;
	int x = read(), T = read(); 
	a[0] = b[0] = 1;
	for(int i=1;i<=B;++i) a[i] = 1ll * a[i-1] * x % P;
	for(int i=1;i<=B;++i) b[i] = 1ll * b[i-1] * a[B] % P;
	while(T--) {
		int t = read();
		printf("%d ", 1ll * a[t % B] * b[t / B] % P);
	}
	return 0;
}
···
posted @ 2022-09-24 13:12  Lates  阅读(41)  评论(0)    收藏  举报