原文链接https://www.cnblogs.com/zhouzhendong/p/CF1109E.html

题意

给定一个长度为 n 的数列 a,以及一个模数 M(不一定是质数)。

要求支持 q 次以下操作:

区间乘

单点除(保证能够整除)

区间求和,最终结果对 M 取模输出。

$$n,q\leq 10^5$$

题解

这里我们设 $f(x)$ 表示 $\frac x {\gcd(x,M)}$ 。

令 $c(x,i)$ 表示 x 最多能够整除 M 的第 $i$ 个质因子的 $c(x,i)$ 次方。

线段树维护一下单点 $f(x)$ 值,以及所有单点 $c(x,i)$;再维护一下所有区间和就好了。

暴写一通,注意取模。这题为什么会放E……

代码

#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
using namespace std;
typedef long long LL;
LL read(){
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f|=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=100005;
int n,mod,phi;
int a[N];
vector <int> fenjie(int n){
static vector <int> ans;
ans.clear();
for (int i=2;i*i<=n;i++)
while (n%i==0)
n/=i,ans.push_back(i);
if (n>1)
ans.push_back(n);
return ans;
}
void Unique(vector <int> &v){
sort(v.begin(),v.end());
int u=unique(v.begin(),v.end())-v.begin();
while (v.size()>u)
v.pop_back();
}
int Phi(int n){
int ans=n;
for (int i=2;i*i<=n;i++)
if (n%i==0){
ans=ans/i*(i-1);
while (n%i==0)
n/=i;
}
if (n>1)
ans=ans/n*(n-1);
return ans;
}
int Pow(int x,int y,int mod=::mod){
int ans=1;
for (;y;y>>=1,x=(LL)x*x%mod)
if (y&1)
ans=(LL)ans*x%mod;
return ans;
}
int Inv(int x){
return Pow(x,phi-1);
}
vector <int> pm;
const int S=40;
int pcnt;
int ti[N<<2][S],val[N<<2],add[N<<2],nad[N<<2];
void pushdown(int rt){
int ls=rt<<1,rs=ls|1;
for (int i=1;i<=pcnt;i++){
ti[ls][i]+=ti[rt][i];
ti[rs][i]+=ti[rt][i];
ti[rt][i]=0;
}
val[ls]=(LL)val[ls]*add[rt]%mod;
val[rs]=(LL)val[rs]*add[rt]%mod;
add[ls]=(LL)add[ls]*add[rt]%mod;
add[rs]=(LL)add[rs]*add[rt]%mod;
add[rt]=1;
nad[ls]=(LL)nad[ls]*nad[rt]%mod;
nad[rs]=(LL)nad[rs]*nad[rt]%mod;
nad[rt]=1;
}
void pushup(int rt){
val[rt]=(val[rt<<1]+val[rt<<1|1])%mod;
}
void Get(int v,int &ans,int *c){
for (int i=1;i<=pcnt;i++)
while (v%pm[i-1]==0)
v/=pm[i-1],c[i]++;
ans=v%mod;
}
void build(int rt,int L,int R){
add[rt]=nad[rt]=1,clr(ti[rt]);
if (L==R){
val[rt]=a[L]%mod;
Get(a[L],nad[rt],ti[rt]);
return;
}
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
build(ls,L,mid);
build(rs,mid+1,R);
pushup(rt);
}
void update1(int rt,int L,int R,int xL,int xR,int v,int *d,int nv){
if (R<xL||L>xR)
return;
if (xL<=L&&R<=xR){
for (int i=1;i<=pcnt;i++)
ti[rt][i]+=d[i];
val[rt]=(LL)val[rt]*v%mod;
add[rt]=(LL)add[rt]*v%mod;
nad[rt]=(LL)nad[rt]*nv%mod;
return;
}
pushdown(rt);
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
update1(ls,L,mid,xL,xR,v,d,nv);
update1(rs,mid+1,R,xL,xR,v,d,nv);
pushup(rt);
}
int Calc(int v,int *c){
for (int i=1;i<=pcnt;i++)
v=(LL)v*Pow(pm[i-1],c[i])%mod;
return v;
}
void update2(int rt,int L,int R,int x,int v,int *d,int nv){
if (L==R){
nad[rt]=(LL)nad[rt]*Inv(nv)%mod;
for (int i=1;i<=pcnt;i++)
ti[rt][i]-=d[i];
val[rt]=Calc(nad[rt],ti[rt]);
return;
}
pushdown(rt);
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
if (x<=mid)
update2(ls,L,mid,x,v,d,nv);
else
update2(rs,mid+1,R,x,v,d,nv);
pushup(rt);
}
int Query(int rt,int L,int R,int xL,int xR){
if (R<xL||L>xR)
return 0;
if (xL<=L&&R<=xR)
return val[rt];
pushdown(rt);
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
return (Query(ls,L,mid,xL,xR)+Query(rs,mid+1,R,xL,xR))%mod;
}
int main(){
n=read(),mod=read(),phi=Phi(mod);
for (int i=1;i<=n;i++)
a[i]=read();
pm=fenjie(mod);
Unique(pm);
pcnt=pm.size();
build(1,1,n);
int q=read();
while (q--){
static int cc[S];
int type=read(),a=read(),b=read();
clr(cc);
if (type==1){
int x=read(),v;
Get(x,v,cc);
update1(1,1,n,a,b,x,cc,v);
}
else if (type==2){
int v;
Get(b,v,cc);
update2(1,1,n,a,b,cc,v);
}
else {
printf("%d\n",Query(1,1,n,a,b));
}
}
return 0;
}

  

Codeforces 1109E. Sasha and a Very Easy Test 线段树的更多相关文章

  1. CodeForces 1109E. Sasha and a Very Easy Test

    题目简述:给定$m \leq 10^9+9$,维护以下操作 1. "1 l r x":将序列$a[l], a[l+1], \dots, a[r]$都乘上$x$. 2. " ...

  2. Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树

    如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...

  3. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  4. Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树

    E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  5. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  6. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  7. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  9. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

随机推荐

  1. 2.1 re 模块

    正则表达式 简单的范围的字符组 字符 量词 字符集 分组 转义字符 flags 方法  .findall() .finditer() .search() .match()  .sub() .subn( ...

  2. Magento2自定义命令

    命令命名准则 命名指南概述 Magento 2引入了一个新的命令行界面(CLI),使组件开发人员能够插入模块提供的命令. Command name Command name 在命令中,它紧跟在命令的名 ...

  3. 「洛谷3292」「BZOJ4568」「SCOI2016」幸运数字【倍增LCA+线性基+合并】

    [bzoj数据下载地址]不要谢我 先讲一下窝是怎么错的... \(MLE\)是因为数组开小了.. 看到异或和最大,那么就会想到用线性基. 如果不会线性基的可以参考一下我的学习笔记:「线性基」学习笔记a ...

  4. 2018年秋季学期《c语言程序设计》编程总结

    <c语言程序设计>第四周编程总结 <c语言程序设计>第五周编程总结 <c语言程序设计>第六周编程总结 <c语言程序设计>第七周编程总结 <c语言程 ...

  5. 关于QQ农场牧场等曾经流行的游戏的一些见解

    大概在上上周,我偶然间打开QQ空间玩了一会QQ农牧场,玩完之后我在想,在那个年代他们为什么那么红? 我觉得可能有以下几点: 1:凭借着QQ海量的用户,可以迅速推广 2:迎合了人们爱占小便宜的心理,不过 ...

  6. PYTHON使用入门

    一 写在开头1.1 本文内容PYTHON语言的基础知识. 二 Q & A2.1 为什么选择PYTHON?软件质量:在很大程度上,PYTHON更注重可读性.一致性和软件质量,从而与脚本语言世界中 ...

  7. Geometric regularity criterion for NSE: the cross product of velocity and vorticity 1: $u\times \om$

    在 [Chae, Dongho. On the regularity conditions of suitable weak solutions of the 3D Navier-Stokes equ ...

  8. webpack学习笔记——publicPath路径问题

    output: { filename: "[name].js", path:path.resolve(__dirname,"build") } 如果没有指定pu ...

  9. Vue项目中使用基于Vue.js的移动组件库cube-ui

    cube-ui 是滴滴公司的技术团队基于 Vue.js 实现的精致移动端组件库.很赞,基本场景是够用了,感谢开源!感谢默默奉献的你们. 刚爬完坑,就来总结啦!!希望对需要的朋友有小小的帮助. (一)创 ...

  10. Centos7 安装 tree

    Centos7 安装 tree 用命令 yum 安装  tree yum -y install tree