T633406 『欢乐暑假』序列变换

· · 题解

说在前面

真的有橙吗???为啥我感觉是红??

前置芝士:前缀和、差分、字符串。

题目分析

注意到前缀和是差分的逆运算。所以它们两个可以相互抵消。最终的结果只和 m 有关。若 m 是偶数,那么输出所有数字的和;若 m 是奇数,那么输出最后一个数字。而判断一个数字的奇偶只需要看他的最后一位。于是就做完了!!!!有史以来出过的最垃圾的题!!!!

时间复杂度 O(n + m 的位数)。

完整代码

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1000000 + 5;

int a[N];

int n, x;

string m;

signed main() {
    // freopen("test10.in", "r", stdin);
    // freopen("test10.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        x += a[i];
    }
    int tmp = m[(int)m.length() - 1] - '0';
    if (tmp & 1) {
        cout << a[n];
    } else {
        cout << x;
    }
    return 0;
}

说在后面

没做对的敲个 6 我看看。

若代码、思路讲解有误,欢迎提出!