fork download
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <numeric>
  7. #include <map>
  8. #include <unordered_map>
  9. #include <cmath>
  10. using namespace std;
  11. #define all(a) a.begin(), a.end()
  12. #define ll long long
  13. #define fo(i,n) for (long long i = 0; i < n; i++)
  14. bool comp(pair<char,ll> a, pair<char,ll> b)
  15. {
  16. return (a.first < b.first);
  17. }
  18. bool comp2(pair<char,ll> a, pair<char,ll> b)
  19. {
  20. return (a.first > b.first);
  21. }
  22. ll power(ll x, ll y)
  23. {
  24. ll res = 1; // Initialize result
  25.  
  26. while (y > 0) {
  27. // If y is odd, multiply x with result
  28. if (y & 1)
  29. res = res * x;
  30.  
  31. // y must be even now
  32. y = y >> 1; // y = y/2
  33. x = x * x; // Change x to x^2
  34. }
  35. return res;
  36. }
  37. int main()
  38. {
  39. ios_base::sync_with_stdio(false);
  40. cin.tie(0); cout.tie(0);
  41. ll n,m,i;
  42. ll a,b;
  43. string input;
  44. cin >> i;
  45. while (i--)
  46. {
  47. cin >> n;
  48. vector<ll> nums(n);
  49. fo(j,n)
  50. {
  51. cin >> nums[j];
  52. }
  53. ll count2 = 0;
  54. ll sum = 0;
  55. for (int j = 0; j < n; j++)
  56. {
  57. if (nums[j] == 1)
  58. {
  59. count2 = 0;
  60. for (int k = j+1; k < n; k++)
  61. {
  62. if (nums[k] == 2)
  63. {
  64. count2++;
  65. }
  66. if (nums[k] == 3)
  67. {
  68. sum += power(2,count2)-1%998244353;
  69. }
  70. }
  71. }
  72. }
  73. cout << sum << '\n';
  74. }
  75. }
Success #stdin #stdout 0.01s 5292KB
stdin
4
7
3 2 1 2 2 1 3
4
3 1 2 2
3
1 2 3
9
1 2 3 2 1 3 2 2 3
stdout
3
0
1
22