An easy problem

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1553    Accepted Submission(s): 697

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.
 
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.

 
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.
 
Sample Input
1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7
 
Sample Output
Case #1:
2
1
2
20
10
1
6
42
504
84
 
思路:该题用long long 类型运算会溢出,结果WA。用高精度会TLE。线段树思路:第i个叶子结点维护的是第i个操作.若为乘运算则将结点的值修改为乘数,若为除运算则将结点的值修改为1.父结点维护左右子结点之积。每次输出结果为根节点的值。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=;
typedef long long ll;
struct Node{
ll val;
int l,r;
}a[MAXN*];
int n,mod;
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].val=;
if(l==r)
{
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
}
void update(int rt,int pos,int val)
{
if(a[rt].l==pos&&a[rt].r==pos)
{
a[rt].val=val%mod;
return ;
}
int mid=(a[rt].l+a[rt].r)>>;
if(pos<=mid)
{
update(rt<<,pos,val);
}
else
{
update((rt<<)|,pos,val);
}
a[rt].val=(a[rt<<].val*a[(rt<<)|].val)%mod;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d",&n,&mod);
build(,,n);
printf("Case #%d:\n",cas);
for(int i=;i<=n;i++)
{
int type,val;
scanf("%d%d",&type,&val);
if(type==)
{
update(,i,val);
}
else
{
update(,val,);
}
printf("%lld\n",a[].val);
}
}
return ;
}
 

最新文章

  1. Ubuntu下查看机器信息
  2. distribution数据库过大问题
  3. Octopus系列之各个页面调用示例2
  4. Win8.1 远程桌面 凭据无法工作
  5. 修复浏览器不支持Array自带的indexOf方法的扩展
  6. Qt + CURL + mimetic 发送邮件(带附件)
  7. 玩玩hibernate
  8. Android(java)学习笔记183:判断SD卡状态和存储空间大小
  9. 监视/etc/passwd文件是否正常
  10. POJ 2449 Remmarguts&#39; Date (SPFA + A星算法) - from lanshui_Yang
  11. 10gocm-&amp;gt;session5-&amp;gt;数据库管理实验-&amp;gt;GC资源管理器的资源消耗组介绍
  12. 使用React改版网站后的一些感想
  13. Python查看MQ队列深度
  14. List之Union(),Intersect(),Except() 即并集,交集,差集运算。
  15. 【Redis篇】初始Redis与Redis安装
  16. C语言fprintf, fwrite, fscanf, fread混用问题
  17. JavaScript test() 方法
  18. Hexo NexT 博客本地搭建指南
  19. 6.简单提取小红书app数据保存txt-2
  20. 洛谷 P2233 [HNOI2002]公交车路线 解题报告

热门文章

  1. 大话设计模式之PHP篇 - 观察者模式
  2. 《Inode与Block重要知识总结核心讲解》【转】
  3. java assert的使用并深入解析Java的assertion
  4. SQl查询基础
  5. BZOJ4197 / UOJ129 [Noi2015]寿司晚宴
  6. UOJ104 【APIO2014】Split the sequence
  7. python基础5 - 产生随机数
  8. 2017-02-23 错误信息:未在本地计算机上注册“Microsoft.ACE.oledb.12.0”提供程序。
  9. python 各种装饰器示例(python3)
  10. C#下利用正则表达式实现字符串搜索功能的方法(转)