HDU X mod f(x) (数位DP)

题意:求一个区间内各位数字之和能被该数整除的个数。

析:数位DP,dp[i][j][k][l] 表示前 i 位和为 j,对 k 取模为 l,然后就好做了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;

}
int dp[12][85][85][85];
int a[12];

int dfs(int pos, int val, int x, int y, bool ok){
    if(!pos)  return y == 0 && val == x;
    int &ans = dp[pos][val][x][y];
    if(!ok && ans >= 0)  return ans;

    int res = 0, n = ok ? a[pos] : 9;
    for(int i = 0; i <= n; ++i)
        res += dfs(pos-1, val+i, x, (y*10+i)%x, ok && i == n);
    if(!ok)  ans = res;
    return res;
}

int solve(int n){
    int len = 0;
    int k = 0;
    while(n){
        a[++len] = n % 10;
        k += a[len];
        n /= 10;
    }

    int ans = 0;
    for(int i = 1; i < 82; ++i)
        ans += dfs(len, 0, i, 0, true);
    return ans;
}

int main(){
    int T;  cin >> T;
    memset(dp, -1, sizeof dp);
    for(int kase = 1; kase <= T; ++kase){
        scanf("%d %d", &m, &n);
        printf("Case %d: %d\n", kase, solve(n) - solve(m-1));
    }
    return 0;
}
时间: 2024-08-27 11:54:38

HDU X mod f(x) (数位DP)的相关文章

hdu 4389 X mod f(x) 数位dp

题链:http://acm.hdu.edu.cn/showproblem.php?pid=4389 X mod f(x) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2330    Accepted Submission(s): 919 Problem Description Here is a function f(x): int

HDU ACM 4734 F(x)-&gt;数位DP入门练习题

分析:数位DP的入门联系题,通过该題对数位DP有感觉了:dp[len][presum]代表长度为len权值不大于presum的数.记忆化搜索提高效率,除边界(因为边界上算出来的答案在其它数中可能是不完整的)外记录dp[len][presum]的值,下次发现以前计算过就直接return:dfs(len, presum, fg)表示求长度为len,不超过pre的全部符合条件的值.fg是控制边界的. #include<iostream> using namespace std; int dp[11]

[hdu 4933]Miaomiao&#39;s Function 数位DP+大数

Miaomiao's Function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 79    Accepted Submission(s): 18 Problem Description Firstly , Miaomiao define two functions f(x) , g(x): (K is the smallest

HDU 4352 XHXJ&#39;s LIS 数位DP + 状压

由LIS的nlogn解法 可以得出最后统计数组中数的个数即为LIS的长度 这样就可以状压了 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <c

hdu 3709 Balanced Number (数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1871    Accepted Submission(s): 836 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

HDU 3709 Balanced Number 枚举+数位DP

枚举支点之后数位DP,注意姿势 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list&g

HDU 4588 Count The Carries 数位DP || 打表找规律

2013年南京邀请赛的铜牌题...做的很是伤心,另外有两个不太好想到的地方....a 可以等于零,另外a到b的累加和比较大,大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数,然后统一进位. 设最低位为1,次低位为2,依次类推,ans[]表示这一位上有多少个1,那么有 sum += ans[i]/2,ans[i+1] += ans[i]/2; sum即为答案. 好了,现在问题转化成怎么求ans[]了. 打表查规律比较神奇,上图不说话. 打表的代码 #include <algo

HDU 4933 Miaomiao&#39;s Function(数位DP + 高精度)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4933 题意 : 自己看吧,还是很容易理解的,就一个公式而已. 方法 : 比赛的时候想到两次数位DP:先对所有的数进行一次mod9 的数位DP,再得出的答案x为新的mod,再进行一次mod,后来写着写着发现似乎有点问题,对于answer = 0要特判,然后没想到好的方法,比赛就结束了. 赛后用java写了一发大数过的,对于题解中用多个大质数去判这种神奇的作法只能意会下了 , 貌似rand() 也行

HDU.4352.XHXJ&#39;s LIS(数位DP 状压 LIS)

题目链接 数位DP. 至于怎么求LIS,因为只有10个数,所以可以参照O(nlogn)求LIS的方法,状压记录状态. 每次加一个数和求LIS一样更新状态.最后状态中1的个数就是LIS的长度. //93MS 3004K #include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #define gc() getchar() typedef long long LL; c