2 solutions

  • 4
    @ 2025-4-8 20:26:10
    #include<bits/stdc++.h>
    using namespace std;
    int n,a[500005],cnt[500005],maxn;
    int main() {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)cin>>a[i];
        for(int i=1;i<=n;i++)cnt[i]=(cnt[i-1]+a[i-1])%7;
        unordered_map<int,int>first;
        for(int i=0;i<=n;i++){
            if(first.find(cnt[i])==first.end())first[cnt[i]]=i;
    		else maxn=max(maxn,i-first[cnt[i]]);
        }
        cout<<maxn;
        return 0;
    }
    
    • @ 2025-4-8 22:13:37

      猴哥打表水平又进步了👍还知道对于本题unordered_map比map好。后排推荐本题直接数组打表

  • 2
    @ 2025-4-8 20:14:10

    ansi,jans_{i,j} 表示以第 ii 个数字结尾并且mod7\mod 7 等于 jj 的最大的区间长度就可以了。

    #include<bits/stdc++.h>
    using namespace std;
    
    int n, a, ans[50005][10], maxn;
    
    main()
    {
        cin >> n >> a;
        ans[0][a % 7] = 1;
        for (int i = 0; i < n - 1; ++i)
        {
            cin >> a;
            for (int j = 0; j < 7; ++j)
                if (ans[i][j])
                    ans[i + 1][ (j + a) % 7] = ans[i][j] + 1;
            ans[i + 1][a % 7] = max(ans[i + 1][a % 7], 1);
        }
        for(int i = 0; i < n; ++i) maxn = max(maxn, ans[i][0]);
        cout << maxn << endl;
    }
    
    • @ 2025-4-8 22:11:00

      DP解法很棒👍👍👍题解质量顶尖

  • 1

Information

ID
1157
Time
500ms
Memory
512MiB
Difficulty
9
Tags
# Submissions
88
Accepted
5
Uploaded By