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 ...
随机推荐
- GitHub上优秀Android 开源项目
GitHub在中国的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要.利用这些项目,有时能够让你 ...
- linux-2.6.22.6/Makefile:416: *** mixed implicit and normal rules: deprecated syntax
今天在按照韦东山大哥的教程流程编译内核的时候出现了这个问题 linux-2.6.22.6/Makefile:416: *** mixed implicit and normal rules: ...
- 192.168.28.168:3000打开网页无法调试localhost为前缀的接口
最近我在IIS上发布.net Core API的时候发现 当我用localhost去打开我的web项目时 并且,ajax调用的接口前缀为localhost时候,运行正确 但是当我用192.168.28 ...
- AngularJS(一):概述
本文也同步发表在我的公众号“我的天空” 在我们之前学习的前端代码编写过程中,总是通过HTML与CSS来进行页面布局,而使用JS来控制页面逻辑,因此,我们习惯于在JS中来操作页面元素,如以下代码,我们希 ...
- 巧用伪元素绘制带边的三角形--CSS3
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 单链表常见面试题(C语言实现)
总结常见的单链表操作函数,复习使用,仅供参考,代码调试通过. #include<stdio.h> typedef struct node{ int data; struct node *n ...
- UVA 11988 Broken Keyboard (链表)
简单题,题目要求显然是很多次插入,所以是链表. 插入两个语句,nxt[i] = nxt[u] 表示 i结点指向u的后继, nxt[u] = i表示把u的后继设成i. 设置一个头结点,指向一个不存在的结 ...
- codeforce Gym 100500I Hall of Fame (水)
题意:统计一些串中,字母的出现频率,不分大小写,找出现频率最高5个字符(相同频率优先取字典序大的),把他们的对应的值加起来判断以下是否大于62. 没出现的不算. #include<cstdio& ...
- 前端安全系列(二):如何防止CSRF攻击?
前端安全系列(二):如何防止CSRF攻击? 背景 随着互联网的高速发展,信息安全问题已经成为企业最为关注的焦点之一,而前端又是引发企业安全问题的高危据点.在移动互联网时代,前端人员除了传统的 XS ...
- js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了
js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了