2 条题解
-
1
拿到这么大一坨运算头都晕了,所以先写暴力,可以通过测试点
我们发现,这些运算没有什么可以转化的地方,注意到测试点 的模数较小,所以一段运算过后的结果数也较少,因此考虑用一棵线段树维护每个值在运算之后变成了什么,合并方式为 ,时间复杂度
我们通过瞪眼法发现后面的模数都不是质数,且最大质因子才 ,因此启发我们对模数进行分解,对于每部分都建一棵上述的线段树,查询时查出每种模数转移后的结果,用一些方法把它们合起来,那么是什么方法呢?中国剩余定理!
时间复杂度 ,可以通过本题
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII; const int N = 2e5 + 5; int id,n,q,mod; int a[N]; char s[N]; int ksm(int a,int b,int mod) { int ans = 1; for(;b;b >>= 1,a = 1LL * a * a % mod) if(b & 1) ans = 1LL * ans * a % mod; return ans; } int exgcd(int a,int b,int &x,int &y){ if(!b) return x = 1,y = 0,a; int d = exgcd(b,a % b,y,x); return y -= a / b * x,d; } namespace BF { void solve() { while(q --) { ll x;cin >> x; if(x == 1) { cin >> x; for(int i = 1;i <= n;i ++) { if(s[i] == '*') x = x * a[i] % mod; if(s[i] == '^') x = ksm(x,a[i],mod); if(s[i] == '+') x = (x + a[i]) % mod; } cout << x << "\n"; } else { cin >> x >> s[x] >> a[x]; } } } } namespace TR { struct SegTree { int mod,tr[30][N << 2]; #define lson p << 1 #define rson p << 1 | 1 #define mid ((l + r) >> 1) void init(int p,char op,ll x) { for(int i = 0;i < mod;i ++) { if(op == '*') tr[i][p] = i * x % mod; if(op == '^') tr[i][p] = ksm(i,x,mod); if(op == '+') tr[i][p] = (i + x) % mod; } } void update(int p) { for(int i = 0;i < mod;i ++) { tr[i][p] = tr[tr[i][lson]][rson]; } } void build(int p,int l,int r) { if(l == r) return init(p,s[l],a[l]),void(0); build(lson,l,mid),build(rson,mid + 1,r); update(p); } void change(int p,int l,int r,int pos) { if(l == r) return init(p,s[l],a[l]),void(0); if(pos <= mid) change(lson,l,mid,pos); else change(rson,mid + 1,r,pos); update(p); } int query(int x) { return tr[x % mod][1]; } #undef lson #undef rson #undef mid } tr[4]; int m; void build() { for(int i = 0;i < m;i ++) { tr[i].build(1,1,n); } } void change(int pos) { for(int i = 0;i < m;i ++) { tr[i].change(1,1,n,pos); } } int query(int val) { ll ans = 0; for(int i = 0;i < m;i ++) { int x,y,p = tr[i].mod; exgcd(mod / p,p,x,y); ans += 1LL * tr[i].query(val) * mod / p % mod * ((x + p) % p); } return ans % mod; } void solve() { for(int i = 2,x = mod;x > 1;i ++) { if(x % i == 0) { int p = 1; while(x % i == 0) { p *= i,x /= i; } tr[m ++].mod = p; } } build(); while(q --) { int x;cin >> x; if(x == 1) { cin >> x; cout << query(x) << "\n"; } else { cin >> x >> s[x] >> a[x]; change(x); } } } } void solve() { cin >> id >> n >> q >> mod; for(int i = 1;i <= n;i ++) { cin >> s[i] >> a[i]; } // TR::solve(); if(id <= 3) BF::solve(); else TR::solve(); } signed main() { freopen("expr.in","r",stdin); freopen("expr.out","w",stdout); ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); int t = 1; // cin >> t; while(t --) solve(); return 0; } -
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; }考察知识点
中国剩余定理 线段树
- 1
信息
- ID
- 41
- 时间
- 4000ms
- 内存
- 512MiB
- 难度
- 9
- 标签
- 递交数
- 28
- 已通过
- 3
- 上传者