大意: 给定n元素序列, q个操作: (1)区间乘 (2)单点除(保证整除) (3)区间求和对m取模

要求回答所有操作(3)的结果

主要是除法难办, 假设单点除$x$, $x$中与$m$互素的素因子可以直接欧拉求逆, 其余因子维护一个向量即可.

这种沙茶题结果各种细节出错改了一个多小时......太菜了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e5+10; int n, m, q, sz, Phi;
vector<int> A; int phi(int x) {
int s = x, mx = sqrt(x+0.5);
REP(i,2,mx) if (x%i==0) {
s = s/i*(i-1);
A.pb(i);
while (x%i==0) x/=i;
}
if (x>1) A.pb(x),s=s/x*(x-1);
sz = A.size();
return s;
} ll qpow(ll a,ll n) {ll r=1%m;for (a%=m;n;a=a*a%m,n>>=1)if(n&1)r=r*a%m;return r;} int sum[N<<2], rtag[N<<2], tag[N<<2], res[N<<2];
int c[N<<2][10], tagc[N<<2][10];
int ans, ql, qr, qv[10]; void pd(int o) {
if (tag[o]!=1) {
tag[lc] = (ll)tag[lc]*tag[o]%m;
tag[rc] = (ll)tag[rc]*tag[o]%m;
sum[lc] = (ll)sum[lc]*tag[o]%m;
sum[rc] = (ll)sum[rc]*tag[o]%m;
tag[o] = 1;
}
if (rtag[o]!=1) {
res[lc] = (ll)res[lc]*rtag[o]%m;
res[rc] = (ll)res[rc]*rtag[o]%m;
rtag[lc] = (ll)rtag[lc]*rtag[o]%m;
rtag[rc] = (ll)rtag[rc]*rtag[o]%m;
rtag[o] = 1;
}
REP(i,0,sz-1) {
c[lc][i]+=tagc[o][i],c[rc][i]+=tagc[o][i];
tagc[lc][i]+=tagc[o][i],tagc[rc][i]+=tagc[o][i];
tagc[o][i]=0;
}
} void pu(int o) {
sum[o]=(sum[rc]+sum[lc])%m;
} void build(int o, int l, int r) {
sum[o]=res[o]=tag[o]=rtag[o]=1;
if (l==r) {
int t;
scanf("%d", &t);
sum[o] = t%m;
REP(i,0,sz-1) {
while (t%A[i]==0) t/=A[i],++c[o][i];
}
res[o] = t%m;
return;
}
build(ls),build(rs);
pu(o);
} void mul(int o, int l, int r, int x, int R) {
if (ql<=l&&r<=qr) {
sum[o] = (ll)sum[o]*x%m;
tag[o] = (ll)tag[o]*x%m;
res[o] = (ll)res[o]*R%m;
rtag[o] = (ll)rtag[o]*R%m;
REP(i,0,sz-1) c[o][i]+=qv[i],tagc[o][i]+=qv[i];
return;
}
pd(o);
if (mid>=ql) mul(ls,x,R);
if (mid<qr) mul(rs,x,R);
pu(o);
}
void div(int o, int l, int r, int x, int v) {
if (l==r) {
REP(i,0,sz-1) {
while (v%A[i]==0) v/=A[i],--c[o][i];
}
res[o] = (ll)res[o]*qpow(v,Phi-1)%m;
sum[o] = res[o];
REP(i,0,sz-1) sum[o]=(ll)sum[o]*qpow(A[i],c[o][i])%m;
return;
}
pd(o);
if (mid>=x) div(ls,x,v);
else div(rs,x,v);
pu(o);
}
void query(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return (ans+=sum[o])%=m,void();
pd(o);
if (mid>=ql) query(ls,ql,qr);
if (mid<qr) query(rs,ql,qr);
} int main() {
scanf("%d%d", &n, &m);
Phi = phi(m);
build(1,1,n);
scanf("%d", &q);
while (q--) {
int op, x, p;
scanf("%d", &op);
if (op==1) {
scanf("%d%d%d",&ql,&qr,&x);
int t = x;
REP(i,0,sz-1) {
qv[i] = 0;
while (t%A[i]==0) t/=A[i],++qv[i];
}
mul(1,1,n,x,t);
} else if (op==2) {
scanf("%d%d",&p,&x);
div(1,1,n,p,x);
} else {
scanf("%d%d",&ql,&qr);
ans = 0, query(1,1,n,ql,qr);
printf("%d\n", ans);
}
}
}

Sasha and a Very Easy Test CodeForces - 1109E (数学,线段树)的更多相关文章

  1. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  2. Codeforces 787D. Legacy 线段树建模+最短路

    D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  3. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  4. Sereja and Brackets CodeForces - 380C (线段树+分治思路)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  5. CodeForces 91B Queue (线段树,区间最值)

    http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...

  6. codeforces 650D. Zip-line 线段树

    题目链接 题目的意思很简单, 就是给你n个数, m个询问, 每次询问修改某一个位置的值, 然后问你修改完之后数列的lis是多少. 询问独立. 对于原数列, 我们将它离散化, 令dp1[i]为以i为结尾 ...

  7. Codeforces 343D WaterTree - 线段树, DFS序

    Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...

  8. codeforces 787D - Legacy 线段树优化建图,最短路

    题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...

  9. Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)

    题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...

随机推荐

  1. Azkaban学习笔记(一)

    1. 任务调度概述 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 各任务单元之间存在时间先后及前后依赖关系 现成的开源调 ...

  2. Linux 远程连接sftp与ftp

    linux sftp远程连接命令 sftp -oPort=60001 root@192.168.0.254 使用-o选项来指定端口号. -oPort=远程端口号 sftp> get /var/w ...

  3. php array 根据value获取key,in_array()判断是否在数组内实例

    php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...

  4. HTTP从入门到入土(5)——HTTP报文格式

    HTTP报文格式 HTTP报文分为请求报文和响应报文,只有发送了请求报文,才会有响应报文. 常见的报文格式如下所示:

  5. MySQL Crash Course #02# Chapter 3. 4 通配符. 分页

    索引 查看表.文档操作 检索必须知道的两件事 数据演示由谁负责 通配符.非必要不用 检索不同的行 限制结果集.分页查找 运用数据库.表全名 命令后加分号对于很多 DBMS 都不是必要的,但是加了也没有 ...

  6. Python入门之Python Colorama模块

    Python的Colorama模块,可以跨多终端,显示字体不同的颜色和背景,只需要导入colorama模块即可,不用再每次都像linux一样指定颜色: 官方参考:https://pypi.org/pr ...

  7. Python学习笔记之面向对象编程(三)Python类的魔术方法

    python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...

  8. Python面试题之装饰器漫谈

    讲 Python 装饰器前,我想先举个例子,虽有点污,但跟装饰器这个话题很贴切. 每个人都有的内裤主要功能是用来遮羞,但是到了冬天它没法为我们防风御寒,咋办?我们想到的一个办法就是把内裤改造一下,让它 ...

  9. bzoj1635 / P2879 [USACO07JAN]区间统计Tallest Cow

    P2879 [USACO07JAN]区间统计Tallest Cow 差分 对于每个限制$(l,r)$,我们建立一个差分数组$a[i]$ 使$a[l+1]--,a[r]++$,表示$(l,r)$区间内的 ...

  10. python监控端口脚本[jkport2.0.py]

    #!/usr/bin/env python #!coding=utf-8 import os import time import sys import smtplib from email.mime ...