Josif_tepe

Untitled

Jul 23rd, 2025
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. const int maxn = 200001;
  6. const ll MOD = 1e9 + 7;
  7. int n;
  8. string s;
  9. ll dp[maxn][8];
  10.  
  11. ll rec(int at, int remainder) {
  12.     if(at == n and remainder == 0) {
  13.         return 1;
  14.     }
  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.    
  27.     int new_remainder = (remainder * 10 + (s[at] - '0')) % 8;
  28.     res += rec(at + 1, new_remainder);
  29.     res %= MOD;
  30.    
  31.     dp[at][remainder] = res;
  32.     return res;
  33. }
  34. int main() {
  35.     cin >> n;
  36.     cin >> s;
  37.    
  38.     memset(dp, -1, sizeof dp);
  39.     cout << rec(0, 0) - 1 << endl;
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment