1 条题解

  • 1
    @ 2024-10-11 12:01:44

    思路

    暴力做法:暴力枚举左端点 ll 和右端点 rr,求 maxrl+1\operatorname{max} r-l+1

    满分做法:区间和被 77 整除当且仅当 prermod7=prel1mod7pre_r \bmod 7 = pre_{l-1} \bmod 7

    从左到右枚举,将前缀和对 77 取摸,存储每个模数较前位置的下标作为左端点 ll 即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7;
    const int N = 5e5 + 10;
    ll a[N], pre[N];
    int pos[N], rev[N];
    int n, ans;
    
    int main() {
        ios::sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            pre[i] = (pre[i - 1] + a[i]) % 7;
        }
        for (int i = n; i >= 1; i--) {
            pos[pre[i]] = i;
        }
        pos[0] = 0;
        for (int i = 1; i <= n; i++) {
            rev[pre[i]] = i;
        }
        for (int i = 0; i <= 6; i++) {
            ans = max(ans, rev[i] - pos[i]);
        }
        cout << ans;
        return 0;
    }
    
    • 1

    信息

    ID
    18
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    189
    已通过
    34
    上传者