Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F
欧拉函数 + 区间更新线段树
题意
对一个序列(n<=4e5,a[i]<=300)两种操作:
1. 将a[l,r]的数字乘以x(x<=300)
2. 求\(\varphi(\prod_{i=l}^ra[i])\)对1e9+7取模
题解
欧拉函数性质
- 假如\(p\)是一个质数,\(\varphi(p)=p-1\),\(\varphi(p^k)=p^{k-1}*(p-1)=p^k*\frac{p-1}{p}\)
- 假如p,q互质,\(\varphi(p*q)=\varphi(p)*\varphi(q)\)
- 对于一个正整数n,\(\varphi(n)=n*\frac{p_1-1}{p_1}*...*\frac{p_n-1}{p_n}\)
- 对于每个数分开维护\(n\)和\(\frac{p_1-1}{p_1}*...*\frac{p_n-1}{p_n}\),因为所有数只有300大,有62位素数,所以可以用位运算维护后半部分,剩下前半部分就是维护数的乘积
处理
- 开两个标记数组,ly[]维护乘积的延迟标记,st[]维护位运算的延迟标记
- 每次区间操作找到合适的区间就直接修改,需要向下递归前才向下推标记
- 预处理出x(x<=300)每个数的素因子bit[],可以在埃式筛的过程中处理
- 查询的时候,需要用两个信息用array<ll,2>,
附上重载加号代码
array<ll,2> operator +(array<ll,2> a,array<ll,2> b){
return {a[0]*b[0]%P,a[1]|b[1]};
}
代码(区间修改线段树板子)
#include<bits/stdc++.h>
#define P 1000000007
#define ls (o<<1)
#define rs (o<<1|1)
#define ll long long
#define M 1600000
using namespace std;
ll x[M],s[M],ly[M],st[M];
ll bit[305],pr[305],inv[305];
int vi[305];
ll a[400005],tp,X,cnt,i;
int n,q,l,r;
char S[20];
array<ll,2>ans;
array<ll,2> operator +(array<ll,2> a,array<ll,2> b){
return {a[0]*b[0]%P,a[1]|b[1]};
}
ll pw(ll bs,ll x){
ll ans=1;while(x){if(x&1)ans=ans*bs%P;bs=bs*bs%P;x>>=1;}
return ans;
}
void push_up(int o){ //st数组和ly数组同理,作用为标记每次改变量
x[o]=x[ls]*x[rs]%P;
s[o]=s[ls]|s[rs];
ly[o]=1;
st[o]=0;
}
void push_down(int o,int l,int r){ //更新子节点信息,将本节点标记去掉
int mid=(l+r)/2;
if(ly[o]>1){
ly[ls]=ly[ls]*ly[o]%P;
ly[rs]=ly[rs]*ly[o]%P;
s[ls]=s[ls]|st[o];
st[ls]=st[ls]|st[o];
s[rs]=s[rs]|st[o];
st[rs]=st[rs]|st[o];
x[ls]=x[ls]*pw(ly[o],mid-l+1)%P;
x[rs]=x[rs]*pw(ly[o],r-mid)%P;
ly[o]=1;
st[o]=0;
}
}
void build(int o,int l,int r){
if(l==r){
ly[o]=x[o]=a[l];
st[o]=s[o]=bit[a[l]];
return;
}
int mid=(l+r)/2;
build(ls,l,mid);build(rs,mid+1,r);
push_up(o);
}
array<ll,2> qy(int o,int l,int r,int L,int R){
int mid=(l+r)/2;
array<ll,2>ans={1,0};
if(L<=l&&r<=R){
return {x[o],s[o]};
}
push_down(o,l,r);
if(L<=mid)ans=ans+qy(ls,l,mid,L,R);
if(R>mid)ans=ans+qy(rs,mid+1,r,L,R);
return ans;
}
void ud(int o,int l,int r,int L,int R,ll X){
int mid=(l+r)/2;
//if(l!=r)push_down(o,l,r); //1.先要将上次的信息传下去不然就会清空了2.并且要特判是不是最后一层
if(L<=l&&r<=R){
ly[o]=X*ly[o]%P;x[o]=x[o]*pw(X,r-l+1)%P;
st[o]|=bit[X];s[o]|=bit[X];
return;
}
push_down(o,l,r); //只要不向下搜就不向下推
if(L<=mid)ud(ls,l,mid,L,R,X);
if(R>mid)ud(rs,mid+1,r,L,R,X);
push_up(o); //更新了才需要向上推
}
void init(){
for(int i=2;i<301;i++){
if(!vi[i]){
pr[++cnt]=i;
inv[cnt]=pw(i,P-2);
for(int j=i;j<301;j+=i){
vi[j]=1;bit[j]|=(1ll<<cnt);
}
}
}
}
int main(){
init();
cin>>n>>q;
for(i=1;i<=n;i++)scanf("%lld",&a[i]);
build(1,1,n);
while(q--){
scanf("%s",S);
if(S[0]=='M'){
scanf("%d%d%lld",&l,&r,&X);
ud(1,1,n,l,r,X);
}else{
scanf("%d%d",&l,&r);
ans=qy(1,1,n,l,r);
tp=ans[0]%P;
for(i=1;i<63;i++){
if((ans[1]>>i)&1)tp=tp*(pr[i]-1)%P*inv[i]%P;
}
printf("%lld\n",tp);
}
}
}
Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树的更多相关文章
- Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] + Fibonacci[i-l ...
- Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树
B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...
- Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...
- Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】
任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
随机推荐
- AtCoder Regular Contest 096 D - Static Sushi(线性dp)
Problem Statement "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only o ...
- iOS版本设置
Base SDK指的是当前编译所用的SDK 版本: iOS Deployment Target指的是,编译后的 app 可在 终端的哪个 版本上运行. 设置方法: 点击xcode工程左侧项目名称-&g ...
- 项目总结15:JavaScript模拟表单提交(实现window.location.href-POST提交数据效果)
JavaScript模拟表单提交(实现window.location.href-POST提交数据效果) 前沿 1-在具体项目开发中,用window.location.href方法下载文件,因windo ...
- 【Linux 线程】常用线程函数复习《二》
1.函数pthread_join /************************************************************************* > Fil ...
- cpio解压initramfs.img
一.解压initramfs.img # mkdir test # cp /boot/initramfs.img /test # cd test # file initramfs.img initram ...
- rsync同步工具的配置与使用
一.什么是rsync?rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具. rsync官网 http://rsync.samba.org/ 二.rsync的工 ...
- echarts柱状图图例不显示的问题
如果想要图例有效果,legend中数据要和series中name的值保持一致,切记切记,这是我曾经遇到的坑,不保持一致是没有效果的
- JFinal Web开发学习(五)注册界面和后端验证
效果: 直接点击注册后 : 后端验证是可靠地,前端js验证是不可靠的.只需要在浏览器删除js验证代码即可突破js验证. 1.注册界面 在WebRoot下新建regist.jsp <%@ page ...
- 以太坊测试网络搭建以及RPC服务开启-配置注意事项
.Eth QA Test环境: 数据文件路径: C:\Users\Administrator\AppData\Roaming\Ethereum console启动参数: --rpc --testnet ...
- vue-awesome-swiper轮播的使用
一.安装vue-awesome-swiper npm install vue-awesome-swiper --save 二.引入插件 main.js里面分别引入(记得有些电脑要引入样式) impor ...