Why am I getting a segmentation fault in the following code? This code starts with an array s whose first element is 0. Then an array t whose elements are compliment of s and then t is appended to s until size of is greater than 1000. The user then inputs the number of queries and the element with index x of array s is printed.
#include <bits/stdc++.h>
using namespace std;
int duplication(int x){
     // Complete this function
     vector <int> s;
     vector <int> t;
     int i=0,j;
     int n=0;
     s.push_back(n);
     while(true){
         t.clear();
         for(j=0;j<s.size();j++){
             int k = 1 - s.at(j);
             t.push_back(k);
         }
        for(j=0;j<s.size();j++){
             s.push_back(t[j]);
        }
        if(s.size()>1000) break;
    }
    i=s[x];
    return i;
}
int main() {
    int q;
    cin >> q;
    for(int a0 = 0; a0 < q; a0++){
        int x;
        cin >> x;
        int result = duplication(x);
        cout << result << endl;
    }
    return 0;
}
 
     
     
     
    