题意
给定数列 a n , x a_n,x an,x,询问 ∏ l r ( μ ( a i ) + a i + x ) ( m o d 998244353 ) \prod\limits _l^r(\mu(a_i)+a_i+x)\pmod{998244353} l∏r(μ(ai)+ai+x)(mod998244353)
思路
可以预处理,变为求 ∏ l r b i ( m o d 998244353 ) , b i = μ ( a i ) + a i + x \prod\limits _l^r b_i\pmod{998244353}, b_i=\mu(a_i)+a_i+x l∏rbi(mod998244353),bi=μ(ai)+ai+x,前缀积即可
于是我在处理0的情况出了问题,一直95分自闭很久。
注意如果 b i = 0 b_i=0 bi=0,那么要单独讨论,因为可能有一种情况: b l − 1 = 0 , ∏ l r b i ≠ 0 b_{l-1}=0,\prod\limits _l^r b_i\neq0 bl−1=0,l∏rbi=0,维护一个前缀0个数的数组
那么处理前缀积的时候,如果 a i = 0 , i < j , a j = ∏ i + 1 j b i a_i=0,i<j,a_j=\prod\limits _{i+1}^j b_i ai=0,i<j,aj=i+1∏jbi, 这不是单纯的前缀积了
所以前缀积的时候一定要注意 0的讨论,以0为分界点,分段前缀积
代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define NDEBUG
#include <assert.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef unsigned int ui;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
#define endl '\n'
#define rd read()
#define pb push_back
#define mst(a, b) memset((a), (b), sizeof(a));
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
#define ls (u << 1)
#define rs (u << 1 | 1)
#define mod 998244353
#define maxn (int)(3e5+5)
int n, q, x, tot, p[maxn], zero[maxn];
ll a[maxn];
bool vis[maxn];
ll mu(ll n) {
ll v = 1;
for(int i = 2; i * i <= n; i++)
if(n % i == 0) {
v = -v; n /= i;
if(n % i == 0) return 0;
}
if(n != 1) v = -v;
return v;
}
void exgcd(int a, int b, int &x, int &y) {
if(!b) {x = 1, y = 0; return;}
exgcd(b, a % b, y, x), y -= a / b * x;
}
int inv(int a, int b) {
int x, y; exgcd(a, b, x, y);
return x > 0 ? x : x + b;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D:\\Chrome Downloadings\\input.txt", "r", stdin);
freopen("D:\\Chrome Downloadings\\output.txt", "w", stdout);
#endif
cin >> n >> q >> x;
a[0] = 1;
for(int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
a[i] = (a[i] + mu(a[i]) + x) % mod;
if(a[i] == 0) zero[i] = 1;
zero[i] += zero[i - 1];//统计前缀0个数
if(a[i - 1] != 0) a[i] = a[i] * a[i - 1] % mod;
}
while(q--) {
int l = rd, r = rd;
if(zero[r] - zero[l - 1]) puts("0");//如果[l,r]有0
else if(a[l - 1] == 0) printf("%lld\n", a[r]);//如果左边界是0,0没有逆元
else printf("%lld\n", a[r] * inv(a[l - 1], mod) % mod);
}
return 0;
}