【BZOJ4869】相逢是问候(线段树,欧拉定理)

题面

BZOJ

题解

根据欧拉定理递归计算(类似上帝与集合的正确用法)

所以我们可以用线段树维护区间最少的被更新的多少次

如果超过了\(\varphi\)的限制

就不用再计算了

如果需要计算就每次暴力算

这样的复杂度\(O(nlog^2)\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define lson (now<<1)
#define rson (now<<1|1)
#define MAX 80000
#define ll long long
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int a[MAX],C,P,n,m;
ll phi[MAX],tot;
ll Phi(ll x)
{
ll ret=x;
for(ll i=2;i*i<=x;++i)
if(x%i==0)
{
ret=ret/i*(i-1);
while(x%i==0)x/=i;
}
if(x>1)ret=ret/x*(x-1);
return ret;
}
ll fpow(ll a,ll b,ll P)
{
long long s=1;
bool fl=false,f2=false;
while(b)
{
if(b&1)s=1ll*s*a,fl|=f2;
if(s>=P)fl=true,s%=P;
a=a*a;
if(a>=P)f2=true,a%=P;
b>>=1;
}
if(fl)s+=P;
return s;
}
struct Node{long long sum;int tt;}t[MAX<<2];
inline void Build(int now,int l,int r)
{
if(l==r){t[now].sum=a[l]=read();return;}
int mid=(l+r)>>1;
Build(lson,l,mid);Build(rson,mid+1,r);
t[now].sum=(t[lson].sum+t[rson].sum)%P;
}
ll Calc(int l,int r,ll x,ll P)
{
if(l==r)return fpow(x,1,P);
return fpow(C,Calc(l+1,r,x,phi[l+1]),P);
}
void Modify(int now,int l,int r,int L,int R)
{
if(t[now].tt>=tot)return;
if(l==r)
{
t[now].sum=Calc(0,++t[now].tt,a[l],P)%P;
//t[now].sum=fpow(C,a[l],P)%P;
//a[l]=fpow(C,a[l],phi[1]);
return;
}
int mid=(l+r)>>1;
if(L<=mid)Modify(lson,l,mid,L,R);
if(R>mid)Modify(rson,mid+1,r,L,R);
t[now].tt=min(t[lson].tt,t[rson].tt);
t[now].sum=(t[lson].sum+t[rson].sum)%P;
}
int Query(int now,int l,int r,int L,int R)
{
if(L<=l&&r<=R)return t[now].sum;
int mid=(l+r)>>1;ll ret=0;
if(L<=mid)ret=(ret+Query(lson,l,mid,L,R))%P;
if(R>mid)ret=(ret+Query(rson,mid+1,r,L,R))%P;
return ret;
}
int main()
{
n=read();m=read();P=read();C=read();
Build(1,1,n);
phi[0]=P;
for(tot=1;;++tot)
{
phi[tot]=Phi(phi[tot-1]);
if(phi[tot]==1)break;
}
phi[++tot]=1;
while(m--)
{
int opt=read(),l=read(),r=read();
if(opt)printf("%d\n",Query(1,1,n,l,r));
else Modify(1,1,n,l,r);
}
return 0;
}

【BZOJ4869】相逢是问候(线段树,欧拉定理)的更多相关文章

  1. 【BZOJ4869】相逢是问候 [线段树][欧拉定理]

    相逢是问候 Time Limit: 40 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Informatikverbin ...

  2. [BZOJ4869][六省联考2017]相逢是问候(线段树+扩展欧拉定理)

    4869: [Shoi2017]相逢是问候 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1313  Solved: 471[Submit][Stat ...

  3. 【bzoj4869】[Shoi2017]相逢是问候 线段树+扩展欧拉定理

    Description Informatikverbindetdichundmich. 信息将你我连结.B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以 分为两 ...

  4. BZOJ4869 [Shoi2017]相逢是问候 【扩展欧拉定理 + 线段树】

    题目链接 BZOJ4869 题解 这题调得我怀疑人生,,结果就是因为某些地方\(sb\)地忘了取模 前置题目:BZOJ3884 扩展欧拉定理: \[c^a \equiv c^{a \mod \varp ...

  5. SHOI 2017 相逢是问候(扩展欧拉定理+线段树)

    题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...

  6. 洛谷P3747 [六省联考2017]相逢是问候

    传送门 题解 扩展欧拉定理. 线段树维护,已经全改到底了的节点就不管,不然暴力修改下去. //Achen #include<algorithm> #include<iostream& ...

  7. bzoj 4869: [Shoi2017]相逢是问候 [扩展欧拉定理 线段树]

    4869: [Shoi2017]相逢是问候 题意:一个序列,支持区间\(a_i \leftarrow c^{a_i}\),区间求和.在模p意义下. 类似于开根操作,每次取phi在log次后就不变了. ...

  8. BZOJ4869 六省联考2017相逢是问候(线段树+欧拉函数)

    由扩展欧拉定理,a^(a^(a^(……^x)))%p中x作为指数的模数应该是φ(φ(φ(φ(……p)))),而p取log次φ就会变为1,也即每个位置一旦被修改一定次数后就会变为定值.线段树维护区间剩余 ...

  9. bzoj4869: [Shoi2017]相逢是问候(欧拉函数+线段树)

    这题是六省联考的...据说数据还出了点锅,心疼六省选手QAQ 首先要知道扩展欧拉定理... 可以发现每次区间操作都会使模数进行一次phi操作,而一个数最多取logp次phi就会变成1,这时后面的指数就 ...

随机推荐

  1. Windows Server 2012开启多人远程

    首先在Server Roles中选择Remote Desktop Services,然后在Role Services中安装Remote Desktop Session Host 安装完成后需要重启机器 ...

  2. Netty ByteBuf梳理

    我们知道,网络数据的基本单位总是字节.Java NIO提供了ByteBuffer作为它的字节容器,但是这个类使用起来过于复杂,而且也有些繁琐. Netty的ByteBuffer替代品是ByteBuf, ...

  3. python[error] - mysql_config not found

    具体报错信息: root@pts/4 $ pip install MySQL-python Collecting MySQL-python Using cached MySQL-python-1.2. ...

  4. php实现的短网址算法分享

    这篇文章主要介绍了php实现的短网址算法,理论上支持1,073,741,824个短网址,个人使用足够了,需要的朋友可以参考下 每个网址用6个字符代替,(6^32) 最多可以拥有1,073,741,82 ...

  5. map,vector 等容器内容的循环删除问题(C++)

    map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...

  6. vs code 使用git

    1.下载git https://git-scm.com/ 2. git 全局设置 git config --global user.name "xxxx" git config - ...

  7. React设计思想

    熟悉一个新技术的关键是熟悉他的特色和理念 React框架本身和我们常用的JavaScript MVC框架,如:AngularJS,Backbone,Ember等,没有直接的可比性.在React的官方博 ...

  8. java定时器schedule和scheduleAtFixedRate区别

    package cn.lonecloud.test; import java.util.Date; import java.util.Timer; import java.util.TimerTask ...

  9. vue——安装并新建项目

    一.对于vue的安装: 1.安装vue之前先安装node,https://nodejs.org/zh-cn/download/,我装的是windows64位的: 2.下载好了之后就可以按照正常顺序安装 ...

  10. Flask從入門到入土(一)——程序的基本結構

    一.初始化 所有Flask程序都必須創建一個程序實例.Web服務器使用一種名爲Web服務器網關接口的協議,把接收自客戶端的所有請求都轉交給這個對象處理.程序實例書Flask類的對象,創建代碼: fro ...