[DP] SRM 452 Div1 Hard IncreasingNumber

Solution S o l u t i o n

刚开始自己脑补了一个 O((123)m2logn) O ( ( 12 3 ) m 2 log ⁡ n ) 的做法。。大概就是 dpi,lo,hi,j d p i , l o , h i , j 表示考虑 i i 位,以 lo l o 开头以 hi h i 结尾,模 m m j j 的方案数。然后倍增DP。然而过不去。。。。。。。。。。。。。。。

可以发现对于一个数 i,1i<10 i , 1 ≤ i < 10 只要选 10ai19 10 a i − 1 9 这样的东西就好了。
这个 1111....111 1111....111 m m 可能是有循环节的??我是倍增DP出剩余系每个数的个数。
然后背包就好了。
要注意的是最长的那个 1111.111 1111.111 是一定要选的。
爆int+1

// BEGIN CUT HERE

// END CUT HERE
#line 5 "IncreasingNumber.cpp"
#include 
using namespace std;

typedef long long ll;
const int N = 555;
const int MOD = 1000000007;
typedef pair<int, int> pairs;

ll n;
int m, ans, tmp;
int dp[N][N][10];
int f[N], f1[N];
int val[N][10];
int inv[N];

class IncreasingNumber {  
public:

    inline void pre(int n) {
        inv[1] = 1;
        for (int i = 2; i <= n; i++)
            inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
    }
    inline int C(ll n, int m) {
        int res = 1; n %= MOD;
        for (int i = 0; i < m; i++) {
            res = (n - i) * res % MOD;
            res = (ll)res * inv[i + 1] % MOD;
        }
        return (res + MOD) % MOD;
    }
    inline void add(int &x, int a) {
        x += a; while (x >= MOD) x -= MOD;
    }
    inline pairs solve(ll n) {
        if (n == 0) return pairs(0, 0);
        if (n == 1) {
            f[1 % m] = 1;
            return pairs(1 % m, 1 % m);
        }
        if (n & 1) {
            pairs p = solve(n - 1);
            int base = p.first * 10 % m, _base = (p.second * 10 + 1) % m;
            add(f[_base], 1);
            return pairs(base, _base);
        } else {
            pairs p = solve(n / 2);
            int base = p.first * 10, _base = p.second;
            for (int i = 0; i < m; i++) f1[i] = f[i];
            for (int i = 0; i < m; i++)
                add(f[(i * base + _base) % m], f1[i]);
            return pairs(base * base / 10 % m, _base * (base + 1) % m);
        }
    }

    int countNumbers(long long digits, int divisor) {
        n = digits; m = divisor; pre(10);
        tmp = (solve(n - 1).second * 10 + 1) % m;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < 10; j++)
                val[i][j] = C(f[i] + j - 1, j);
        dp[0][0][0] = 1;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++)
                for (int k = 0; k < 10; k++)
                    for (int x = 0; x + k < 10; x++)
                        add(dp[i + 1][(j + i * x) % m][k + x], (ll)val[i][x] * dp[i][j][k] % MOD);
        for (int i = 1; i < 10; i++)
            for (int j = 0; j + i < 10; j++)
                add(ans, dp[m][(m - tmp * i % m) % m][j]);
        return ans;
    }

    // BEGIN CUT HERE
public:
    void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
    template <typename T> string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
    void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
    void test_case_0() { long long Arg0 = 2LL; int Arg1 = 12; int Arg2 = 4; verify_case(0, Arg2, countNumbers(Arg0, Arg1)); }
    void test_case_1() { long long Arg0 = 3LL; int Arg1 = 111; int Arg2 = 9; verify_case(1, Arg2, countNumbers(Arg0, Arg1)); }
    void test_case_2() { long long Arg0 = 452LL; int Arg1 = 10; int Arg2 = 0; verify_case(2, Arg2, countNumbers(Arg0, Arg1)); }
    void test_case_3() { long long Arg0 = 6LL; int Arg1 = 58; int Arg2 = 38; verify_case(3, Arg2, countNumbers(Arg0, Arg1)); }

    // END CUT HERE

};

// BEGIN CUT HERE
int main(void) {
    IncreasingNumber ___test;
    ___test.run_test(-1);
    system("pause");
}
// END CUT HERE

你可能感兴趣的:(动态规划)