牛客练习赛22-E-简单数据结构1

题意

给一个长为n的序列,m次操作,每次操作:

  1. $[l,r]$ 加 $x$
  2. 对于$[l,r]$,查询$a[l] ^ {a[l+1] ^ {a[l+2]…}} mod \ P$

    分析

    这个欧拉函数的取模好迷啊…

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 2e7+5;
ll diff[maxn];
#define Mod(x,p) x < p ? x : x % p + p
bool vis[maxn];
int prime[maxn], phi[maxn];
void init() { // 线性筛预处理欧拉函数
clr(vis, 0);
phi[1] = 1;
int tot = 0;
for (int i = 2; i < maxn; i++) {
if (!vis[i]) {
prime[tot++] = i;
phi[i] = i - 1;
}
for (int k = 0; k < tot && 1LL * i * prime[k] < maxn; k++) {
vis[i * prime[k]] = 1;
if (i % prime[k] == 0) {
phi[i * prime[k]] = phi[i] * prime[k];
break;
}
phi[i * prime[k]] = phi[i] * (prime[k] - 1);
}
}
}

ll my_pow(ll a,ll b,ll mod){
ll ret = 1;
while(b){
if(b & 1) ret = Mod(ret * a,mod);
a = Mod(a * a,mod);
b >>= 1;
}
return Mod(ret,mod);
}

inline ll lowb(ll x) { return x & (-x); }
inline void update(int l, int r, ll val, int n) {
for (ll i = l; i <= n; i += lowb(i)) diff[i] += val;
for (ll i = r + 1; i <= n; i += lowb(i)) diff[i] -= val;
}
inline ll query(int l, int r, ll p) {
ll ret = 0;
for (ll i = l; i > 0; i -= lowb(i)) ret += diff[i];
if (l == r || p == 1) return Mod(ret, p);
return my_pow(Mod(ret,p), query(l + 1, r, phi[p]), p);
}
int main() {
// /*
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
// */
std::ios::sync_with_stdio(false);
int n, m;
init();
while (~scanf("%d%d",&n,&m)) {
ll foo;
for (int i = 1; i <= n; i++) {
scanf("%lld",&foo);
update(i, i, foo, n);
}
ll l, r, w, p, op;
for (int i = 0; i < m; i++) {
scanf("%lld",&op);
if (op == 1) {
scanf("%lld%lld%lld",&l,&r,&w);
update(l, r, w, n);
} else {
scanf("%lld%lld%lld",&l,&r,&p);
printf("%lld\n",query(l,r,p) % p);
}
}
}
}
Thank you for your support!