fork download
  1. #include <bits/stdc++.h>
  2. #define boostcode ios_base::sync_with_stdio(0); cin.tie(0);
  3. #define openf freopen("CHIA3.inp", "r", stdin); freopen("CHIA3.out", "w", stdout);
  4.  
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. int n; // Lưu n từ input
  10. string ai; // Lưu các số từ dãy A từ input
  11. int du[3]; // Lưu trữ số lượng giá trị dư của các số từ dãy A khi chia lấy dư cho 3
  12. int temp; // Lưu trữ tổng các chữ số của ai
  13. ll res; // Kết quả bài toán cần tìm
  14.  
  15. int main() {
  16. boostcode;
  17. openf;
  18.  
  19. cin >> n; // Nhập input n
  20. // Nhập input các phần tử của dãy A và xử lí kết quả đông thời:
  21. for (int i = 0; i < n; i++) {
  22. cin >> ai;
  23. for (char c:ai) temp += (c-'0');
  24. res += du[(3-(temp%3))%3];
  25. du[temp%3]++;
  26. temp = 0;
  27. }
  28. cout << res; // In output
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty