2 條題解
-
1
这题一堆运算是没有什么性质或者转化的,只能维护每个输入对应的输出。
测试点 1~3
需要特殊用暴力处理,因为那里的 ,可能会有大质数出现,无法用线段树维护。
测试点 6~7
线段树每个节点维护一个
val数组, 表示在这段区间内,将放在表达式的开头能得到的结果。上传信息时,设左儿子的数组是 ,右儿子是 ,则它本身的数组可以这样得到:
表示先算左儿子区间里的运算,结果放到右儿子里再运算。
时间复杂度 。
100%
发现题目给的模数都不是质数,比如 ,。考虑将 拆成几个互质的更小的数,分别用线段树求出结果对于它们取模的值,然后再用 CRT 合并。
做完了。设为模数互质的因子和,则时间复杂度 。
有一点要注意:指数需要用扩展欧拉定理缩小到级别,否则如果序列里全是
^,复杂度里的 就会变成 ,虽然不是双log ,但仍容易被卡 T。#include<cstdio> #include<cstring> #include<cstdlib> #include<vector> #include<iostream> typedef long long LL; using namespace std; inline void read(int &x) { x = 0; bool f = 0; char c = getchar(); for (; c < '0' || c > '9'; c = getchar())if (c == '-') f = !f; for (; c >= '0' && c <= '9'; c = getchar())x = x * 10 + c - '0'; if(f)x=-x; } void write(int n) { if (n < 0) { putchar('-'), write(-n); } else { if (n / 10) write(n / 10); putchar(n % 10 + '0'); } } const int maxn=2e5+5,numP=7; int a[maxn],ny[numP],pr[numP],cnt,leaf[maxn<<2]; char op[maxn]; int n,m,P,tid,phi; struct node{ int v[numP][30]; }t[maxn<<2]; inline LL Pow(LL a,LL b,LL P) { LL res = 1; while (b) { if (b & 1)res = res * a % P; a = a * a % P; b >>= 1; } return res; } inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } inline LL exgcd(int a,int b,LL &x, LL &y) { if (!b) { x = 1, y = 0; return a; } int g = exgcd(b, a % b, x, y); LL t = x; x = y; y = t - a / b * y; return g; } inline void push_up(int k) { for (int i = 0; i < cnt; ++i) for (int j = 0; j < pr[i]; ++j)//j放在左侧得到的结果 t[k].v[i][j] = t[k << 1 | 1].v[i][t[k << 1].v[i][j]]; } inline void update(int idx,int k) { if (op[idx] == '+') { for (int i = 0; i < cnt; ++i) for (LL j = 0; j < pr[i]; ++j)//j放在左侧得到的结果 t[k].v[i][j] = (j + a[idx]) % pr[i]; } else if (op[idx] == '*') { for (int i = 0; i < cnt; ++i) for (LL j = 0; j < pr[i]; ++j)//j放在左侧得到的结果 t[k].v[i][j] = (j * a[idx]) % pr[i]; } else { //if(op[idx]=='^') if (a[idx])a[idx] = a[idx] % phi + phi; for (int i = 0; i < cnt; ++i) for (LL j = 0; j < pr[i]; ++j)//j放在左侧得到的结果 t[k].v[i][j] = Pow(j, a[idx], pr[i]); } } inline void build(int l,int r,int k) { if (l == r) { update(l, k); leaf[l] = k; return; } int mid = (l + r) >> 1; build(l, mid, k << 1); build(mid + 1, r, k << 1 | 1); push_up(k); } inline void modify(int x) { update(x, leaf[x]); for (x = leaf[x] >> 1; x; x >>= 1)push_up(x); } int main() { freopen("expr.in", "r", stdin); freopen("expr.out", "w", stdout); read(tid); read(n); read(m); read(P); for (int i = 1; i <= n; ++i) { char c = getchar(); while (c != '+' && c != '*' && c != '^')c = getchar(); op[i] = c; read(a[i]); } if (tid < 4) { while (m--) { int opt; read(opt); if (opt == 2) { int i; read(i); char c = getchar(); while (c != '+' && c != '*' && c != '^')c = getchar(); op[i] = c; read(a[i]); } else { int x; read(x); LL res = x; for (int i = 1; i <= n; ++i) { if (op[i] == '+')res = (res + a[i]) % P; else if (op[i] == '*')res = (res * a[i]) % P; else if (op[i] == '^')res = Pow(res, a[i], P); } write(res); putchar('\n'); } } } else { // for (int i = 1; i < P; ++i)if (gcd(i, P) == 1)++phi; LL ta = 0, tb = 0; int x = P, y = 0; cnt = 0; phi = P; for (LL i = 2; i * i <= x; ++i) { if (x % i == 0) { y = 1; while (x % i == 0)y *= i, x /= i; exgcd(P / y, y, ta, tb);//ta * P/y + tb * y = 1 ny[cnt] = (ta % y + y) % y;//逆元 pr[cnt] = y; ++cnt; phi = 1ll * phi * y / i * (i - 1); } } if (x > 1) { exgcd(P / x, x, ta, tb);//ta * P/y + tb * y = 1 ny[cnt] = (ta % x + x) % x; pr[cnt] = x; ++cnt; phi = 1ll * phi * (x - 1); } build(1, n, 1); while (m--) { int opt; read(opt); if (opt == 2) { int i; read(i); char c = getchar(); while (c != '+' && c != '*' && c != '^')c = getchar(); op[i] = c; read(a[i]); modify(i); } else { int x; read(x); LL res = 0; for (int i = 0; i < cnt; ++i) { res = (res + 1ll * P / pr[i] * ny[i] % P * t[1].v[i][x % pr[i]] % P) % P; } write(res), putchar('\n'); } } } return 0; }考察知识点
中国剩余定理 线段树
資訊
- ID
- 41
- 時間
- 4000ms
- 記憶體
- 512MiB
- 難度
- 9
- 標籤
- 遞交數
- 28
- 已透過
- 3
- 上傳者