杭电5475/2015 ACM/ICPC Asia Regional Shanghai Online An easy problem(线段树过)
来源:互联网 发布:linux java开发 编辑:程序博客网 时间:2024/04/28 02:47
An easy problem
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1137 Accepted Submission(s): 557
Problem Description
One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first, X = 1. This calculator only supports two types of operation.
1. multiply X with a number.
2. divide X with a number which was multiplied before.
After each operation, please output the number X modulo M.
1. multiply X with a number.
2. divide X with a number which was multiplied before.
After each operation, please output the number X modulo M.
Input
The first line is an integer T(1≤T≤10 ), indicating the number of test cases.
For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (1≤Q≤105,1≤M≤109 )
The next Q lines, each line starts with an integer x indicating the type of operation.
if x is 1, an integer y is given, indicating the number to multiply. (0<y≤109 )
if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)
It's guaranteed that in type 2 operation, there won't be two same n.
For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (
The next Q lines, each line starts with an integer x indicating the type of operation.
if x is 1, an integer y is given, indicating the number to multiply. (
if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)
It's guaranteed that in type 2 operation, there won't be two same n.
Output
For each test case, the first line, please output "Case #x:" and x is the id of the test cases starting from 1.
Then Q lines follow, each line please output an answer showed by the calculator.
Then Q lines follow, each line please output an answer showed by the calculator.
Sample Input
110 10000000001 22 11 21 102 32 41 61 71 122 7
Sample Output
Case #1:2122010164250484
这是一道怎么说的题目呢。刚开始看到5000MS,直接暴力去做。
然后我得到了这样的一个思路:(暴力思路)
一步一步乘啊乘,乘啊乘,除啊除,除啊除。
然后感觉好二,因为我是和我队友一起做,他跟我提出个道理,除掉哪组数据,就相当于不乘这组数据。因为毕竟除数据是会有纰漏的,会有精度消耗之类的不可避免的问题。然后还是暴力解:乘啊乘乘啊乘,标记啊标记,标记啊标记。然后就TLE了~
后来发现,如果是求数据乘积,我可以利用线段树去做啊~然后就开始构思:因为是n组数据,一共所有数也就n个,因为是乘积,所以我初始化为1就行:
void pushup(int rt){ tree[rt]=(tree[rt<<1]%m*tree[rt<<1|1]%m)%m;//别忘记求余}void build( int l ,int r , int rt ){if( l == r ){tree[rt]=1;return ;}else{int m = (l+r)>>1 ;build(lson) ;build(rson) ;pushup(rt) ;}}
然后是数据修改:如果是操作1,表示要乘上这个数,对应树上的位子改变数据为要乘的数就行,相反的如果是操作2,表示有某一组数据相当于不乘,那么相对应的位子改为乘1就行了:
void update(int p,int c,int l,int r,int rt)//p表示位子,c表示更改数据{ if(l==r) { tree[rt]=c; } else { int m=(l+r)>>1; if(p<=m) update(p,c,lson); else update(p,c,rson); pushup(rt); }}
因为是求树中所有数的乘积,那么我们输出tree【1】就行了,这里是完整AC代码:
#include<stdio.h>#include<string.h>using namespace std;#define lson l,m,rt*2#define rson m+1,r,rt*2+1__int64 tree[1212121];int n,m;void pushup(int rt){ tree[rt]=(tree[rt<<1]%m*tree[rt<<1|1]%m)%m;}void build( int l ,int r , int rt ){if( l == r ){tree[rt]=1;return ;}else{int m = (l+r)>>1 ;build(lson) ;build(rson) ;pushup(rt) ;}}void update(int p,int c,int l,int r,int rt){ if(l==r) { tree[rt]=c; } else { int m=(l+r)>>1; if(p<=m) update(p,c,lson); else update(p,c,rson); pushup(rt);//printf("sum[%d]:%d\n",rt,tree[rt]); }}int main(){ int t; int kase=0; scanf("%d",&t); while(t--) { memset(tree,0,sizeof(tree)); scanf("%d%d",&n,&m); build(1,n,1); printf("Case #%d:\n",++kase); for(int i=0;i<n;i++) { int q,c; scanf("%d%d",&q,&c); if(q==1) { update(i+1,c,1,n,1); printf("%I64d\n",tree[1]%m); } if(q==2) { update(c,1,1,n,1); printf("%I64d\n",tree[1]%m); } } }}
0 0
- 杭电5475/2015 ACM/ICPC Asia Regional Shanghai Online An easy problem(线段树过)
- HDU 5475 2015 ACM/ICPC Asia Regional Shanghai Online An easy problem(线段树)
- 2015 ACM/ICPC Asia Regional Shanghai Online An easy problem(简单题)
- HDU 5475.An easy problem【思路】【2015 ACM/ICPC Asia Regional Shanghai Online】【9月26】
- HDU 5475 An easy problem(用大数模板,你就上当了)——2015 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online
- 2014 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online 1010题
- 2015 上海网络赛 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online 1008
- hdu 5477(2015 ACM/ICPC Asia Regional Shanghai Online)
- hdu 5478(2015 ACM/ICPC Asia Regional Shanghai Online )
- 2015 ACM/ICPC Asia Regional Shanghai Online --HDU 5478
- 2015 ACM/ICPC Asia Regional Shanghai Online A Sweet Journey(简单题)
- HDU 5478 Can you find it (卡常数)2015 ACM/ICPC Asia Regional Shanghai Online
- SQL添加表字段
- android 4.0 到 4.4的不同(我碰到的)
- Perspective Correction(透视矫正)
- OpenStack网络指导手册 -基本网络概念
- 关于阿里巴巴一道笔试题
- 杭电5475/2015 ACM/ICPC Asia Regional Shanghai Online An easy problem(线段树过)
- Oracle 12c安装及RHCS高可用配置
- 在代码中改变TextView的drawableLeft的图片资源
- RxJava之lift()变换原理及实现自定义 Operator
- ubuntu安装ssh和telnet服务器
- linux-mysql-insert1
- servlet读取properties文件
- recyclerview的简单使用
- Arduino和Android通过OTG 通信