#include #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; random_device rnd; mt19937 mt(rnd()); int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int N; cin >> N; string S; cin >> S; int mod3 = 0; vector cnt(10, 0); for(char c : S) { cnt[c - '0']++; mod3 = (mod3 + c - '0') % 3; } if(mod3 == 0) { mod3 += 3; } vector can(120, false); while(mod3 < 120000) { int tmp = mod3; vector c(10, 0); while(tmp > 0) { c[tmp % 10]++; tmp /= 10; } bool fn = true; for(int i = 0; i < 10; i++) { if(c[i] > cnt[i]) { fn = false; } } if(fn) { can[mod3 % 120] = true; } mod3 += 3; } cout << accumulate(can.begin(), can.end(), 0) << '\n'; return 0; }