fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e7+14;
  4. long long n, pre[maxn], k;
  5. int ans = -1;
  6. void solve(){
  7. cin >> n >> k;
  8. pre[0] = 0;
  9. unordered_map < long long , int > m;
  10. for (int i = 1; i <= n; i++){
  11. int x;
  12. cin >> x;
  13. pre[i] = (pre[i - 1] + x) % k;
  14. if (!m[pre[i]]) m[pre[i]] = i;
  15. }
  16. m[0] = 0;
  17. for (int i = 1; i <= n; i++) {
  18. if (pre[i] == 0) ans = max(ans, i);
  19. if (m[pre[i]]){
  20. ans = max(ans, i - m[pre[i]]);
  21. }
  22. }
  23. if (ans == 0 ){
  24. cout << -1 ;
  25. return ;
  26. }
  27.  
  28. cout << ans ;
  29. }
  30. int main(){
  31. ios_base::sync_with_stdio(0);
  32. cin.tie(0); cout.tie(0);
  33. if (fopen("input.txt","r")){
  34. freopen("input.txt","r",stdin);
  35. freopen("output.txt","w",stdout);
  36. }
  37. solve();
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
-1