码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj4869: [Shoi2017]相逢是问候

时间:2018-01-20 00:17:50      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:www   typedef   while   set   存在   har   display   build   cas   

题面

传送门

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]相逢是问候

标签:www   typedef   while   set   存在   har   display   build   cas   

原文地址:https://www.cnblogs.com/cjoieryl/p/8319117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!