Josif_tepe

Untitled

Nov 12th, 2025
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5. typedef long long ll;
  6. const int maxn = 200005;
  7. const ll MOD = 1e9 + 7;
  8. int n;
  9. string s;
  10.  
  11. ll dp[maxn][8];
  12. ll rec(int at, int remainder) {
  13.     if(at == n and remainder == 0) {
  14.         return 1;
  15.     }
  16.     if(at == n) {
  17.         return 0;
  18.     }
  19.    
  20.     if(dp[at][remainder] != -1) {
  21.         return dp[at][remainder];
  22.     }
  23.     ll res = 0;
  24.     res += rec(at + 1, remainder);
  25.     res %= MOD;
  26.     int new_remainder = (remainder * 10 + (s[at] - '0')) % 8;
  27.    
  28.    
  29.     res += rec(at + 1, new_remainder);
  30.     res %= MOD;
  31.     dp[at][remainder] = res;
  32.     return res;
  33. }
  34. int main() {
  35.     ios_base::sync_with_stdio(false);
  36.     cin >> n >> s;
  37.     memset(dp, -1, sizeof dp);
  38.    
  39.     cout << rec(0, 0) - 1 << endl;
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment