Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
void incliment(int n, int m, vector<int> &after, vector<int> &before) {
#if 0
    cout << "incliment" << endl;
    for(int i = 0;i<before.size();i++){
        cout << before[i] << " ";
    }
    cout << endl;
#endif
  after.clear();
  for (int i = 0; i < before.size(); i++) {
    after.push_back(before[i]);
  }
  for (int i = (after.size()) - 1; i >= 0; i--) {
    // cout << " i:" <<i <<" ";

    if (after[i] < m) {
      // cout << "pathA"<<endl;
      // 位上げなし
      after[i]++;
      return;
      // 次の再帰で再度forloop
    } else {
      // 位上げ有り
      if (i != 0) {
        // cout << "pathB"<<endl;
        for (int j = i; j < n; j++) {
          after[j] = (after[i - 1] + 1);
        }
        // 次のループでv[j-1]がインクリメントしてReturn
      }
    }
  }
}
// void getAllSequence(int n, int m,vector<vector<int>>&vv){
//     //cout << "getAllSequence " << " n:" << n << " m:" << m <<  " vv.size():
//     "<< vv.size()<<endl; vv.push_back(vector<int>()); for(int i = 0; i <
//     vv[vv.size()-2].size(); i++){
//         vv.back().push_back(vv[vv.size()-2][i]);
//     }
//     incliment(n,m,vv.back());
//     if(vv.size()>2){
//         vv[0].clear();
//         vv.erase(vv.begin());
//     }
//     while(true){
//         bool isLast = true;
//         for(int i = 0;i < n;i++){
//             if(vv.back()[i]!=m){
//                 isLast = false;
//             }
//         }
//         if(isLast == true){
//             break;
//         }
//         getAllSequence(n,m,vv);
//     }
// }
int calcScore(int a, int b, int c, int d, vector<int> &v) {
#if 0
    cout << "calcScore ";
    cout << "a:" << a << " b:"<<b<<" c:"<<c<<" d:"<<d<<endl;
#endif
#if 1
  int result = 0;
  int indexA = a - 1;
  int indexB = b - 1;
  if ((v[indexB] - v[indexA]) == c) {
    result = d;
  }
  // cout <<"result:"<< result<<endl;
  return result;
#endif
}
int main() {
  int n, m, q;
  cin >> n;
  cin >> m;
  cin >> q;
  vector<int> a, b, c;
  vector<long> d;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
    cin >> tmp;
    b.push_back(tmp);
    cin >> tmp;
    c.push_back(tmp);
    long ttmp;
    cin >> ttmp;
    d.push_back(ttmp);
  }
#if 0
    cout << "n " << n << " m " << m << " q " << q << endl;
    for(int i = 0; i < q; i++){
        cout << "i " << i << " a " << a[i] << " b " << b[i] << " c " << c[i] << " d " << d[i] << endl;
    }
#endif
  int result = 0;
  int tmp = 0;
  vector<int> before;
  vector<int> after;
  for (int i = 0; i < n; i++) {
    before.push_back(1);
  }
  for (int i = 0; i < q; i++) {
    tmp += calcScore(a[i], b[i], c[i], d[i], before);
  }
  if (tmp > result) {
    result = tmp;
    tmp = 0;
  }
#if 0
    for(int i = 0; i < before.size();i++){
        cout << before[i]<< " ";
    }
    cout << endl;
#else
  while (true) {
    if (m != 1) {
      incliment(n, m, after, before);
    } else {
      for (int i = 0; i < before.size(); i++) {
        after.push_back(before[i]);
      }
    }
    for (int i = 0; i < after.size(); i++) {
      // cout << after[i] << " ";
    }
    // cout << endl;
    for (int i = 0; i < q; i++) {
      int ttmp = calcScore(a[i], b[i], c[i], d[i], after);
      tmp += calcScore(a[i], b[i], c[i], d[i], after);
      // if(ttmp != 0){
      //     cout << "i:"<<i<<endl;
      //     cout << ttmp << endl;
      //     //cout << tmp << endl;
      // }
    }
    if (tmp > result) {
      result = tmp;
      // cout <<"resutl "<< result << endl;
    }
    tmp = 0;
    bool isOver = true;
    for (int i = 0; i < after.size(); i++) {
      if (after[i] != m) {
        isOver = false;
      }
    }
    if (isOver) {
      break;
    }
    for (int i = 0; i < before.size(); i++) {
      before[i] = after[i];
    }
    after.clear();
  }
