[Luogu] 子共七

https://www.luogu.org/problemnew/show/P3131

A表示前缀和数组

A[r] - A[l - 1] = 0 (mod 7) 

  A[r] = A[l - 1] (mod 7)

#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 1e5 + 10;

#define yxy getchar()

int n, ans, x, A[N], first[7], last[7];

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


int main() {
    n = read();
    for(int i = 1; i <= n; i ++) x = read(), A[i] = (A[i - 1] + x) % 7;
    for(int i = 1; i <= n; i ++) last[A[i]] = i;
    for(int i = n; i >= 1; i --) first[A[i]] = i;
    for(int i = 0; i < 7; i ++) ans = max(ans, last[i] - first[i]);
    printf("%d", ans);
}

 

posted @ 2018-02-09 15:19  xayata  阅读(218)  评论(0编辑  收藏  举报