prithivMLmods/Coder-Stat huggingface dataset:
Problem p02843 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int x;
  cin >> x;

  int wk1, wk2;
  wk1 = x / 100;
  wk2 = x % 100;

  bool flag = false;
  for (int i = 0; i <= wk1; i++) {
    for (int j = 0; j + i <= wk1; j++) {
      for (int k = 0; i + j + k <= wk1; k++) {
        for (int l = 0; i + j + k + l <= wk1; l++) {
          for (int m = 0; i + j + k + l + m <= wk1; m++) {
            if (i * 1 + j * 2 + k * 3 + l * 4 + m * 5 == wk2 &&
                i + j + k + l + m <= wk1) {
              flag = true;
              break;
            }
          }
        }
      }
    }
  }

  if (flag)
    cout << 1 << endl;
  else
    cout << 0 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  int x;
  cin >> x;

  int wk1, wk2;
  wk1 = x / 100;
  wk2 = x % 100;

  bool flag = false;
  for (int i = 0; i <= wk1; i++) {
    for (int j = 0; j + i <= wk1; j++) {
      for (int k = 0; i + j + k <= wk1; k++) {
        for (int l = 0; i + j + k + l <= wk1; l++) {
          int m = (wk2 - i * 1 - j * 2 - k * 3 - l * 4) / 5;
          if (i * 1 + j * 2 + k * 3 + l * 4 + m * 5 == wk2 &&
              i + j + k + l + m <= wk1) {
            flag = true;
            break;
          }
        }
      }
    }
  }

  if (flag)
    cout << 1 << endl;
  else
    cout << 0 << endl;

  return 0;
}
Problem p02843 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define io                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pb push_back
#define rep(i, l, r) for (int i = l; i < r; i++)
#define bep(i, l, r) for (int i = l; i >= r; i--)
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pii>
#define el '\n'
#define sz(s) s.size()
#define all(v) v.begin(), v.end()

int inf = 1e18;
const int N = 4e5 + 5;

/*void dfs(int u,int par,int maxx)
{


    for(int v:adj[u])
    {
        if(v==par)
            continue;
        dfs(v,u,maxx);
    }
}*/
vi freq(N);
vi arr(N);
vi dp(N);
bool help(int mid) {}

