题意:

给一个计算器,有一系列计算步骤,只有加,乘,幂三种运算。

有一种查询操作:查询初始值为\(x\)的时候,最终运算结果模\(29393\)的值。

有一种修改操作:可以修改第\(p\)个运算的运算符和运算数。

分析:

分解一下,\(29393=7 \times 13 \times 17 \times 19\)。

所以我们可以维护\(4\)棵线段树,区间维护的信息就是初始值为\(x\)经过这段区间最终得到的值。

然后就用中国剩余定理整合一下。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 50000 + 10;
const int maxnode = maxn * 4; const int prime[] = { 7, 13, 17, 19 }; int val[4][20][maxnode];
int n, m;
char op[maxn], tmp[5];
int x[maxn]; int pow_mod(int a, int b, int mod) {
int ans = 1;
while(b) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
} int calc(int a, char op, int b, int mod) {
if(op == '+') return ((a + b) % mod);
if(op == '*') return a * b % mod;
return pow_mod(a, b, mod);
} void pushup(int o) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < prime[i]; j++) {
int t = val[i][j][o<<1];
val[i][j][o] = val[i][t][o<<1|1];
}
} void build(int o, int L, int R) {
if(L == R) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < prime[i]; j++)
val[i][j][o] = calc(j, op[L], x[L], prime[i]);
return;
}
int M = (L + R) / 2;
build(o<<1, L, M);
build(o<<1|1, M+1, R);
pushup(o);
} void update(int o, int L, int R, int p) {
if(L == R) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < prime[i]; j++)
val[i][j][o] = calc(j, op[p], x[p], prime[i]);
return;
}
int M = (L + R) / 2;
if(p <= M) update(o<<1, L, M, p);
else update(o<<1|1, M+1, R, p);
pushup(o);
} void gcd(int a, int b, int& d, int& x, int& y) {
if(!b) { d = a; x = 1; y = 0; }
else { gcd(b, a%b, d, y, x); y -= x*(a/b); }
} int a[4];
int CRT() {
int M = 29393, d, y, x = 0;
for(int i = 0; i < 4; i++) {
int w = M / prime[i];
gcd(prime[i], w, d, d, y);
x = (x + y*w*a[i]) % M;
}
return (x+M)%M;
} int main()
{
int T; scanf("%d", &T);
for(int kase = 1; kase <= T; kase++) {
printf("Case #%d:\n", kase);
scanf("%d%d", &n, &m); getchar();
for(int i = 1; i <= n; i++) {
scanf("%c%d", op + i, x + i);
getchar();
}
build(1, 1, n); while(m--) {
int cmd, p;
scanf("%d%d", &cmd, &p);
if(cmd == 1) {
for(int i = 0; i < 4; i++)
a[i] = val[i][p%prime[i]][1];
printf("%d\n", CRT());
} else {
getchar();
scanf("%c%d", op + p, x + p);
update(1, 1, n, p);
}
}
} return 0;
}

HDU 5238 Calculator 线段树 中国剩余定理的更多相关文章

  1. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  2. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  3. hdu 3579 Hello Kiki 不互质的中国剩余定理

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  4. 《孙子算经》之"物不知数"题:中国剩余定理

    1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数.  如果 m ...

  5. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  6. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...

  7. poj1006中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103506   Accepted: 31995 Des ...

  8. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  9. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

随机推荐

  1. 数据结构之Hyperloglog

    前置知识 调和平均数 通常我们求一堆数的平均数 就是把一堆数加起来除以这堆数的数量,如 x1, x2, x3, x4, .... ,xn的平均数 H = (x1 + x2 + x3 + x4 + xn ...

  2. Spring MVC 示例

    Srping MVC项目结构如下: 一.首先创建一个Dynamic Web Project 二.WebContent/WEB-INF/文件夹下新增 web.xml,配置servlet 容器对于web. ...

  3. UIScrollView使用stoboard自动布局

    使用stoboard布局 scrollView 是有点麻烦的,首先我们往往约束好一个 scrollView 然后在添加子控件,此时都会报错,原因是, scrollView必须确定滚动范围 然后在使用V ...

  4. 通用的MIME类型:application/octet-stream

    按照内容类型排列的 Mime 类型列表 类型/子类型 扩展名 application/envoy evy application/fractals fif application/futurespla ...

  5. 【UML】使用环境(转)

    http://blog.csdn.net/sds15732622190/article/details/49404169 用例图         用例图是在需求文档中使用的,但一定要配合用例一同使用. ...

  6. UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)

    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 先预处理出每个元素之前和之后相同元素出现的位置,就可以在O(1)的 ...

  7. 剑指offer58 二叉树的下一个结点

    自己写的 class Solution { public: TreeLinkNode* GetNext(TreeLinkNode* pNode) { if(pNode == NULL) return ...

  8. JavaScript -- 内置对象数组

    数组 创建数组的基本方式有两种: 1.使用 Array构造函数 语法:new Array() 小括号( )说明: (1)预先知道数组要保存的项目数量 (2)向Array构造函数中传递数组应包含的项 2 ...

  9. 解决vs 编译的bug“请检查是否是磁盘空间不足、路径无效或权限不够”

    昨晚用vs编译遇到一个问题,编译一半发现硬盘没空间,一直重启vs,重启电脑, 删除pdb文件都没用,之后尝试重新生成解决方案,就解决了.这个是vs的一个bug

  10. odoo前端

    bootstrap: http://www.runoob.com/bootstrap/bootstrap-tutorial.html javascript: http://www.runoob.com ...