L Euler Function
题解
线段树维护区间欧拉函数和。
代码
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
#include <bits/stdc++.h> #define lc rt << 1 #define rc rt << 1 | 1 using namespace std; typedef long long LL; const int N = 100009; const int M = 109; const LL Mod = 998244353; LL p[M]; class SegmentTree { public: void build(int l, int r, int rt) { t[rt].val = t[rt].lzy = 1LL; t[rt].tag.reset(); if(l>=r) return; int mid = (l + r) >> 1; build(l, mid, lc); build(mid + 1, r, rc); pushup(rt); } void update(int l, int r, int rt, int L, int R, int ip, int k) { if(l > r || r < L || l > R || L > R) return; if(L <= l && r <= R && t[rt].tag[ip]) { for (int i = 1; i <= k; i++) { (t[rt].val *= p[ip]) %= Mod; (t[rt].lzy *= p[ip]) %= Mod; } return; } if(l==r) { (t[rt].val *= p[ip] - 1LL) %= Mod; (t[rt].lzy *= p[ip] - 1LL) %= Mod; t[rt].tag[ip] = 1; for (int i = 1; i < k; i++) { (t[rt].val *= p[ip]) %= Mod; (t[rt].lzy *= p[ip]) %= Mod; } return; } int mid = (l + r) >> 1; pushdown(rt); update(l, mid, lc, L, R, ip, k); update(mid + 1, r, rc, L, R, ip, k); pushup(rt); return; } LL query(int l, int r, int rt, int L, int R) { if(r < L || l > R || l > r || L > R) return 0; if(L <= l && r <= R) return t[rt].val % Mod; int mid = (l + r) >> 1; pushdown(rt); LL lval = query(l, mid, lc, L, R); LL rval = query(mid + 1, r, rc, L, R); pushup(rt); return (lval + rval) % Mod; } private: struct Node { LL val, lzy; bitset<30> tag; }; Node t[4 * N]; void pushdown(int rt) { (t[lc].val *= t[rt].lzy) %= Mod; (t[lc].lzy *= t[rt].lzy) %= Mod; (t[rc].val *= t[rt].lzy) %= Mod; (t[rc].lzy *= t[rt].lzy) %= Mod; t[rt].lzy = 1LL; } void pushup(int rt) { t[rt].val = (t[lc].val + t[rc].val) % Mod; t[rt].tag = t[lc].tag & t[rc].tag; } }; SegmentTree t; bitset<30> g[M]; int cnt, n, m; void init() { for (int i = 2; i <= 100; i++) { bool flag = 1; for (int j = 2; j * j <= i; j++) { if(i % j) continue; flag = 0; break; } if(!flag) continue; p[++cnt] = i; } } int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); freopen("output.out", "w", stdout); #endif init(); scanf("%d%d", &n, &m); t.build(1, n, 1); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); for (int j = 1; j <= cnt; j++) { int tmp = 0; while(x % p[j] == 0) tmp++, x /= p[j]; if(tmp) t.update(1, n, 1, i, i, j, tmp); } } for (int i = 1; i <= m; i++) { int op; scanf("%d", &op); if(op == 0) { int l, r, x; scanf("%d%d%d", &l, &r, &x); for (int j = 1; j <= cnt; j++) { int tmp = 0; while(x % p[j] == 0) tmp++, x /= p[j]; if(tmp) t.update(1, n, 1, l, r, j, tmp); } } else { int l, r; scanf("%d%d", &l, &r); LL ans = t.query(1, n, 1, l, r); printf("%lld\n", ans); } } return 0; } |
叨叨几句... NOTHING