int32_t main() {
  io int tt = 1;
  // cin>>t;
  while (tt--) {
    int x;
    cin >> x;
    int arr[6] = {0, 1, 2, 3, 4, 5};
    int num = x / 100;
    int mod = x % (num * 100);
    if (mod == 0) {
      cout << 1 << el;
    } else {
      if (mod > 5 * num)
        cout << 0 << el;
      else
        cout << 1;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define io                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pb push_back
#define rep(i, l, r) for (int i = l; i < r; i++)
#define bep(i, l, r) for (int i = l; i >= r; i--)
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pii>
#define el '\n'
#define sz(s) s.size()
#define all(v) v.begin(), v.end()

int inf = 1e18;
const int N = 4e5 + 5;

/*void dfs(int u,int par,int maxx)
{


    for(int v:adj[u])
    {
        if(v==par)
            continue;
        dfs(v,u,maxx);
    }
}*/
vi freq(N);
vi arr(N);
vi dp(N);
bool help(int mid) {}

int32_t main() {
  io int tt = 1;
  // cin>>t;
  while (tt--) {
    int x;
    cin >> x;
    int arr[6] = {0, 1, 2, 3, 4, 5};
    if (x < 100) {
      cout << 0;
      return 0;
    }
    int num = x / 100;
    int mod = x % (num * 100);
    if (mod == 0) {
      cout << 1 << el;
    } else {
      if (mod > 5 * num)
        cout << 0 << el;
      else
        cout << 1;
    }
  }

  return 0;
}
Problem p02843 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<vector<vector<vector<ll>>>> vvvvl;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;

int X;

int main() {
  cin >> X;
  int a = X % 100; // 下二桁
  int count = 0;
  while (true) {
    if (a == 0) {
      break;
    }
    for (int i = 0; i < 5; i++) {
      if (a - (5 - i) >= 0) {
        a -= (5 - i);
        count++;
        break;
      }
    }
  }
  if (X / count >= 100) {
    cout << 1 << endl;
  } else {
    cout << 0 << endl;
  }
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<vector<vector<vector<ll>>>> vvvvl;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;

int X;

int main() {
  cin >> X;
  int a = X % 100; // 下二桁
  int count = 0;
  while (true) {
    if (a == 0) {
      break;
    }
    for (int i = 0; i < 5; i++) {
      if (a - (5 - i) >= 0) {
        a -= (5 - i);
        count++;
        break;
      }
    }
  }
  if (count == 0) {
    if (X >= 100) {
      cout << 1 << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  }
  if (X / count >= 100) {
    cout << 1 << endl;
  } else {
    cout << 0 << endl;
  }
}
Problem p02843 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a); i > int(b); --i)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define show(v)                                                                \
  rep(i, v.size()) { cout << v[i] << " "; }                                    \
  cout << endl;
#define MOD 1000000007
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define SZ(x) ((int)(x).size())
#define sum(v, i) accumulate(all(v), i)
// #define int long long
using ll = long long;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 最大公約数
int lcm(int a, int b) { return a * b / gcd(a, b); }     // 最小公倍数
// template <typename... Args>void IN(Args& ... args) {cin
// >>args;IN(args...);return;} //まとめて入力　IN(a,b,c,d...)
void Main() {
  int n;
  cin >> n;
  int v[100001] = {};
  v[0] = 1;
  // rep(i, n)cout << v[i];
  repi(i, 0, n) {
    // cout << v;
    if (v[i] == 1) {
      repi(j, 100, 106) { v[i + j] = 1; }
    }
  }
  // rep(i,n)cout << v[i];
  cout << v[n];
}

signed main() {
  Main();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a); i > int(b); --i)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define show(v)                                                                \
  rep(i, v.size()) { cout << v[i] << " "; }                                    \
  cout << endl;
#define MOD 1000000007
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define SZ(x) ((int)(x).size())
#define sum(v, i) accumulate(all(v), i)
// #define int long long
using ll = long long;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 最大公約数
int lcm(int a, int b) { return a * b / gcd(a, b); }     // 最小公倍数
// template <typename... Args>void IN(Args& ... args) {cin
// >>args;IN(args...);return;} //まとめて入力　IN(a,b,c,d...)
void Main() {
  int n;
  cin >> n;
  bitset<1000001> v;
  v[0] = 1;
  // rep(i, n)cout << v[i];
  repi(i, 0, n) {
    // cout << v;
    if (v[i] == 1) {
      repi(j, 100, 106) { v[i + j] = 1; }
    }
  }
  // rep(i,n)cout << v[i];
  cout << v[n];
}

signed main() {
  Main();

  return 0;
}
Problem p02843 (C++)

Buggy submission (status: Runtime Error):
// Dragon_warrior7(IITK)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define maxin(v) *max_element(v.begin(), v.end())
#define minin(v) *min_element(v.begin(), v.end())
#define sumall(vect) accumulate(vect.begin(), vect.end(), 0)
#define count(x, vect) count(vect.begin(), vect.end(), x)
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 100000000000000000
#define deb(x) cout << #x << " " << x << "\n"
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cout << "------------------------------------\n"
#define min(a, b, c) min(a, min(b, c))
#define max(a, b, c) max(a, max(b, c))
void read(vector<ll> &v) {
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
}

void print(vector<ll> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
}
ll n, m, a, b, temp;
string str;
set<ll> s;
vll v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  bool dp[100000 + 1] = {0};
  dp[0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 100; j <= 105; j++) {
      if (dp[i] == 1)
        dp[i + j] = 1;
    }
  }
  // for(int i=0;i<=n;i++)
  //	{deb(i);
  //	deb(dp[i]);}
  if (dp[n] == 1)
    cout << 1;
  else
    cout << 0;
}

Fixed submission:
// Dragon_warrior7(IITK)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define maxin(v) *max_element(v.begin(), v.end())
#define minin(v) *min_element(v.begin(), v.end())
#define sumall(vect) accumulate(vect.begin(), vect.end(), 0)
#define count(x, vect) count(vect.begin(), vect.end(), x)
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 100000000000000000
#define deb(x) cout << #x << " " << x << "\n"
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cout << "------------------------------------\n"
#define min(a, b, c) min(a, min(b, c))
#define max(a, b, c) max(a, max(b, c))
void read(vector<ll> &v) {
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
}

void print(vector<ll> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
}
ll n, m, a, b, temp;
string str;
set<ll> s;
vll v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  bool dp[1000000 + 1] = {0};
  dp[0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 100; j <= 105; j++) {
      if (dp[i] == 1)
        dp[i + j] = 1;
    }
  }
  // for(int i=0;i<=n;i++)
  //	{deb(i);
  //	deb(dp[i]);}
  if (dp[n] == 1)
    cout << 1;
  else
    cout << 0;
}
Problem p02843 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

const int m[] = {100, 101, 102, 103, 104, 105};

