Artifact c255d7f6a0dc716709e9da0d4917b7b00c7902af
#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;
// mod 演算用ユーティリティ
LL MODVAL = 1000000007LL;
LL ADD(LL x, LL y) { return (x+y)%MODVAL; }
LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; }
LL MUL(LL x, LL y) { return (x*y)%MODVAL; }
LL POW(LL x, LL e) {
LL v = 1;
for(;e;x=MUL(x,x),e>>=1)
if(e&1)
v = MUL(v, x);
return v;
}
LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); }
LL C(LL n, LL k) {
LL v = 1;
k = min(k, n-k);
for(LL i=1; i<=k; ++i)
v = DIV(MUL(v, n-i+1), i);
return v;
}
// 本体
class IncreasingNumber { public:
int countNumbers(long long digits, int divisor)
{
LL N = digits;
int D = divisor;
// ちょうど N 桁の Increasing number
//=
// 0<=Σa<=8 な a[] を使って
// 1*a[0] + 11*a[1] + ... + 111..111*(a[N-1]+1)
// の形で書ける数
//
//なので
//
// D で割れるちょうど N 桁の Increasing number の個数
//=
// 0<=Σa<=8 な a[] で
// coeff[0]*a[0] + coeff[1]*a[1] + ... + coeff[N-1]*(a[N-1]+1) = 0
// なものの個数 ( where coeff[i] := "1がi+1個並んだもの" % D )
// ステージ1
vector<LL> nc(D); // nc[m] = (coeff[i]%D==m な i の数)
int ceLast; // coeff[N-1]
{
// coeff は高々 D 回でループしてるはず
vector<int> coeff;
int loop_start = -1;
for(int v=1%D ;;)
{
loop_start = find(coeff.begin(), coeff.end(), v) - coeff.begin();
if( loop_start != coeff.size() )
break;
coeff.push_back( v );
v = (v*10 + 1) % D;
}
int loop_len = coeff.size() - loop_start;
for(int i=0; i<coeff.size() && i<N; ++i)
if( i < loop_start )
nc[coeff[i]] = 1;
else
nc[coeff[i]] = (N-i+loop_len-1)/loop_len % MODVAL;
ceLast = N-1 < loop_start ? coeff[N-1]
: coeff[(N-1-loop_start)%loop_len+loop_start];
}
// ステージ1.5
vector< vector<LL> > choice(D, vector<LL>(9));
for(int m=0; m<D; ++m)
for(int n=0; n<=8; ++n)
// nc[m] 種類のものから n 個取る取り方
choice[m][n] = (n==0 ? 1 : nc[m]==0 ? 0 : C(n+nc[m]-1, nc[m]-1));
// ステージ2
// dp[m][n][q] = 余り [0, 1, ... m] 係数から n 個使って総和を余り q にするやり方は何通り?
vector< vector< vector<LL> > > dp(D, vector< vector<LL> >(9, vector<LL>(D)));
for(int m=0; m<D; ++m)
dp[m][0][ceLast] = 1;
for(int n=1; n<=8; ++n)
dp[0][n][ceLast] = choice[0][n];
for(int m=1; m<D; ++m)
for(int n=1; n<=8; ++n)
for(int q=0; q<D; ++q)
for(int k=0,q2=q; k<=n; ++k,q2=(q2-m+D)%D)
dp[m][n][q] = ADD( dp[m][n][q], MUL( dp[m-1][n-k][q2], choice[m][k] ) );
LL ans = 0;
for(int n=0; n<=8; ++n)
ans = ADD(ans, dp[D-1][n][0]);
return ans;
}
};
// 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 int& Expected, const int& 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(_, IncreasingNumber().countNumbers(digits, divisor));}
int main(){
CASE(-2)
long long digits = 1LL;
int divisor = 16;
int _ = 0;
END
CASE(-1)
long long digits = 1LL;
int divisor = 1;
int _ = 9;
END
CASE(0)
long long digits = 2LL;
int divisor = 12;
int _ = 4;
END
CASE(1)
long long digits = 3LL;
int divisor = 111;
int _ = 9;
END
CASE(2)
long long digits = 452LL;
int divisor = 10;
int _ = 0;
END
CASE(3)
long long digits = 6LL;
int divisor = 58;
int _ = 38;
END
CASE(4)
long long digits = 1000000000000000000LL;
int divisor = 500;
int _ = 0;
END
CASE(5)
long long digits = 26542766498659LL;
int divisor = 25;
int _ = 766312864;
END
}
// END CUT HERE