Artifact 54e51fc2420680302bbd07b7b6c24a8bcd5d417c
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
template<typename T>
struct DP2
{
const int N1, N2;
vector<T> data;
DP2(int N1, int N2, const T& t = T())
: N1(N1), N2(N2), data(N1*N2, t) {}
T& operator()(int i1, int i2)
{ return data[ (i1*N2)+i2 ]; }
void swap(DP2& rhs)
{ data.swap(rhs.data); }
};
class NumbersAndMatches { public:
long long differentNumbers(long long N, int K)
{
string s;
for(; N; N/=10)
s = char('0' + N%10) + s;
DP2<LL> dp(127,127);
dp(0,0) = 1;
for(int i=0; i<s.size(); ++i)
{
DP2<LL> dp2(127, 127);
for(int tooMany=0; tooMany<=K; ++tooMany)
for(int tooFew=0; tooFew<=K; ++tooFew)
for(char c='0'; c<='9'; ++c)
{
int tM=tooMany, tF=tooFew;
diff(s[i], c, &tM, &tF);
if( tM<=K && tF<=K )
dp2(tM,tF) += dp(tooMany,tooFew);
}
dp.swap(dp2);
}
LL ans = 0;
for(int tMF=0; tMF<=K; ++tMF)
ans += dp(tMF, tMF);
return ans;
}
void diff(char a, char b, int* tF, int* tM)
{
const char* a7 = seven_seg(a);
const char* b7 = seven_seg(b);
for(int i=0; i<7; ++i)
if( a7[i] < b7[i] )
++*tF;
else if( a7[i] > b7[i] )
++*tM;
}
const char* seven_seg(char c)
{
const char* ans[] = {
"1110111",
"0010011",
"1011101",
"1011011",
"0111010",
"1101011",
"1101111",
"1010010",
"1111111",
"1111011",
};
return ans[c-'0'];
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, NumbersAndMatches().differentNumbers(N, K));}
int main(){
CASE(0)
long long N = 10LL;
int K = 1;
long long _ = 4LL;
END
CASE(1)
long long N = 23LL;
int K = 1;
long long _ = 4LL;
END
CASE(2)
long long N = 66LL;
int K = 2;
long long _ = 15LL;
END
CASE(3)
long long N = 888888888LL;
int K = 100;
long long _ = 1LL;
END
CASE(4)
long long N = 444444444444444444LL;
int K = 2;
long long _ = 1LL;
END
CASE(5)
long long N = 1LL;
int K = 1;
long long _ = 2LL;
END
CASE(6)
long long N = 12345678901234567LL;
int K = 126;
long long _ = -1LL;
END
}
// END CUT HERE