模意义下除法若结果仍为整数的话,可以记录模数的所有质因子,计算这些质因子的次幂数,剩余的exgcd解决。

$O(n\log n)$但有9的常数(1e9内的数最多有9个不同的质因子),T了。

 #include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=;
int T,n,mod,op,w,tot,res,x,d[N][],p[],s[]; void frac(int n){
for (int i=; i*i<=n; i++) if (n%i==){
p[++tot]=i;
while (n%i==) n/=i;
}
if (n>) p[++tot]=n;
} void exgcd(int a,int b,int &x,int &y){
if (!b) x=,y=;
else exgcd(b,a%b,y,x),y-=a/b*x;
} int main(){
freopen("bzoj5334.in","r",stdin);
freopen("bzoj5334.out","w",stdout);
for (scanf("%d",&T); T--; ){
scanf("%d%d",&n,&mod); tot=; res=; frac(mod);
rep(i,,) s[i]=;
rep(i,,n){
scanf("%d",&op);
if (op==){
scanf("%d",&w);
rep(j,,tot) d[i][j]=;
rep(j,,tot)
while (w%p[j]==) w/=p[j],d[i][j]++,s[j]++;
int x,y; res=1ll*res*w%mod;
exgcd(w,mod,x,y); d[i][]=(x%mod+mod)%mod;
int ans=res;
rep(j,,tot) rep(k,,s[j]) ans=1ll*ans*p[j]%mod;
printf("%d\n",ans);
}else{
scanf("%d",&x); res=1ll*res*d[x][]%mod;
rep(j,,tot) s[j]-=d[x][j];
int ans=res;
rep(j,,tot) rep(k,,s[j]) ans=1ll*ans*p[j]%mod;
printf("%d\n",ans);
}
}
}
return ;
}

删除操作难以维护的话,考虑线段树分治即可。

 #include<cstdio>
#include<algorithm>
#define ls (x<<1)
#define rs (ls|1)
#define lson ls,L,mid
#define rson rs,mid+1,R
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=;
int T,n,mod,x,op,tag[N<<];
struct P{ int l,r,v; }p[N]; void push(int x){
tag[ls]=1ll*tag[ls]*tag[x]%mod;
tag[rs]=1ll*tag[rs]*tag[x]%mod;
tag[x]=;
} void build(int x,int L,int R){
tag[x]=;
if (L==R) return;
int mid=(L+R)>>;
build(lson); build(rson);
} void ins(int x,int L,int R,int l,int r,int k){
if (L==l && r==R){ tag[x]=1ll*tag[x]*k%mod; return; }
int mid=(L+R)>>;
if (r<=mid) ins(lson,l,r,k);
else if (l>mid) ins(rson,l,r,k);
else ins(lson,l,mid,k),ins(rson,mid+,r,k);
} int que(int x,int L,int R,int pos){
if (L==R) return tag[x];
int mid=(L+R)>>; push(x);
if (pos<=mid) return que(lson,pos); else return que(rson,pos);
} int main(){
for (scanf("%d",&T); T--; ){
scanf("%d%d",&n,&mod);
rep(i,,n){
scanf("%d",&op);
if (op==) scanf("%d",&x),p[i]=(P){i,n,x};
else scanf("%d",&x),p[x].r=i-,p[i].l=;
}
build(,,n);
rep(i,,n) if (p[i].l) ins(,,n,p[i].l,p[i].r,p[i].v);
rep(i,,n) printf("%d\n",que(,,n,i));
}
return ;
}

[BZOJ5334][TJOI2018]数学计算(exgcd/线段树)的更多相关文章

  1. BZOJ5334 [TJOI2018] 数学计算 【线段树分治】

    题目分析: 大概是考场上的签到题.首先mod不是质数,所以不能求逆元.注意到有加入操作和删除操作.一个很典型的想法就是线段树分治.建立时间线段树然后只更改有影响的节点,最后把所有标记下传.时间复杂度是 ...

  2. 【BZOJ5334】数学计算(线段树)

    [BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...

  3. 洛谷P4588 [TJOI2018]数学计算 【线段树】

    题目链接 洛谷P4588 题解 用线段树维护即可 #include<algorithm> #include<iostream> #include<cstring> ...

  4. P4588 [TJOI2018]数学计算 (线段树)

    用线段树维护操作序列,叶子结点存要乘的数,非叶子结点存区间乘积,每次输出tr[1] 就是答案. 1 #include<bits/stdc++.h> 2 #define ll long lo ...

  5. BZOJ5334: [Tjoi2018]数学计算

    BZOJ5334: [Tjoi2018]数学计算 https://lydsy.com/JudgeOnline/problem.php?id=5334 分析: 线段树按时间分治即可. 代码: #incl ...

  6. BZOJ 5334--[Tjoi2018]数学计算(线段树)

    5334: [Tjoi2018]数学计算 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 220  Solved: 147[Submit][Status ...

  7. BZOJ5334:[TJOI2018]数学计算(线段树)

    Description 小豆现在有一个数x,初始值为1. 小豆有Q次操作,操作有两种类型:  1 m: x = x  *  m ,输出 x%mod; 2 pos: x = x /  第pos次操作所乘 ...

  8. 2018.06.26「TJOI2018」数学计算(线段树)

    描述 小豆现在有一个数 xxx ,初始值为 111 . 小豆有 QQQ 次操作,操作有两种类型: 111 $ m$ : x=x×mx=x×mx=x×m ,输出 xxx modmodmod MMM : ...

  9. [Tjoi2018]数学计算

    [Tjoi2018]数学计算 BZOJ luogu 线段树分治 是不是想问为什么不暴力做? 模数没说是质数,所以不一定有逆元. 然后就是要每次build一下把线段树权值init成1, 博猪不知道为什么 ...

随机推荐

  1. 面向对象 ( OO ) 的程序设计——理解对象

    本文地址:http://www.cnblogs.com/veinyin/p/7607938.html  1 创建自定义对象 创建自定义对象的最简单方法为创建 Object 的实例,并添加属性方法,也可 ...

  2. jquery 中$符号六大作用

    jquery 中$符号六大作用 2012-12-16 86市场网 javascript a.$用作选择器, var e = $("h1 a"); var f = $("t ...

  3. Knockout双向绑定

    knockout双工绑定基于 observe 模式,性能高.核心就是observable对象的定义.这个函数最后返回了一个也叫做 observable 的函数,也就是用户定义值的读写器(accesso ...

  4. ajax技术整理总结(1)

    1.创建ajax对象 var xhr=new XMLHttpRequest(); 4.监听状态信息 xhr.onreadystatechange=function(){ //4接收完毕 ){ docu ...

  5. You can fail at what you don't want, so you might as well take a chance on doing what you love.

    You can fail at what you don't want, so you might as well take a chance on doing what you love. 做不想做 ...

  6. PDFRender4NET的使用之pdf转图片

    同样的需要第三方的.dll,http://www.o2sol.com/pdfview4net/download.htm using O2S.Components.PDFRender4NET; usin ...

  7. postman中 form-data、x-www-form-urlencoded、raw、binary的区别 && 下载文件

    1.form-data:  就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件 ...

  8. geoip 扩展包根据ip定位详情

    教程:https://laravel-china.org/courses/laravel-package/2024/get-the-corresponding-geo-location-informa ...

  9. 3.rabbitmq 发布/订阅

    1. 发布者 #coding:utf8 import pika import json import sys message = ''.join(sys.argv[1:]) or "hell ...

  10. 安装scrapy 出错 building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required.

    安装Scrapy出现错误: building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required. ...