HDU - 5152(线段树+欧拉降幂)A Strange Problem

题目链接: hdu-5152.

题目描述

长度为 n n n 的序列 ( a 1 , a 2 , . . . , a n ) (a_{1},a_{2},...,a_{n}) (a1,a2,...,an),有 m m m 个操作。操作分为 3 3 3 种,

  1. 求区间和并输出,结果对 2333333 2333333 2333333 取模;
  2. a p → 2 a p a_{p} \rightarrow 2^{a_{p}} ap2ap
  3. 区间加 ( l , r , x ) (l,r,x) (l,r,x)

1 ≤ n , m ≤ 50000 1\leq n,m\leq 50000 1n,m50000, 0 ≤ a i , x ≤ 1 0 9 0\leq a_{i},x \leq 10^{9} 0ai,x109.

思路

比较难的就是操作2.
首先要知道,欧拉降幂公式:
A x &MediumSpace; % &MediumSpace; P = { A x &MediumSpace; % &MediumSpace; p h i ( P ) + p h i ( P ) &MediumSpace; % &MediumSpace; P ,  if  x ≥ p h i ( P ) . A x &MediumSpace; % &MediumSpace; P ,  if  x &lt; p h i ( P ) . A^{x}\: \% \: P = \begin{cases} &amp; A^{x\: \% \: phi(P) + phi(P)}\: \% \:P, \text{ if } x\geq phi(P) .\\ &amp; A^{x}\: \% \:P, \text{ if } x&lt;phi(P) . \end{cases} Ax%P={Ax%phi(P)+phi(P)%P, if xphi(P).Ax%P, if x<phi(P).
p h i ( P ) phi(P) phi(P)表示 P P P 的欧拉函数
&ThickSpace;&ThickSpace;&ThickSpace;&ThickSpace;&ThickSpace; \;\;\;\;\; 但是在这个题里面要逆着求,因为现在的值下一次会成为指数,一级一级,越靠前的数越需要经历多次欧拉降幂。并不能求出当前的结果,后面直接使用,因为对于同一个数来说,每次级数可能不同,模数就是不同的。比如说对于某个数 A x A_{x} Ax,第一次 A x % P A_{x}\%P Ax%P ,进行一次操作2之后是 2 A x % p h i ( P ) + p h i ( P ) % P 2^{A_{x}\% phi(P) + phi(P) } \%P 2Ax%phi(P)+phi(P)%P ,再进行一次操作2之后就是 2 2 A x % p h i ( p h i ( P ) ) + p h i ( p h i ( P ) ) % p h i ( P ) + p h i ( P ) % P 2^{2^{A_{x}\%phi(phi(P))+phi(phi(P))}\%phi(P) + phi(P) } \%P 22Ax%phi(phi(P))+phi(phi(P))%phi(P)+phi(P)%P 。之前的结果后面并不能用到。(这怎么办呢?凉了呀!别担心,只要思想不滑坡,办法总比困难多!
&ThickSpace;&ThickSpace;&ThickSpace;&ThickSpace;&ThickSpace; \;\;\;\;\; 经研(da)究(biao),可以发现,模数 2333333 2333333 2333333 ,求 p h i ( 2333333 ) phi(2333333) phi(2333333),求 p h i ( p h i ( 2333333 ) ) phi(phi(2333333)) phi(phi(2333333)),…,第18次时,欧拉函数即为1,意味着之后级数再高,到这里模数是1,结果就是0,所以,只要用数组记录操作2,每次需要时算一遍即可。
&ThickSpace;&ThickSpace;&ThickSpace;&ThickSpace;&ThickSpace; \;\;\;\;\; 但是,在成为指数的过程中,还在不断地加值,两种操作交替进行,所以用一个数组记录,当前的值什么时候进行操作2,什么时候进行了操作3,先后顺序也不能乱。如下公式:
2 2 A x % x + x % y + A d d 1 + A d d 2 + A d d 3 % P 2^{2^{A_{x}\%x+x}\%y +Add_{1}} +Add_{2}+Add_{3}\%P 22Ax%x+x%y+Add1+Add2+Add3%P .

代码


#include <bits/stdc++.h>

using namespace std;
#define LL long long
#define lson(u) (u<<1)
#define rson(u) (u<<1|1)
const int MAXN = 50005;

int a[MAXN], Mod[100];
vector<LL>v[MAXN];
LL lazy[MAXN<<2];
struct node {
    int l, r;
    LL sum;
}t[MAXN << 2];

int Phi(int n) { // 求欧拉函数 
    int x = n;
    for(int i = 2; i*i <= n; i++) {
        if(n % i == 0) {
            x = x - x/i;
            while(n % i == 0) n /= i;
        }
    }
    if(n > 1) x = x - x/n;
    return x;
}
void Init() {
    Mod[0] = 2333333;
    for(int i = 1; ; i++) {
        Mod[i] = Phi(Mod[i-1]);
        if(Mod[i] == 1) {
            break;
        }
    }
}
LL power(LL a, LL b, LL c) { // a ^ b % c 
    LL ans = 1;
    while(b) {
        if(b&1) ans = ans * a % c;
        a = a * a % c;
        b = b >> 1;
    }
    return ans;
}
void push_up(int u) {
    t[u].sum = t[lson(u)].sum + t[rson(u)].sum;
    t[u].sum %= Mod[0];
    return;
}
void push_down(int u){
    if(lazy[u]) {
        t[lson(u)].sum += 1ll * (t[lson(u)].r - t[lson(u)].l + 1) * lazy[u]; 
        t[rson(u)].sum += 1ll * (t[rson(u)].r - t[rson(u)].l + 1) * lazy[u];
        t[lson(u)].sum %= Mod[0];t[rson(u)].sum %= Mod[0];
        lazy[lson(u)] += lazy[u];
        lazy[rson(u)] += lazy[u];
        lazy[u] = 0;
    }
}
void build(int l, int r, int u) {
    t[u].l = l;
    t[u].r = r;
    lazy[u] = 0;
    if(l == r) {
        t[u].sum = a[l] % Mod[0];
        v[l].push_back(a[l]);
        return;
    }
    int mid = l + r >> 1;
    build(l, mid, lson(u));
    build(mid + 1, r, rson(u));
    push_up(u);
    return;
}
LL calc(int place, int now, int dep) { // *QQ* 递归比较好理解 
    if(dep >= 19) return 0;
    if(now == 0) return v[place][now];
    LL tmp = calc(place, now-1, dep+1);
    if(tmp < Mod[dep]) return (power(2ll, tmp, Mod[dep-1]) + v[place][now]) % Mod[dep-1];
    else return (power(2ll, tmp % Mod[dep] + Mod[dep], Mod[dep-1]) + v[place][now]) % Mod[dep-1];
}
void update_Point(int place, int u) { // 点更新 
    if(t[u].l == t[u].r) {
        int x = t[u].l;
        if(lazy[u]) {
            v[x][v[x].size()-1] += lazy[u]; // 操作 3 累加至操作 2 上 
            lazy[u] = 0;
        }
        v[x].push_back(0); // 操作 2 放 0 
        t[u].sum = calc(x,v[x].size()-1,1);
        return;
    }
    push_down(u);
    int mid = t[u].l + t[u].r >> 1;
    if(mid >= place) update_Point(place, lson(u));
    if(mid < place) update_Point(place, rson(u));
    push_up(u);
    return;
}
void update(int L, int R, int x, int u) { // 区间加 
    if(L <= t[u].l && R >= t[u].r) {
        lazy[u] += x;
        t[u].sum += 1ll * (t[u].r - t[u].l + 1) * x % Mod[0];
        t[u].sum %= Mod[0];
        return;
    }
    push_down(u);
    int mid = t[u].l + t[u].r >> 1;
    if(L <= mid) update(L, R, x, lson(u));
    if(R > mid) update(L, R, x, rson(u));
    push_up(u);
    return;
}
LL Query_sum(int L, int R, int u) {
    if(L <= t[u].l && R >= t[u].r) {
        return t[u].sum % Mod[0];
    }
    push_down(u);
    LL sum = 0;
    int mid = t[u].l + t[u].r >> 1;
    if(L <= mid) sum += Query_sum(L, R, lson(u));
    if(R > mid) sum += Query_sum(L, R, rson(u));
    return sum % Mod[0];
}
int main() {
    
    Init();
    int n, m, L, R, p, s, x;
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i = 1; i <= n; i++) v[i].clear();
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        build(1,n,1);
        for(int i = 1; i <= m; i++) {
            scanf("%d", &s);
            if(s == 1) { // 区间和 
                scanf("%d%d", &L, &R);
                printf("%lld\n",Query_sum(L, R, 1));
            }
            if(s == 2) { // ap -> 2 ^ ap 
                scanf("%d", &p);
                update_Point(p,1);
            }
            if(s == 3) {
                scanf("%d%d%d", &L, &R, &x);
                update(L, R, x, 1);
            }
        }
    }
    return 0; 
} 
/*
3 4
1 2 3
2 3
1 2 3
3 1 3 2
1 1 3

2 2
5 2
2 1
1 1 2 
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值