#include #include #include using namespace std; typedef unsigned long long LL; #define FOR(k,a,b) for(int k((int)a); k < ((int)b); ++k) #define REP(k,a) for(int k=0; k < ((int)a); ++k) int T,MOD, F[10], FCount; LL N; int step(int addValue) { if(F[addValue]==-1) { if(FCount == 0) return 1; if(FCount == 1) return 0; return FCount; } return F[addValue]; }; LL calc(int prMask, int len, int actRem) { //this function calculate for some P prefix //\sum_{i=0,i<10^len} F(P concenate i)%MOD //(actually we don't need P, just prMask because it is contains F(P) mask // which numbers are reserved, and actRem = F(P)%MOD //this function is the soul of the solution this cause we don't need to // calculate every F(i)%MOD actRem%=MOD; if(len == 0) return actRem; --len; if(!prMask) { return 9*calc(2,len,1); } if(prMask == 2) { return 9*calc(3,len,actRem*10)+calc(2,len,actRem*10+1); } LL res = 0; REP(i,10) { if(prMask&(1<-1) { while((pr+1)*tp<=N) { addValue = step(pr%10); ++pr; res += calc(prefixFMask|(1<