#endif
  // cout << "kkmdkt"<<endl;
  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void incliment(int n, int m, vector<int> &after, vector<int> &before) {
#if 0
    cout << "incliment" << endl;
    for(int i = 0;i<before.size();i++){
        cout << before[i] << " ";
    }
    cout << endl;
#endif
  after.clear();
  for (int i = 0; i < before.size(); i++) {
    after.push_back(before[i]);
  }
  for (int i = (after.size()) - 1; i >= 0; i--) {
    // cout << " i:" <<i <<" ";
    if (after[i] < m) {
      // cout << "pathA"<<endl;
      // 位上げなし
      after[i]++;
      return;
      // 次の再帰で再度forloop
    } else {
      // 位上げ有り
      if (i != 0) {
        // cout << "pathB"<<endl;
        for (int j = i; j < n; j++) {
          after[j] = (after[i - 1] + 1);
        }
        // 次のループでv[j-1]がインクリメントしてReturn
      }
    }
  }
}
// void getAllSequence(int n, int m,vector<vector<int>>&vv){
//     //cout << "getAllSequence " << " n:" << n << " m:" << m <<  " vv.size():
//     "<< vv.size()<<endl; vv.push_back(vector<int>()); for(int i = 0; i <
//     vv[vv.size()-2].size(); i++){
//         vv.back().push_back(vv[vv.size()-2][i]);
//     }
//     incliment(n,m,vv.back());
//     if(vv.size()>2){
//         vv[0].clear();
//         vv.erase(vv.begin());
//     }
//     while(true){
//         bool isLast = true;
//         for(int i = 0;i < n;i++){
//             if(vv.back()[i]!=m){
//                 isLast = false;
//             }
//         }
//         if(isLast == true){
//             break;
//         }
//         getAllSequence(n,m,vv);
//     }
// }
int calcScore(int a, int b, int c, int d, vector<int> &v) {
#if 0
    cout << "calcScore ";
    cout << "a:" << a << " b:"<<b<<" c:"<<c<<" d:"<<d<<endl;
#endif
#if 1
  int result = 0;
  int indexA = a - 1;
  int indexB = b - 1;
  if ((v[indexB] - v[indexA]) == c) {
    result = d;
  }
  // cout <<"result:"<< result<<endl;
  return result;
#endif
}
int main() {
  int n, m, q;
  cin >> n;
  cin >> m;
  cin >> q;
  vector<int> a, b, c;
  vector<long> d;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
    cin >> tmp;
    b.push_back(tmp);
    cin >> tmp;
    c.push_back(tmp);
    long ttmp;
    cin >> ttmp;
    d.push_back(ttmp);
  }
#if 0
    cout << "n " << n << " m " << m << " q " << q << endl;
    for(int i = 0; i < q; i++){
        cout << "i " << i << " a " << a[i] << " b " << b[i] << " c " << c[i] << " d " << d[i] << endl;
    }
#endif
  int result = 0;
  int tmp = 0;
  vector<int> before;
  vector<int> after;
  for (int i = 0; i < n; i++) {
    before.push_back(1);
  }
  for (int i = 0; i < q; i++) {
    tmp += calcScore(a[i], b[i], c[i], d[i], before);
  }
  if (tmp > result) {
    result = tmp;
    tmp = 0;
  }
#if 0
    for(int i = 0; i < before.size();i++){
        cout << before[i]<< " ";
    }
    cout << endl;
#else
  while (true) {
    incliment(n, m, after, before);
    // for(int i = 0; i<after.size();i++){
    //     cout << after[i] << " ";
    // }
    // cout << endl;
    for (int i = 0; i < q; i++) {
      int ttmp = calcScore(a[i], b[i], c[i], d[i], after);
      tmp += calcScore(a[i], b[i], c[i], d[i], after);
      // if(ttmp != 0){
      //     cout << "i:"<<i<<endl;
      //     cout << ttmp << endl;
      //     //cout << tmp << endl;
      // }
    }
    if (tmp > result) {
      result = tmp;
      // cout <<"resutl "<< result << endl;
    }
    tmp = 0;
    bool isOver = true;
    for (int i = 0; i < after.size(); i++) {
      if (after[i] != m) {
        isOver = false;
      }
    }
    if (isOver) {
      break;
    }
    for (int i = 0; i < before.size(); i++) {
      before[i] = after[i];
    }
    after.clear();
  }
#endif
  // cout << "kkmdkt"<<endl;
  cout << result << endl;
  return 0;
}