int main() {

  int x;
  vector<bool> v(100000 + 10, false);

  cin >> x;

  for (int i = 0; i < 6; ++i) {
    v[m[i]] = true;
  }

  for (int i = 100; i <= 100000; ++i) {

    if (v[i] == true) {
      for (int j = 0; j < 6; ++j) {
        v[i + m[j]] = true;
      }
    }
  }

  cout << (int)v[x] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

const int m[] = {100, 101, 102, 103, 104, 105};

int main() {

  int x;
  vector<bool> v(100000 + 300, false);

  cin >> x;

  for (int i = 0; i < 6; ++i) {
    v[m[i]] = true;
  }

  for (int i = 100; i <= 100000; ++i) {

    if (v[i] == true) {
      for (int j = 0; j < 6; ++j) {
        v[i + m[j]] = true;
      }
    }
  }

  cout << (int)v[x] << endl;
  return 0;
}
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(1000):
    T = str(i).zfill(3)
    j = 0
    for c in S:
        if c == T[j]:
            j += 1
            if j > 2:
                ans += 1
                break
print(ans)


Fixed submission:
N = int(input())
S = input()
ans = 0
d = [set() for _ in range(3)]
for i in range(N - 2):
    if S[i] in d[0]:
        continue
    d[0].add(S[i])
    for j in range(i + 1, N - 1):
        if S[i] + S[j] in d[1]:
            continue
        d[1].add(S[i] + S[j])
        for k in range(j + 1, N):
            if S[i] + S[j] + S[k] in d[2]:
                continue
            d[2].add(S[i] + S[j] + S[k])
            ans += 1
print(ans)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
def password():
    for i in range(10):
        for j in range(10):
            for k in range(10):
                yield [str(i), str(j), str(k)]


n = int(input())
s = input()

ans = 0
for perm in password():
    j = 0
    for i in range(n):
        if perm[j] == s[i]:
            j += 1
            if j >= len(perm):
                break
    if j >= len(perm):
        ans += 1

print(ans)


Fixed submission:
def password():
    for i in range(10):
        for j in range(10):
            for k in range(10):
                yield [str(i), str(j), str(k)]


n = int(input())
s = input()

ans = 0
for perm in password():
    if perm[0] in s:
        a = s.find(perm[0])
        if perm[1] in s[a + 1 :]:
            b = s[a + 1 :].find(perm[1])
            if perm[2] in s[a + 1 :][b + 1 :]:
                ans += 1

print(ans)
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1e18

using namespace std;

int a[3];

int check(string s, int d) {
  if (d == 3)
    return 1;

  if (s.size() == 0)
    return 0;

  if ((s[0] - '0') == a[d])
    return check(s.substr(1), d + 1);

  return check(s.substr(1), d);
}

int main(void) {
  ll n;
  string s;
  cin >> n >> s;

  ll res = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        a[0] = i;
        a[1] = j;
        a[2] = k;
        res += check(s, 0);
      }
    }
  }

  std::cout << res << std::endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1e18

using namespace std;

int a[3];

int check(string s, int d) {
  if (d == 3)
    return 1;

  if (s.size() == 0)
    return 0;

  if ((s[0] - '0') == a[d])
    return check(s.substr(1), d + 1);

  return check(s.substr(1), d);
}

