1. 程式人生 > >Bzoj4869: [Shoi2017]相逢是問候

Bzoj4869: [Shoi2017]相逢是問候

end build const space pow(x long long 一段 ont esp

題面

傳送門

Sol

擺定理
\[ a^b\equiv \begin{cases} a^{b\%\phi(p)}~~~~~~~~~~~gcd(a,p)=1\a^b~~~~~~~~~~~~~~~~~~gcd(a,p)\neq1,b<\phi(p)\a^{b\%\phi(p)+\phi(p)}~~~~gcd(a,p)\neq1,b\geq\phi(p) \end{cases}~~~~~~~(mod~p) \]
處理出\(p\)每次取\(\varphi\)取到\(1\)為止的\(\varphi\)值(註意還要取個1,可能存在其它的\(p\)\(\varphi\)為1),最多\(log\)


每次暴力修改,如果這個點被修改了超過了\(p取\varphi\)的次數它就不會變了,那就不改了
每個數只會最多改\(log\)次,所以復雜度對了
直接搞luogu和loj上TLE了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(5e4 + 5), __(1e4 + 1);

IL ll Read(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, c, prime[__], num, phi[30], lenp;
bool isprime[__];

IL void Sieve(){
    isprime[1] = 1;
    for(RG int i = 2; i < __; ++i){
        if(!isprime[i]) prime[++num] = i;
        for(RG int j = 1; j <= num && prime[j] * i < __; ++j){
            isprime[i * prime[j]] = 1;
            if(!(i % prime[j])) break;
        }
    }
}

IL int Phi(RG int x){
    RG int cnt = x;
    for(RG int i = 1; i <= num && prime[i] * prime[i] <= x; ++i){
        if(x % prime[i]) continue;
        while(!(x % prime[i])) x /= prime[i];
        cnt -= cnt / prime[i];
    }
    if(x > 1) cnt -= cnt / x;
    return cnt;
}

IL ll Pow(RG ll x, RG ll y, RG ll p){
    RG int flg2 = 0, flg1 = 0; RG ll cnt = 1;
    for(; y; y >>= 1){
        if(y & 1) flg1 |= (cnt * x >= p || flg2), cnt = cnt * x % p;
        flg2 |= (x * x >= p); x = x * x % p;
    }
    return cnt + flg1 * p;
}

IL ll Calc(RG int l, RG int r, RG ll x, RG ll p){
    if(l == r) return Pow(x, 1, p);
    return Pow(c, Calc(l + 1, r, x, phi[l + 1]), p);
}

int tim[_ << 2], sum[_ << 2], a[_];

IL void Build(RG int x, RG int l, RG int r){
    if(l == r){  sum[x] = a[l] = Read(); return;  }
    RG int mid = (l + r) >> 1;
    Build(x << 1, l, mid); Build(x << 1 | 1, mid + 1, r);
    sum[x] = (sum[x << 1] + sum[x << 1 | 1]) % phi[0];
}

IL void Modify(RG int x, RG int l, RG int r, RG int L, RG int R){
    if(tim[x] >= lenp) return;
    if(l == r){  ++tim[x]; sum[x] = Calc(0, tim[x], a[l], phi[0]) % phi[0]; return;  }
    RG int mid = (l + r) >> 1;
    if(L <= mid) Modify(x << 1, l, mid, L, R);
    if(R > mid) Modify(x << 1 | 1, mid + 1, r, L, R);
    sum[x] = (sum[x << 1] + sum[x << 1 | 1]) % phi[0];
    tim[x] = min(tim[x << 1], tim[x << 1 | 1]);
}

IL int Query(RG int x, RG int l, RG int r, RG int L, RG int R){
    if(L <= l && R >= r) return sum[x];
    RG int mid = (l + r) >> 1, ans = 0;
    if(L <= mid) ans = Query(x << 1, l, mid, L, R);
    if(R > mid) ans = (ans + Query(x << 1 | 1, mid + 1, r, L, R)) % phi[0];
    return ans;
}

int main(RG int argc, RG char* argv[]){
    Sieve(); n = Read(); m = Read(); RG int p = Read(); c = Read();
    phi[0] = p; while(p != 1) p = phi[++lenp] = Phi(p); phi[++lenp] = 1;
    Build(1, 1, n);
    for(RG int i = 1; i <= m; ++i){
        RG int op = Read(), l = Read(), r = Read();
        if(!op) Modify(1, 1, n, l, r);
        else printf("%d\n", Query(1, 1, n, l, r));
    }
    return 0;
}

也可以直接強行去掉快速冪的\(log\)
處理出兩段\(c的冪,記為pow\),一段處理\(10000\)以內的,另一段處理以外的,查詢時兩端拼起來就好了

int Query(int x,int a,int mm)
{
    if (a <= 10000) return pow1[a][mm];
    return (1ll*pow2[a/10000][mm]*pow1[a%10000][mm])%phi[mm];
}

Bzoj4869: [Shoi2017]相逢是問候