LG #214472

· · 题解

T1

P1226

题意

给你三个整数 a,b,p,求 a^b \bmod p。

输出一行一个字符串 a^b mod p=s,其中 a,b,p 分别为题目给定的值, s 为运算结果。

做法

直接使用快速幂板子即可。

快速幂:

比如要求 2^5,正常做法为连续乘,时间复杂度 \mathcal{O}(n),对于大数明显无法接受。

可以将它进行分解:

2^5=2^{2^2}\times2^1

时间复杂度为 \mathcal{O}(\log n),可以接受。

Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 7;

//int f[N][N], logn[N];
int n, m, k;

ll qp(ll a, ll b, ll mod) { // 快速幂
    ll ans = 1;
    while(b) {
        if(b & 1) ans = (ans * a) % mod;
        b >>= 1;
        a = (a * a) % mod;
    }
    return ans;
}

int main() {
    cin >> n >> m >> k;
//  for (int i = 1;i <= n;i++) cin >> f[i][0];
//  logn[1] = 0, logn[2] = 1;
//  for (int i = 3;i < N;i++) logn[i] = logn[i / 2] + 1;
    cout << n << "^" << m << " mod " << k << "=" << qp(n, m, k) << endl;
    return 0;
}

T2

P1440

题意

一个含有 n 项的数列,求出每一项前的 m 个数到它这个区间内的最小值。若前面的数不足 m 项则从第 1 个数开始,若前面没有数则输出 0。

做法分析

将有的元素全部丢进一个队列,每次将后面比当前元素小的数扔掉,并将前面比 i - m 小的元素扔掉。

Code

#include <bits/stdc++.h>
using namespace std;

const int N = 3e6 + 2, Logn = 22;

struct s{
    int p, a;
}a[N];

deque <s> q;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(), cout.tie();
    int n, m;
    cin >> n >> m;
    for (int i = 1;i <= n;i++) cin >> a[i].a, a[i].p = i - 1;
    cout << 0 << endl;
    for (int i = 1;i < n;i++) {
        while (!q.empty() && q.back().a >= a[i].a) q.pop_back();
        q.push_back(a[i]);
        while (!q.empty() && q.front().p < i - m) q.pop_front();
        cout << q.front().a << endl;
    }
    return 0;
}

T3

P3865

题意

查询指定区间最大值。

做法分析

很明显,正常查询会爆炸。

考虑采用倍增思想,ST 表。

我们把查询区间分为两部分。

预处理 \mathcal{O}(n\log n),查询 \mathcal{O}(1)。

坑点:必开快读,否则卡常。

Code

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 2, Logn = 22;

int f[N][Logn], logn[N], n, m;

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(), cout.tie();
    n = read(), m = read();
    for (int i = 1;i <= n;i++) f[i][0] = read();
    logn[1] = 0, logn[2] = 1;
    for (int i = 3;i < N;i++) logn[i] = logn[i / 2] + 1;
    for (int i = 1;i <= Logn;i++) for (int j = 1;j + (1 << i) - 1 <= n;j++) f[j][i] = max(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
    for (int i = 1;i <= m;i++) {
        int x, y;
        x = read(), y = read();
        int mid = logn[y - x + 1];
        cout << max(f[x][mid], f[y - (1 << mid) + 1][mid]) << '\n';
    }
    return 0;
}

T4

P2880

题意

Farmer John 他想知道每一组里面最高和最低的牛的身高差。

每次输入一对 l,r,求出结果。

做法分析

类似地,添加一个数组,用于计算区间最小值,最后用最大值减最小值即可。

Code

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 2, Logn = 22;

int a[N][Logn], b[N][Logn], logn[N], n, m;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(), cout.tie();
    cin >> n >> m;
    for (int i = 1;i <= n;i++) cin >> a[i][0], b[i][0] = a[i][0];
    logn[1] = 0, logn[2] = 1;
    for (int i = 3;i < N;i++) logn[i] = logn[i / 2] + 1;
    for (int i = 1;i <= Logn;i++)
        for (int j = 1;j + (1 << i) - 1 <= n;j++)
            a[j][i] = max(a[j][i - 1], a[j + (1 << (i - 1))][i - 1]), 
            b[j][i] = min(b[j][i - 1], b[j + (1 << (i - 1))][i - 1]);

    for (int i = 1;i <= m;i++) {
        int x, y;
        cin >> x >> y;
        int mid = logn[y - x + 1];
        cout << max(a[x][mid], a[y - (1 << mid) + 1][mid]) - min(b[x][mid], b[y - (1 << mid) + 1][mid]) << '\n';
    }
    return 0;
}

T5

P3390

题意

计算数组 a 的 k 次方。

做法分析

明显无法使用直接乘法。

所以考虑重载运算符,再矩阵快速幂。

Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e3 + 2;
const ll mod = 1e9 + 7;

ll n, m;

struct Matrix {
    ll a[N][N];
    Matrix() { memset(a, 0, sizeof(a)); }
    void build() {
        for (int i = 1; i <= n; i++) a[i][i] = 1;
    }
};

Matrix operator * (const Matrix &x, const Matrix &y) {
    Matrix z;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                z.a[i][j] = (z.a[i][j] + x.a[i][k] * y.a[k][j] % mod) % mod;
            }
        }
    }
    return z;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(), cout.tie();

    cin >> n >> m;
    Matrix a, ans;
    ans.build();

    // 输入矩阵a
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a.a[i][j];
        }
    }

    // 快速幂计算
    while (m) {
        if (m % 2 == 1) ans = ans * a;
        a = a * a;
        m >>= 1;
    }

    // 输出结果
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << ans.a[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}