比赛 NOIP模拟赛by mzx Day2 评测结果 AAAATTTTTT
题目名称 学姐的巧克力盒 最终得分 40
用户昵称 KZNS 运行时间 12.822 s
代码语言 C++ 内存使用 52.56 MiB
提交时间 2016-10-20 20:39:02
显示代码纯文本
//KZNS
#include <cstdio>
#include <utility>
using namespace std;
#define SIZE 1000003
int N, M, K, P1, P2, phP2;
int A[SIZE];
void phphph() {
    phP2 = P2;
    int up = P2;
    for (int i = 2; i * i <= up; i++) {
        if (!(up % i)) {
            phP2 /= i;
            phP2 *= i-1;
            while (!(up % i))
                up /= i;
        }
    }
    if (up > 1) {
        phP2 /= up;
        phP2 *= up-1;
    }
}
void init() {
    scanf("%d %d %d %d %d", &N, &M, &K, &P1, &P2);
    for (int i = 1; i <= N; i++)
        scanf("%d", A+i);
    if (P2) {
        K %= P2;
        phphph();
    }
}
typedef pair<int, int> pr;
typedef long long LL;
class poi {
public:
    int l, r;
    pr s;
} tr[SIZE * 4];
void make_tree(int x, int l, int r) {
    poi &u = tr[x];
    u.l = l;
    u.r = r;
    if (l == r) {
        if (P1)
            u.s.first = A[l] % P1;
        if (P2)
            u.s.second = A[l] % phP2;
    }
    else {
        make_tree(x<<1, l, l+r>>1);
        make_tree(x<<1^1, (l+r>>1)+1, r);
        if (P1)
            u.s.first = (LL)(tr[x<<1].s.first) * tr[x<<1^1].s.first % P1;
        if (P2)
            u.s.second = (LL)(tr[x<<1].s.second) * tr[x<<1^1].s.second % phP2;
    }
}
pr Qint(int x, int l, int r) {
    if (l <= tr[x].l && tr[x].r <= r)
        return tr[x].s;
    else {
        pr ans = make_pair(1, 1);
        pr u;
        if (l <= tr[x<<1].r) {
            u = Qint(x<<1, l, r);
            if (P1)
                ans.first = (LL)(ans.first) * u.first % P1;
            if (P2)
                ans.second = (LL)(ans.second) * u.second % phP2;
        }
        if (tr[x<<1^1].l <= r) {
            u = Qint(x<<1^1, l, r);
            if (P1)
                ans.first = (LL)(ans.first) * u.first % P1;
            if (P2)
                ans.second = (LL)(ans.second) * u.second % phP2;
        }
        return ans;
    }
}
int fsm(int b) {
    LL ans = 1;
    LL a = K;
    while (b) {
        if (b & 1)
            ans = ans * a % P2;
        a = a * a % P2;
        b >>= 1;
    }
    return ans;
}
void work() {
    int tp, l, r;
    pr u;
    for (int i = 0; i < M; i++) {
        scanf("%d %d %d", &tp, &l, &r);
        if (tp == 1) {
            u = Qint(1, l, r);
            printf("%d\n", u.first);
        }
        else {
            u = Qint(1, l, r);
            printf("%d\n", fsm(u.second));
        }
    }
}
int main() {
    freopen("chocolatebox.in", "r", stdin);
    freopen("chocolatebox.out", "w", stdout);
    init();
    make_tree(1, 1, N);
    work();
    return 0;
}
//UBWH