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 ...
随机推荐
- fnb2b分支拉取注意事项
1. 大B分支拉取以后不要忘记把index.php中dev环境改为 $save_url = "http://dev-b2b.dev1.fn/"; 2. 大B分支拉取后,记得/bas ...
- shelve
shelve是对pickle的封装 json & pickle是把所有的数据全部封装,一次性写入文件,而shelve可以把数据分类,以键值对的形式分别写入文件 shelve模块是一个简单的k, ...
- 31-java中知识总结:list, set, map, stack, queue
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Lin ...
- 34-ssm 最简洁的模板
见 csdn :https://download.csdn.net/download/qq_39451578/10931448
- 让listView gridView全部扩展开
public class NoScrollListView extends ListView { public NoScrollListView(Context context, Attribu ...
- java String 中替换"\"为"\\"
表示路径的字符串 c:\work\test\afd.out, 要形成 c:\\work\\test\\afd.out 用 String str = strBsf.replaceAll(" ...
- iOS 集成极光推送
最近极光推送更新到V3版本之后,推送又不成功!配合服务器联调了半天,发现是服务器环境配置有问题. 想着就把极光推送的步骤给记录下来. 一.配置push证书 这个可以到极光文档里写,很详细 二.导入必要 ...
- pthreads v3下的Volatile介绍与使用
由于pthreads v3中引入了Threaded对象自动不变性的概念,所以当我们在构造函数中给成员设置为数组时,在其他地方就无法对成员再次改写了. 例子如下: <?php //pthreads ...
- 将文件转换成byte[]数组
代码 /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl&quo ...
- js DomContentLoaded 和 load 的区别
如题:DOMContentLoaded和load都是页面加载的时候触发的事件.区别在于触发的时机不一样. 浏览器渲染页面DOM文档加载的步骤: 1.解析HTML结构. 2.加载外部脚本和css文件. ...