Language: C++

#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,NAME,...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)
#define pr1(a) (#a)<<"="<<(a)
#define pr2(a,b) pr1(a)<<", "<<pr1(b)
#define pr3(a,b,c) pr2(a,b)<<", "<<pr1(c)
#define pr4(a,b,c,d) pr3(a,b,c)<<", "<<pr1(d)
#define pr5(a,b,c,d,e) pr4(a,b,c,d)<<", "<<pr1(e)
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class Int{
public:
  string val;
  Int():val("0"){}
  Int(string num,bool reversed=0){if(!reversed)R(num); val = num;}
  Int(int num){val = to_string(num); R(val);}
  
  void R(string &a){reverse(a.begin(),a.end());}
  bool neg(const string &a)const{return a.back() == '-';}
  char& operator [] (int i){assert(i<(int)val.size());return val[i];}

  bool operator ==  (Int b) {R(b.val); return *this == b.val;}
  bool operator ==  (string b) {R(b);return val == b;}
  template<class T> bool operator == (T b){return *this == Int(b);}
  
  bool operator !=  (Int b) {R(b.val); return *this != b.val;}
  bool operator !=  (string b) {R(b);return !(val == b);}
  template<class T> bool operator != (T b){return *this != Int(b);}
  
  bool operator >=  (Int b) {R(b.val); return *this >= b.val;}
  bool operator >=  (string b) {return !(*this < b);}
  template<class T> bool operator >= (T b){return *this >= Int(b);}

  bool operator >   (Int b) {R(b.val); return *this > b.val;}
  bool operator >   (string b) {return *this>=b && *this != b;}
  template<class T> bool operator > (T b){return *this > Int(b);}

  bool operator <=  (Int b) {R(b.val); return *this <= b.val;}
  bool operator <=  (string b) {return *this<b || *this == b;}
  template<class T> bool operator <= (T b){return *this <= Int(b);}

  bool operator <   (Int b) {R(b.val); return *this < b.val;}
  template<class T> bool operator < (T b){return *this < Int(b);}
 
   bool operator <   (string b) {
    R(b);
    const string &a = val;
    if(neg(a) != neg(b)) return neg(a)? 1:0;
    if(a.size() != b.size()) return !neg(a)? (a.size()<b.size()):(a.size()>b.size());
    for(int i=a.size()-1;i>=0;i--)if(a[i] != b[i]) return !neg(a)? a[i] < b[i]: a[i] > b[i];
    return 0;
  }
  
  Int operator + (string b){
    R(b);
    string a = val;
    string fa = neg(a)? "-":"";
    string fb = neg(b)? "-":"";
    if(neg(a)) a.pop_back();
    if(neg(b)) b.pop_back();
    if(Int(a,1) < Int(b,1)) swap(a,b),swap(fa,fb);
    
    if(fa == fb){
      a += '0';
      for(int i=0;i<(int)a.size()-1;i++){
        if(i < (int)b.size()) a[i] += b[i]-'0';
        a[i+1] += (a[i]-'0')/10;
        a[i] = (a[i]-'0')%10 + '0';
      }
      if(a.size() > 1 && a.back() == '0') a.pop_back();
      if((int)a.size() > 1 && a.back() == '0') a.pop_back();
      if(a != "0") a += fa;
    }
    else {
      for(int i=0;i<(int)a.size();i++){
        if(i < (int)b.size()) a[i] -= b[i]-'0';
        if(a[i]-'0' < 0) a[i]+=10, a[i+1]--; 
      }
      while((int)a.size()>1 && a.back() == '0') a.pop_back();
      if(a != "0") a += fa;
    }
    return Int(a,1);
  }

  Int operator * (string b){
    R(b);
    vector<int> res;
    string a = val;
    string fa = neg(a)? "-":"";
    string fb = neg(b)? "-":"";
    if(neg(a)) a.pop_back();
    if(neg(b)) b.pop_back();
    if(a.size() < b.size()) swap(a,b), swap(fa,fb);
    if(a == "0" || b == "0") return Int("0");
    
    for(int j=0;j<(int)b.size();j++)
      for(int i=0;i<(int)a.size();i++){
        if(i+j >= (int)res.size()) res.push_back(0);
        res[i+j] += (a[i]-'0') * (b[j]-'0');
      }
    for(int i=0; i <(int)res.size(); i++) {
      if(res[i]/10 == 0) continue;
      if(i + 1 >= (int)res.size()) res.push_back(0);
      res[i+1] += res[i]/10, res[i]=res[i]%10;
    }
    string sres;
    for(int i:res) sres += char(i + '0');
    if(fa != fb) sres += '-';
    return Int(sres,1);
  }

  Int div(string b,int flg){
    assert(b != "0");
    string a = val;
    string fa = neg(a)? "-":"";
    string fb = b[0]=='-'? "-":"";
    if(neg(a)) a.pop_back();
    if(b[0]=='-') b.erase(b.begin());
    Int mod("0");
    string d="0";
    for(int i=(int)a.size()-1;i>=0;i--){
      mod = mod * 10 + (a[i] - '0');
      while(mod >= b) mod -= b, d.back()+=1;
      if(i && d != "0") d += '0'; 
    }
    if(flg == 0 && d != "0" && (fa != fb)) d = "-" + d;
    if(flg == 1 && mod != "0" && (fa == "-")) mod *= -1;
    return flg==0? d:mod;
  }
  
  Int operator + (Int b){R(b.val); return *this + b.val;}
  template<class T> Int operator + (T b){return *this + Int(b);}
  
  Int operator * (Int b){R(b.val); return *this * b.val;}
  template<class T> Int operator * (T b){return *this * Int(b);}

  Int operator - (string b){b = (b[0] == '-')? b.substr(1):"-" + b;return *this + b;}
  Int operator - (Int b){R(b.val); return *this - b.val;} 
  template<class T> Int operator - (T b){return *this - Int(b);}
  
  Int operator / (string b){return div(b,0);}
  Int operator / (Int b){R(b.val);return div(b.val,0);}
  template<class T> Int operator / (T b){return *this / Int(b);}
  
  Int operator % (string b){return div(b,1);}
  Int operator % (Int b){R(b.val);return div(b.val,1);}
  template<class T> Int operator % (T b){return *this % Int(b);}
  
  Int operator += (Int b){return *this = *this+b;}
  Int operator += (string b){return *this = *this+b;}
  template<class T> Int operator += (T b){return *this += Int(b);}
  
  Int operator -= (Int b){return *this = *this-b;}
  Int operator -= (string b){return *this = *this-b;}
  template<class T> Int operator -= (T b){return *this -= Int(b);}
  
  Int operator *= (Int b){return *this = *this*b;}
  Int operator *= (string b){return *this = *this*b;}
  template<class T> Int operator *= (T b){return *this *= Int(b);}
  
  Int operator /= (string b){return *this = *this/b;}
  Int operator /= (Int b){return *this = *this/b;}
  template<class T> Int operator /= (T b){return *this /= Int(b);}
  
  Int operator %= (string b){return *this = *this % b;}
  Int operator %= (Int b){return *this=*this%b;}
  template<class T> Int operator %= (T b){return *this %= Int(b);}
  
  Int operator ++(){return *this += 1;}
  Int operator --(){return *this -= 1;}

#ifdef int
#undef int
#define eraseIntDefine
#endif
  Int operator ++(int){
    Int tmp = *this;
    *this += 1;
    return tmp;
  }
  
  Int operator --(int){
    Int tmp = *this;
    *this -= 1;
    return tmp;
  }
#ifdef eraseIntDefine
#define int long long
#endif

  friend ostream& operator << (ostream& os,const Int a){
    for(int i=(int)a.val.size()-1;i>=0;i--) os<<a.val[i];
    return os;
  }

  friend istream& operator >> (istream& is,Int &a){
    string num;
    is>>num;
    a = Int(num);
    return is;
  }
  friend string to_string(Int a){reverse(a.val.begin(),a.val.end());return a.val;}
};


