HDU 5238 Calculator 线段树 中国剩余定理
题意:
给一个计算器,有一系列计算步骤,只有加,乘,幂三种运算。
有一种查询操作:查询初始值为\(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 线段树 中国剩余定理的更多相关文章
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- 《孙子算经》之"物不知数"题:中国剩余定理
1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数. 如果 m ...
- POJ 1006 中国剩余定理
#include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...
- [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...
- poj1006中国剩余定理
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103506 Accepted: 31995 Des ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
随机推荐
- Oracle批量SQL之 BULK COLLECT 子句
BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNING ...
- 【转载】Ubuntu16.04安装最新版nodejs
安装最新版nodejs 更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties softw ...
- 复习KMP
KMP刚学的时候,看不懂. 再看,哇!原来是这样! 用的时候,忘了. 为了不再跌倒,我决定,记住吧... 在我看来,KMP一般用于字符串匹配时的防超时优化. 他的精髓就是,利用已经匹配的信息,简化这之 ...
- App配置页面头部
记录一下 App配置页面头部 例 上图红色框部分就是自己配置的头部 //我的客户 "/OACustomer/Index": { title: "我的客户", h ...
- js动态更换img的src问题
在本地开发测试过程中,通过js动态更换img的src没有问题,图片正常切换,但是放在服务器上后测试发现,图片不显示,解决方法为:在对应onclick事件执行切换图片的js函数后加上一个return f ...
- Spring 设计原则
Spring 框架有四大原则(Spring所有的功能和设计和实现都基于四大原则): 1. 使用POJO进行轻量级和最小侵入式开发. 2. 通过依赖注入和基本接口编程实现松耦合. 3. 通过AOP和基于 ...
- 22/tcp open|filtered ssh 80/tcp open|filtered http
22/tcp open|filtered ssh80/tcp open|filtered http nmap不能确定该端口是打开还是过滤,这可能是扫描一个打开的端口,但没有回应.
- [Oracle 视图] ALL_OBJECTS
ALL_OBJECTS ALL_OBJECTS describes all objects accessible to the current user. ALL_OBJECTS描述当前用户的可访问的 ...
- BZOJ 4491: 我也不知道题目名字是什么 RMQ
4491: 我也不知道题目名字是什么 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 317 Solved: 174[Submit][Status][ ...
- hadoop相关资料集锦
1 Hadoop集群系列集锦http://www.cnblogs.com/xia520pi/archive/2012/04/08/2437875.html 2 Hadoop和MapReduce详解ht ...