private long sum(int d, long n, int i, int overflow, int mul) {
        
        if (i < 0) return (overflow == mul) ? n : 0;

        long sum = 0;
        
        for (int ni = (n == 0 ? 1 : 0); ni < 10; ++ni) {

            final int fi = ni * (10 - i - mul) + (10 * overflow) - (ni > d ? 10 : 0);
            
            if (-10 < fi && fi < 100) {
                sum += sum(d, n * 10 + ni, i - 1, fi, ni == d ? mul + 10 : mul);
            }
        }
        
        return sum;
    }