zoukankan      html  css  js  c++  java
  • LuoGuP4127:[AHOI2009]同类分布

    Pre

    前前后后交了 (4) 次。

    Solution

    直接枚举模数,就是每一个数的各位数的和,然后 (dp) 到了最后判断和是否相等。

    学习了记忆化搜索实现的数位 (dp) ,发现挺好用的。

    Code

    #include <cstdio>
    #include <cstring>
    #define ll long long
    using namespace std;
    const ll N = 20, M = 200;
    ll dp[N][M][M], a[N], len, mod;
    inline ll dfs (int p, int limit, int st, int pre) {
    	if (!p) {
    		if (st == mod && !pre) return 1;
    		else return 0;
    	}
    	if (!limit && dp[p][st][pre] != -1) return dp[p][st][pre];
    	ll res = 0;
    	for (int i = 0; i <= (limit ? a[p] : 9); ++i) {
    		res += dfs (p - 1, limit && (i == a[p]), st + i, (pre * 10 + i) % mod);
    	}
    	if (!limit)
    		dp[p][st][pre] = res;
    	return res;
    }
    inline ll solve (ll lhjakioi) {
    	if (!lhjakioi) return 0;
    	len = 0;
    	while (lhjakioi) {
    		a[++len] = lhjakioi % 10;
    		lhjakioi /= 10;
    	}
    	ll ans = 0;
    	for (mod = 1; mod <= 162; ++mod) {
    		memset (dp, -1, sizeof (dp));
    		ans += dfs (len, 1, 0, 0);
    	}
    	return ans;
    }
    signed main () {
    	#ifdef chitongz
    	freopen ("x.in", "r", stdin);
    	#endif
    	ll a, b;
    	scanf ("%lld%lld", &a, &b);
    	printf ("%lld
    ", solve (b) - solve (a - 1));
    	return 0;
    }
    

    Conclusion

    作为记忆化搜索实现数位 (dp) 的第一题, (WA) 了三次。

    第一次,第二次是 (dp) 的状态设计错误,没有记录当前的数模枚举的 (mod) 的值。

    第三次是没有 (dfs) 函数没有返回 (long long)

  • 相关阅读:

    字符串比较
    String对象的简单方法(特别讲解length()方法的实现。
    Character类
    线性结构应用实例:多项式加法运算
    队列的顺序和链式存储实现
    堆栈用数组和用链表实现
    广义表和多重链表(十字链表)
    powerDesigner的name和comment转化
    jquery-validate
  • 原文地址:https://www.cnblogs.com/ChiTongZ/p/11355886.html
Copyright © 2011-2022 走看看