string str;
string num = "51?3";

Int mem[20][2][5][5][5];
int used[20][2][5][5][5];
Int dfs(int pos,int less,int cnt,string s,int target){
  //cout<<pr(pos,less,cnt,s,target)<<endl;
  if(pos == (int)str.size()) return cnt == target;
  if(used[pos][less][cnt][s.size()][target]++) return mem[pos][less][cnt][s.size()][target];

  int u = less? 9:str[pos]-'0';
  
  Int res = 0;
  for(int i=0;i<=u;i++){
    int npos = pos+1;
    int nless = !(!less && i == u);
    int ncnt = cnt;
    string ns = s;
    if(i + '0' == num[s.size()] || num[s.size()] == '?') ns += i+'0';
    else if(i + '0' == num[0]) ns.clear(),ns += i+'0';
    else ns = "";
    if(ns.size() == 4) ncnt++,ns.clear();
    res += dfs(npos,nless,ncnt,ns,target);
    
  }
  return mem[pos][less][cnt][s.size()][target] = res;
}

signed main(){
  cin>>str;
  Int sum[5]={};
  for(int i=1;i<5;i++) sum[i] += dfs(0,0,0,"",i);

  //for(int i=1;i<5;i++) cout<<pr(sum[i])<<endl;
  
  Int ans = 0;
  for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
  cout<<ans<<endl;
  
  return 0;
}



Compiler output:
a.cc:23:39: warning: use of 'auto' in template argument only available with '-fconcepts-ts'
   23 | ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
      |                                       ^~~~
a.cc:24:39: warning: use of 'auto' in template argument only available with '-fconcepts-ts'
   24 | istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
      |                                       ^~~~
a.cc:25:12: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   25 | void prArr(auto a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}
      |            ^~~~
a.cc: In function 'int main()':
a.cc:258:43: error: no match for 'operator*' (operand types are 'int' and 'Int')
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                               ~~~~~~~~~~~ ^ ~~~~~~
      |                                   |              |
      |                                   int            Int
In file included from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127,
                 from a.cc:1:
/usr/include/c++/14/complex:400:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const complex<_Tp>&)'
  400 |     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:400:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::complex<_Tp>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/complex:409:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const _Tp&)'
  409 |     operator*(const complex<_Tp>& __x, const _Tp& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:409:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::complex<_Tp>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/complex:418:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const complex<_Tp>&)'
  418 |     operator*(const _Tp& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:418:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   'Int' is not derived from 'const std::complex<_Tp>'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
In file included from /usr/include/c++/14/valarray:605,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const _Expr<_Dom2, typename _Dom2::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const typename _Dom::value_type&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   'Int' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const valarray<typename _Dom::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const valarray<typename _Dom::value_type>&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   'Int' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const valarray<_Tp>&, const valarray<_Tp>&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::valarray<_Tp>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_Constant, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const valarray<_Tp>&, const typename valarray<_Tp>::value_type&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   mismatched types 'const std::valarray<_Tp>' and 'int'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Constant, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const typename valarray<_Tp>::value_type&, const valarray<_Tp>&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:258:50: note:   'Int' is not derived from 'const std::valarray<_Tp>'
  258 |   for(int i=1;i<5;i++) ans += (i%2? 1:-1) * sum[i];
      |                                                  ^