hdu4648Magic Pen 6
http://acm.hdu.edu.cn/showproblem.php?pid=4648
求连续的一段和对m取余为0 若s[j]和s[i]对M的余数都相同 则相见就满足要求 找个最长的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 using namespace std; 7 #define LL long long 8 #define N 100010 9 #define INF 0xfffffff 10 LL s[N]; 11 int a[N],d1[N],d2[N]; 12 int main() 13 { 14 int i,j,n,m,k; 15 while(cin>>n>>m) 16 { 17 memset(s,0,sizeof(s)); 18 LL sum=0; 19 for(i = 1; i <= m ; i++) 20 { 21 d1[i] = -INF; 22 d2[i] = INF; 23 } 24 int ans=0; 25 for(i = 1; i <= n ; i++) 26 { 27 scanf("%d",&a[i]); 28 sum+=a[i]; 29 s[i] = s[i-1]+a[i]; 30 k = s[i]%m; 31 if(k<0) k+=m; 32 if(k==0) 33 ans = max(ans,i); 34 d1[k] = max(d1[k],i); 35 d2[k] = min(d2[k],i); 36 } 37 for(i = 1; i < m ; i++) 38 ans = max(d1[i]-d2[i],ans); 39 cout<<ans<<endl; 40 } 41 return 0; 42 }