[USACO16JAN]子共七Subsequences Summing to Sevens
[USACO16JAN]子共七Subsequences Summing to Sevens
a[i]表示前缀和
如果a[i]%7==t&&a[j]%7==t
那么a[j]-a[i-1]一定是7的整数倍,
这样就o(n)扫一遍,不断更新答案就可以了。
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<cstring> #define inf 2147483647 #define For(i,a,b) for(register long long i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() //by war //2017.10.12 using namespace std; long long n; long long a[50010]; long long p[50010]; long long x,l,ans; void in(long long &x) { char c=g();x=0; while(c<'0'||c>'9')c=g(); while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); } void o(long long x) { if(x>9)o(x/10); p(x%10+'0'); } int main() { in(n); For(i,1,n) { in(x); a[i]=x+a[i-1]; } /* for(register long long len=n;len>=1;len--) { For(i,1,n-len+1) if((a[i+len-1]-a[i-1])%7==0) { o(len); exit(0); } }*/ For(i,0,6) p[i]=inf; For(i,1,n) { p[a[i]%7]=min(p[a[i]%7],i); ans=max(ans,i-p[a[i]%7]); } o(ans); return 0; }