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 91 92 93
| ##include "bits/stdc++.h" using namespace std;
typedef long long ll;
const int N = 2e7 + 10;
int prime[N], cnt, phi[N]; bool is_prime[N];
void init() { phi[1] = 1; for(int i = 2;i < N; i++) { if(!is_prime[i]) prime[++cnt] = i, phi[i] = i - 1; for(int j = 1;j <= cnt && i * prime[j] < N; j++) { is_prime[i * prime[j]] = 1; if(i % prime[j]) phi[i * prime[j]] = phi[i] * phi[prime[j]]; else { phi[i * prime[j]] = phi[i] * prime[j]; break; } } } }
##define lowbit(x) (x & (-x))
ll t[N]; int n, m;
void update(int x, ll val) { while(x <= n) { t[x] += val; x += lowbit(x); } }
ll query(int x) { ll ans = 0; while(x) { ans += t[x]; x -= lowbit(x); } return ans; }
inline ll Mod(ll ans, ll p) { return ans >= p ? ans % p + p : ans; }
ll quick_pow(ll a, ll b, ll p) { ll ans = 1; a = Mod(a, p); while(b) { if(b & 1) ans = Mod(ans * a, p); a = Mod(a * a, p); b >>= 1; } return ans; }
ll dfs(ll p, int l, int r) { ll temp = query(l); if(l == r p == 1) return Mod(temp, p); ll ans = dfs(phi[p], l + 1, r); return quick_pow(temp, ans, p); }
void solve() { init(); cin >> n >> m; for(int i = 1;i <= n; i++) { int x; cin >> x; update(i, x); update(i + 1, -x); } while(m--) { int opt; cin >> opt; if(opt == 1) { int l, r; ll x; cin >> l >> r >> x; update(l, x); update(r + 1, -x); } else { int l, r; ll p; cin >> l >> r >> p; cout << dfs(p, l, r) % p << endl; } } }
signed main() { solve(); }
|