fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25.  
  26. //? Good Subarrays Hashing
  27.  
  28. //* We need to find subarrays : (P[R] - P[L-1])%k == 0
  29. //* P[R]%k == P[L-1]%k
  30. //* Hence for P[R]%k find P[R]%k in past
  31.  
  32.  
  33. //* Eg : [4, 5, 0, -2, -3, 1] , k = 5
  34. //* sum : 0 4 9 9 7 4 5
  35. //* sum%m : 0 4 4 4 2 4 0
  36. //* ct : 1 1 2 3 1 4 2
  37. //* ans : 1 + 2 + 0 + 3 + 1
  38.  
  39.  
  40. vector<int> a;
  41. void consistency(int n, int k) {
  42.  
  43. unordered_map<int,int> mp = {{0, 1}};
  44. int sum = 0;
  45. int ans = 0;
  46. for(int i=0; i<n; i++){
  47. sum += a[i];
  48. int rem = ((sum%k)+k)%k;
  49. if(mp.count(rem)){
  50. ans += mp[rem];
  51. }
  52. mp[rem]++;
  53. }
  54.  
  55. cout << ans << endl;
  56.  
  57. }
  58.  
  59. void solve() {
  60.  
  61. int n, k;
  62. cin >> n >> k;
  63. a.resize(n);
  64. for(int i=0; i<n; i++) cin >> a[i];
  65. consistency(n, k) ;
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. int32_t main() {
  74. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  75.  
  76. int t = 1;
  77. while (t--) {
  78. solve();
  79. }
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 5276KB
stdin
5 2
1 5 3 4 2
stdout
7