int main(void) {
  ll n;
  string s;
  cin >> n >> s;

  ll res = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        a[0] = i;
        a[1] = j;
        a[2] = k;
        int id = 0;
        for (int l = 0; l < n; l++) {
          if (s[l] - '0' == a[id]) {
            id++;
          }
          if (id == 3)
            break;
        }
        if (id == 3)
          res++;
      }
    }
  }

  std::cout << res << std::endl;

  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  string s;
  cin >> n >> s;
  set<string> secrets;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        secrets.insert(string() + s[i] + s[j] + s[k]);
      }
    }
  }
  cout << secrets.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  string s;
  cin >> n >> s;
  set<string> secrets;
  rep(i, 10) {
    int pos1 = s.find(to_string(i));
    if (pos1 == string::npos || pos1 > n - 3)
      continue;
    string substr1 = s.substr(pos1 + 1);
    rep(j, 10) {
      int pos2 = substr1.find(to_string(j));
      if (pos2 == string::npos || pos2 > n - 2)
        continue;
      string substr2 = substr1.substr(pos2 + 1);
      rep(k, 10) {
        if (substr2.find(to_string(k)) != string::npos) {
          secrets.insert(to_string(i) + to_string(j) + to_string(k));
        }
      }
    }
  }
  cout << secrets.size() << endl;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  int ans = 0;
  string s;
  cin >> s;
  int a1[20] = {}, a2[20] = {};
  long long d[20][20][20] = {};
  for (int i = 0; i < n - 2; i++) {
    if (a1[s.at(i) - '0'] == 0) {
      a1[s.at(i) - '0']++;
      for (int j = i + 1; j < n - 1; j++) {
        if (a2[s.at(j) - '0'] == 0) {
          a2[s.at(j) - '0']++;
          for (int k = j + 1; k < n; k++) {
            d[s.at(i) - '0'][s.at(j) - '0'][s.at(k) - '0']++;
          }
        }
      }
    }
    for (int z = 0; z < n; z++) {
      a2[z] = 0;
    }
  }
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        if (d[i][j][k] > 0)
          ans++;
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  int ans = 0;
  string s;
  cin >> s;
  int a1[20] = {}, a2[20] = {};
  long long d[20][20][20] = {};
  for (int i = 0; i < n - 2; i++) {
    if (a1[s.at(i) - '0'] == 0) {
      a1[s.at(i) - '0']++;
      for (int j = i + 1; j < n - 1; j++) {
        if (a2[s.at(j) - '0'] == 0) {
          a2[s.at(j) - '0']++;
          for (int k = j + 1; k < n; k++) {
            d[s.at(i) - '0'][s.at(j) - '0'][s.at(k) - '0']++;
          }
        }
      }
    }
    for (int z = 0; z < 10; z++) {
      a2[z] = 0;
    }
  }
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        if (d[i][j][k] > 0)
          ans++;
      }
    }
  }
  cout << ans;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> ints(n);
  rep(i, n) ints[i] = s[i] - '0';

  // 累積和
  vector<vector<int>> sum(n, vector<int>(10));
  sum[ints[0]][0]++;
  rep(i, n - 1) {
    sum[i + 1] = sum[i];
    sum[i + 1][ints[i + 1]] = sum[i][ints[i + 1]] + 1;
  }

  // make first
  vector<int> first(10, n); // 一つもなければn
  rep(i, 10) {
    rep(j, n) {
      if (ints[j] == i) {
        first[i] = j;
        break;
      }
    }
  }

  // make last
  vector<int> last(10, -1); // 一つもなければ-1
  rep(i, 10) {
    rep(j, n) {
      if (ints[n - 1 - j] == i) {
        last[i] = n - 1 - j;
        break;
      }
    }
  }

  ll res = 0;
  rep(i, 10) {
    if (first[i] == n)
      continue;
    rep(j, 10) {
      if (last[j] == -1)
        continue;
      rep(mid, 10) {
        int left = first[i] + 1;
        int right = last[j] - 1;
        if (left == right && ints[left] == mid) {
          res++;
          // cout << res << "-----" << i << mid << j << left << endl;
        } else if (left < right && sum[right][mid] - sum[left - 1][mid] > 0) {
          res++;
          // cout << res << "---" << i << mid << j << endl;
        }
      }
    }
  }

  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> ints(n);
  rep(i, n) ints[i] = s[i] - '0';

  // 累積和
  vector<vector<int>> sum(n, vector<int>(10));
  sum[0][ints[0]]++;
  rep(i, n - 1) {
    sum[i + 1] = sum[i];
    sum[i + 1][ints[i + 1]] = sum[i][ints[i + 1]] + 1;
  }

  // make first
  vector<int> first(10, n); // 一つもなければn
  rep(i, 10) {
    rep(j, n) {
      if (ints[j] == i) {
        first[i] = j;
        break;
      }
    }
  }

  // make last
  vector<int> last(10, -1); // 一つもなければ-1
  rep(i, 10) {
    rep(j, n) {
      if (ints[n - 1 - j] == i) {
        last[i] = n - 1 - j;
        break;
      }
    }
  }

  ll res = 0;
  rep(i, 10) {
    if (first[i] == n)
      continue;
    rep(j, 10) {
      if (last[j] == -1)
        continue;
      rep(mid, 10) {
        int left = first[i] + 1;
        int right = last[j] - 1;
        if (left == right && ints[left] == mid) {
          res++;
          // cout << res << "-----" << i << mid << j << left << endl;
        } else if (left < right && sum[right][mid] - sum[left - 1][mid] > 0) {
          res++;
          // cout << res << "---" << i << mid << j << endl;
        }
      }
    }
  }

  cout << res << endl;
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(cont) cont.begin(), cont.end()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN                                                                    \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 1000000;

typedef long long int ll;

using namespace std;

int memo[N];

int main() {
  FIN;
#ifndef ONLINE_JUDGE
  freopen("../in.txt", "r", stdin);
  freopen("../out.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  string val;
  cin >> val;
  int ans = 0;
  fore(i, 0, 10) {
    fore(j, 0, 10) {
      fore(k, 0, 10) {
        bool c1 = false;
        bool c2 = false;
        bool c3 = false;
        fore(pos, 0, SZ(val)) {
          if (val[pos] - 48 == i && !c1) {
            c1 = true;
          } else if (val[pos] - 48 == j && !c2 && c1) {
            c2 = true;
          } else if (val[pos] - 48 == k && !c3 && c2 && c1) {
            c3 = true;
          }
        }
        if (c1 && c2 && c3) {
          ans++;
        }
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(cont) cont.begin(), cont.end()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN                                                                    \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 1000000;

typedef long long int ll;

using namespace std;

int memo[N];

int main() {
  int n;
  cin >> n;
  string val;
  cin >> val;
  int ans = 0;
  fore(i, 0, 10) {
    fore(j, 0, 10) {
      fore(k, 0, 10) {
        bool c1 = false;
        bool c2 = false;
        bool c3 = false;
        fore(pos, 0, SZ(val)) {
          if (val[pos] - 48 == i && !c1) {
            c1 = true;
          } else if (val[pos] - 48 == j && !c2 && c1) {
            c2 = true;
          } else if (val[pos] - 48 == k && !c3 && c2 && c1) {
            c3 = true;
          }
        }
        if (c1 && c2 && c3) {
          ans++;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  string S;

  cin >> N;
  cin >> S;

  int count = 0;
  bool finished = false;
  int tmp;
  vector<int> digit(3);

  for (int num = 0; num < 1000; num++) {
    tmp = num;
    for (int d = 2; d >= 0; d--) {
      digit[d] = tmp % 10;
      tmp /= 10;
    }

    finished = false;

    for (int i = 0; i < N - 2; i++) {
      if (S[i] - '0' == digit[0]) {
        for (int j = i + 1; j < N - 1; j++) {
          if (S[j] - '0' == digit[1]) {
            for (int k = j + 1; k < N; k++) {
              if (S[k] - '0' == digit[2]) {
                count++;
                finished = true;
                break;
              }
            }
            finished = true;
            break;
          }
          if (finished == true) {
            break;
          }
        }
      }
      if (finished == true) {
        break;
      }
    }
  }

  cout << count << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  string S;

  cin >> N;
  cin >> S;

  int count = 0;
  bool finished = false;
  int tmp;
  vector<int> digit(3);

  for (int num = 0; num < 1000; num++) {
    tmp = num;
    for (int d = 2; d >= 0; d--) {
      digit[d] = tmp % 10;
      tmp /= 10;
    }

    finished = false;

    for (int i = 0; i < N - 2; i++) {
      if (S[i] - '0' == digit[0]) {
        for (int j = i + 1; j < N - 1; j++) {
          if (S[j] - '0' == digit[1]) {
            for (int k = j + 1; k < N; k++) {
              if (S[k] - '0' == digit[2]) {
                count++;
                finished = true;
                break;
              }
            }
            finished = true;
            break;
          }
          if (finished == true) {
            break;
          }
        }
        finished = true;
        break;
      }
      if (finished == true) {
        break;
      }
    }
  }

  cout << count << endl;
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;

using pqg = priority_queue<ll, vector<ll>, greater<ll>>; // 1,3,5..
using pql = priority_queue<ll, vector<ll>>;              // 5, 3, 1...

using pqg_p = priority_queue<P, vector<P>, greater<P>>; // 1,3,5..
using pql_p = priority_queue<P, vector<P>>;             // 5, 3, 1...

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define REPS(i, s, n) for (int i = s, i##_len = (n); i < i##_len; ++i)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  set<string> s;
  set<string> s2;
  set<string> s3;
  int N;
  string _s;
  cin >> N;
  cin >> _s;

  REP(i, N - 2) {
    string _s3 = _s.substr(i, 1);
    if (s3.count(_s3) != 0)
      continue;

    REPS(j, i + 1, N - 1) {
      string _s2 = _s.substr(i, 1) + _s.substr(j, 1);
      if (s2.count(_s2) != 0)
        continue;

      s2.insert(_s2);
      REPS(k, j + 1, N) { s.insert(_s2 + _s.substr(k, 1)); }
    }
  }

  cout << s.size() << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;

using pqg = priority_queue<ll, vector<ll>, greater<ll>>; // 1,3,5..
using pql = priority_queue<ll, vector<ll>>;              // 5, 3, 1...

using pqg_p = priority_queue<P, vector<P>, greater<P>>; // 1,3,5..
using pql_p = priority_queue<P, vector<P>>;             // 5, 3, 1...

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define REPS(i, s, n) for (int i = s, i##_len = (n); i < i##_len; ++i)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  set<string> s;
  set<string> s2;
  set<string> s3;
  int N;
  string _s;
  cin >> N;
  cin >> _s;

  REP(i, N - 2) {
    string _s3 = _s.substr(i, 1);
    if (s3.count(_s3) != 0)
      continue;
    s3.insert(_s3);

    REPS(j, i + 1, N - 1) {
      string _s2 = _s.substr(i, 1) + _s.substr(j, 1);
      if (s2.count(_s2) != 0)
        continue;

      s2.insert(_s2);
      REPS(k, j + 1, N) { s.insert(_s2 + _s.substr(k, 1)); }
    }
  }

  cout << s.size() << endl;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  int n;
  string s;
  cin >> n >> s;

  int ans = 0;

  for (int i = 0; i < 1000; ++i) {
    char tar[3];
    sprintf(tar, "%03lld", i);

    int cnt = 0;

    for (int j = 0; j < n; ++j) {
      if (s[j] == tar[cnt]) {
        cnt++;
      }
    }

    if (cnt == 3)
      ans++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  int n;
  string s;
  cin >> n >> s;

  int ans = 0;

  for (int i = 0; i < 1000; ++i) {
    char tar[4];
    sprintf(tar, "%03lld", i);

    int cnt = 0;

    for (int j = 0; j < n; ++j) {
      if (s[j] == tar[cnt]) {
        cnt++;
      }
    }

    if (cnt == 3)
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define pll pair<ll, ll>
#define pint pll
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int l[10][10000], r[10][10000];

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<tuple<int, int, int>> se;
  rep(i, n) {
    if (i - 1 >= 0)
      rep(j, 10) l[j][i] = l[j][i - 1];
    l[s[i] - '0'][i]++;
  }
  rep(i, n) {
    rep(j, 10) r[j][n - 1 - i] = r[j][n - i];
    r[s[n - 1 - i] - '0'][n - 1 - i]++;
  }
  for (int i = 1; i <= n - 2; i++) {
    rep(j, 10) rep(k, 10) {
      if (l[j][i - 1] * r[k][i + 1] > 0) {
        tuple<int, int, int> t = make_tuple(j, (int)(s[i] - '0'), k);
        se.insert(t);
      }
    }
  }
  cout << se.size() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define pll pair<ll, ll>
#define pint pll
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int l[10][40000], r[10][40000];

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<tuple<int, int, int>> se;
  rep(i, n) {
    if (i - 1 >= 0)
      rep(j, 10) l[j][i] = l[j][i - 1];
    l[s[i] - '0'][i]++;
  }
  rep(i, n) {
    rep(j, 10) r[j][n - 1 - i] = r[j][n - i];
    r[s[n - 1 - i] - '0'][n - 1 - i]++;
  }
  for (int i = 1; i <= n - 2; i++) {
    rep(j, 10) rep(k, 10) {
      if (l[j][i - 1] * r[k][i + 1] > 0) {
        tuple<int, int, int> t = make_tuple(j, (int)(s[i] - '0'), k);
        se.insert(t);
      }
    }
  }
  cout << se.size() << endl;
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {

  int n;
  string s;
  cin >> n >> s;
  set<int> lucky;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        int num = (s[i] - '0') * 100 + (s[j] - '0') * 10 + (s[k] - '0');
        lucky.insert(num);
      }
    }
  }
  cout << lucky.size() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {

  int n;
  string s;
  cin >> n >> s;
  set<int> lucky;
  int dig1, dig2, dig3;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        int now = 0;
        for (int l = 0; l < n; l++) {
          if (now == 0) {
            if (s[l] == i + '0') {
              dig1 = (int)(s[l] - '0') * 100;
              now++;
            }
          } else if (now == 1) {
            if (s[l] == j + '0') {
              dig2 = (int)(s[l] - '0') * 10;
              now++;
            }
          } else {
            if (s[l] == k + '0') {
              int num = dig1 + dig2 + (int)(s[l] - '0');
              lucky.insert(num);
              break;
            }
          }
        }
      }
    }
  }
  cout << lucky.size() << endl;
  return 0;
}
Problem p02844 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
S = input()

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            fi = S.find(S[i])
            fj = S.find(S[j])
            fk = S.find(S[k])
            if fi >= 0 and fj >= 0 and fk >= 0 and fi < fj and fj < fk:
                ans += 1

print(ans)


Fixed submission:
N = int(input())
S = input()

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            fi = S.find(str(i))
            if fi < 0:
                continue
            fj = S[fi + 1 :].find(str(j))
            if fj < 0:
                continue
            fk = S[fi + fj + 2 :].find(str(k))
            if fk < 0:
                continue
            ans += 1

print(ans)
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  string S;
  cin >> S;
  int res = 0;
  vector<vector<int>> done(10, vector<int>(10, 0));
  for (int i = 0; i < N; ++i) {
    vector<int> suf_cnt(10, 0);
    int occ_cnt = 0;
    for (int j = i + 1; j < N; ++j)
      if (++suf_cnt[S[j] - '0'] == 1)
        ++occ_cnt;
    for (int j = i + 1; j < N; ++j) {
      if (--suf_cnt[S[j] - '0'] == 0)
        --occ_cnt;
      if (!done[S[i] - '0'][S[j] - '0']) {
        done[S[i] - '0'][S[j] - '0'] = 1;
        res += occ_cnt;
      }
    }
  }
  cout << res << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  string S;
  cin >> S;
  int res = 0;
  char c[3];
  for (c[0] = '0'; c[0] <= '9'; ++c[0]) {
    for (c[1] = '0'; c[1] <= '9'; ++c[1]) {
      for (c[2] = '0'; c[2] <= '9'; ++c[2]) {
        int j = 0;
        for (int i = 0; i < N && j < 3; ++i) {
          if (S[i] == c[j]) {
            ++j;
          }
        }
        res += (j == 3);
      }
    }
  }
  cout << res << "\n";

  return 0;
}
Problem p02844 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
s = input()

candidates = [0 for i in range(10**3)]

count = 0
for i in range(1000):
    tmp_pass = f"{i:03}"

    index_0 = s.find(tmp_pass[0])
    if index_0 != -1 and index_0 < len(s) - 2:
        s_1 = s[index_0 + 1 :]
        index_1 = s_1.find(tmp_pass[1])
        if index_1 != -1 and index_1 < len(s_1) - 1:
            s_2 = s_1[index_1 + 1 :]
            # print(tmp_pass)
            # print(s_2)
            # index_2 = s_2.find(tmp_pass[2])
            # print(index_2)
            if s_2.find(tmp_pass[2]) != -1:
                count += 1

print(count)


Fixed submission:
n = int(input())
s = input()

candidates = [0 for i in range(10**3)]

count = 0
for i in range(1000):
    tmp_pass = "{:03}".format(i)

    index_0 = s.find(tmp_pass[0])
    if index_0 != -1 and index_0 < len(s) - 2:
        s_1 = s[index_0 + 1 :]
        index_1 = s_1.find(tmp_pass[1])
        if index_1 != -1 and index_1 < len(s_1) - 1:
            s_2 = s_1[index_1 + 1 :]
            # print(tmp_pass)
            # print(s_2)
            # index_2 = s_2.find(tmp_pass[2])
            # print(index_2)
            if s_2.find(tmp_pass[2]) != -1:
                count += 1

print(count)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N = int(input())
S = input().rstrip()

ans = 0
for i in range(1000):
    t = str(i).zfill(3)
    ti = 0
    for j in range(N):
        if S[j] == t[ti]:
            ti += 1
        if ti == 3:
            break
    if ti == 3:
        ans += 1

print(ans)


Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N = int(input())
S = input().rstrip()

ans = 0
for i in range(1000):
    t = str(i).zfill(3)
    t1 = S.find(t[0])
    if t1 == -1:
        continue
    t2 = S.find(t[1], t1 + 1)
    if t2 == -1:
        continue
    t3 = S.find(t[2], t2 + 1)
    if t3 == -1:
        continue
    ans += 1

print(ans)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

input = sys.stdin.readline

n = int(input())
s = input()
ans = 0

for i in range(1000):
    cnt = 0
    if i < 100:
        if i < 10:
            match = "00" + str(i)
        else:
            match = "0" + str(i)
    else:
        match = str(i)

    for j in range(n):
        if s[j] == match[cnt]:
            cnt += 1

        if cnt == 3:
            ans += 1
            break

print(ans)


Fixed submission:
import sys

input = sys.stdin.readline

n = int(input())
s = input()
ans = 0

for i in range(1000):
    cnt = 0
    if i < 100:
        if i < 10:
            match = "00" + str(i)
        else:
            match = "0" + str(i)
    else:
        match = str(i)

    for S in s:
        if S == match[cnt]:
            cnt += 1

        if cnt == 3:
            ans += 1
            break

print(ans)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
S = input()

ans = 0
for i in range(1000):
    a = "{0:03d}".format(i)
    f1, f2 = False, False
    for s in S:
        if not f1 and s == a[0]:
            f1 = True
            continue
        if f1 and not f2 and s == a[1]:
            f2 = True
            continue
        if f1 and f2 and s == a[2]:
            ans += 1
            break

print(ans)


Fixed submission:
n = int(input())
S = input()

ans = 0
for i in range(1000):
    a = "{0:03d}".format(i)
    if a[0] in S:
        idx1 = S.index(a[0])
        S1 = S[idx1 + 1 :]
        if a[1] in S1:
            idx2 = S1.index(a[1])
            S2 = S1[idx2 + 1 :]
            if a[2] in S2:
                ans += 1

print(ans)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0

for i in range(10):
    for j in range(10):
        for k in range(10):
            hit = 0
            for c in S:
                c_ = int(c)
                if hit == 0 and c_ == i:
                    hit += 1
                elif hit == 1 and c_ == j:
                    hit += 1
                elif hit == 2 and c_ == k:
                    ans += 1
                    break
print(ans)


Fixed submission:
N = int(input())
S = input()
ans = 0

for i in range(10):
    ind_i = S.find(str(i))
    if ind_i != -1:
        S2 = S[ind_i + 1 :]
        for j in range(10):
            ind_j = S2.find(str(j))
            if ind_j != -1:
                S3 = S2[ind_j + 1 :]
                for k in range(10):
                    cnt_k = S3.count(str(k))
                    if cnt_k > 0:
                        ans += 1


print(ans)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
# 000~999で決め打ちして数える
n = int(input())
s = input()
ans = 0

for i in range(0, 1000):
    t = format(i, "0>3")
    idx = 0
    for c in s:
        if c == t[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)


Fixed submission:
# 000~999で決め打ちして数える
# TLEになるんだけどこれそんなに重いか？？？？？

n = int(input())
s = input()
ans = 0

for i in range(0, 1000):
    t = format(i, "0>3")
    idx = 0
    for c in s:
        if c == t[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = map(int, input())

dp = tuple([0] * 1000 for _ in range(4))
dp[0][0] = 1
# dp[x][int(str)]:=x文字のstrは構成可能か？0/1

for x in s:
    for k in range(2, -1, -1):
        for incomplete_key in range(100):
            dp[k + 1][incomplete_key * 10 + x] |= dp[k][incomplete_key]

print(sum(dp[3]))

# dp


Fixed submission:
n = int(input())
s = map(int, input())

dp = tuple([0] * 1000 for _ in range(4))
dp[0][0] = 1
# dp[x][int(str)]:=x文字のstrは構成可能か？0/1

for x in s:
    for incomplete_key in range(100):
        dp[3][incomplete_key * 10 + x] |= dp[2][incomplete_key]
    for incomplete_key in range(10):
        dp[2][incomplete_key * 10 + x] |= dp[1][incomplete_key]
    dp[1][x] = 1

print(sum(dp[3]))

# dp
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()

ans = set()
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            ans.add(S[i] + S[j] + S[k])

print(len(ans))


Fixed submission:
N = int(input())
S = input()

ans = set()
for i in range(10):
    i_index = S.find(str(i))
    if i_index == -1:
        continue
    for j in range(10):
        j_index = S.find(str(j), i_index + 1)
        if j_index == -1:
            continue
        for k in range(10):
            k_index = S.find(str(k), j_index + 1)
            if k_index == -1:
                continue
            ans.add("{}{}{}".format(i, j, k))

print(len(ans))
Problem p02844 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    count = 0
    for i in range(1000):
        left = 0
        not_found = False
        for c in f"{i:03}":
            for j in range(left, N):
                if S[j] == c:
                    left = j + 1
                    break
            else:
                not_found = True
                break
            if not_found:
                break
        if not not_found:
            count += 1
    print(count)


if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    count = 0
    for i in range(1000):
        left = 0
        not_found = False
        for c in "{:03}".format(i):
            for j in range(left, N):
                if S[j] == c:
                    left = j + 1
                    break
            else:
                not_found = True
                break
            if not_found:
                break
        if not not_found:
            count += 1
    print(count)


if __name__ == "__main__":
    main()
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import combinations

N, S = int(input()), input()
result = set()
for indexes in combinations(range(N, 0, -1), N - 3):
    tmp = S
    for i in indexes:
        tmp = tmp[: i - 1] + tmp[i:]
    result.add(tmp)
print(len(result))


Fixed submission:
N = input()
S = input()
count = 0
for i in range(1000):
    n = "{:03d}".format(i)
    n1, n2, n3 = n[0], n[1], n[2]
    i1 = S.find(n1)
    if -1 < i1 < len(S) - 2:
        S1 = S[i1 + 1 :]
        i2 = S1.find(n2)
        if -1 < i2 < len(S1) - 1:
            S2 = S1[i2 + 1 :]
            i3 = S2.find(n3)
            if -1 < i3:
                count += 1
                continue
print(count)
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n;
string s;
bool dp[30000][4][1000];

int main() {
  cin >> n >> s;
  dp[0][0][0] = true;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 3; ++j) {
      for (int k = 0; k <= 999; ++k) {
        if (dp[i][j][k] == false)
          continue;
        dp[i + 1][j][k] = true;
        if (j <= 2) {
          dp[i + 1][j + 1][10 * k + s[i] - '0'] = true;
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k <= 999; ++k) {
    if (dp[n][3][k])
      ++ans;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n;
string s;
bool dp[30009][4][1009];

int main() {
  cin >> n >> s;
  dp[0][0][0] = true;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 3; ++j) {
      for (int k = 0; k <= 999; ++k) {
        if (dp[i][j][k] == false)
          continue;
        dp[i + 1][j][k] = true;
        if (j <= 2) {
          dp[i + 1][j + 1][10 * k + s[i] - '0'] = true;
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k <= 999; ++k) {
    if (dp[n][3][k])
      ++ans;
  }
  cout << ans << endl;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

bool dp[3000][4][1010];

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  dp[0][0][0] = true;
  REP(i, n) REP(j, 4) REP(k, 1000) {
    if (dp[i][j][k] == false)
      continue;
    dp[i + 1][j][k] = true;
    if (j < 3)
      dp[i + 1][j + 1][k * 10 + (s[i] - '0')] = true;
  }

  int cnt = 0;
  REP(i, 1000)
  if (dp[n][3][i])
    cnt++;

  cout << cnt << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

bool dp[30001][4][1009];

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  dp[0][0][0] = true;
  REP(i, n) REP(j, 4) REP(k, 1000) {
    if (dp[i][j][k] == false)
      continue;
    dp[i + 1][j][k] = true;
    if (j < 3)
      dp[i + 1][j + 1][k * 10 + (s[i] - '0')] = true;
  }

  int cnt = 0;
  REP(i, 1000)
  if (dp[n][3][i])
    cnt++;

  cout << cnt << '\n';
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

int main(void) {
  using namespace std;

  long n;
  cin >> n;
  string s;
  cin >> s;

  bitset<1000> st;

  long r = 0;

  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      for (int k = j + 1; k < n; k++) {
        long t = 100 * (s[i] - '0') + 10 * (s[j] - '0') + (s[k] - '0');
        r += 1 - (st[t] ? 1 : 0);
        st[t] = 1;
      }

  cout << r << endl;
}


Fixed submission:
#include <bits/stdc++.h>

int main(void) {
  using namespace std;

  long n;
  cin >> n;
  string s;
  cin >> s;

  bitset<1000> st;

  long r = 0;

  for (int i = 0; i < 1000; i++) {
    long a = i / 100;
    long b = (i / 10) % 10;
    long c = i % 10;

    int k = 0;
    while (k < n && s[k++] - '0' != a)
      ;
    while (k < n && s[k++] - '0' != b)
      ;
    while (k < n && s[k] - '0' != c)
      k++;
    r += k < n;
  }

  cout << r << endl;
}