[hdu 3652]数位dp解决数的倍数问题

原以为很好的理解了数位dp,结果遇到一个新的问题还是不会分析,真的是要多积累啊。

解决13的倍数,可以根据当前余数来推,所以把当前余数记为一个状态就可以了。

#include<bits/stdc++.h>
using namespace std;

int dp[20][13][2][10];
int b[20];

int dfs(int pos,int preok,int rem,int th,int pre)
{
    if (pos==-1)
    {
        if (rem==0&&th==1) return 1;
        else return 0;
    }
    if (preok&&dp[pos][rem][th][pre]!=-1) return dp[pos][rem][th][pre];
    int up=preok?9:b[pos];
    int ans=0;
    for (int i=0;i<=up;i++)
    {
        ans+=dfs(pos-1,i<b[pos]||preok,(rem*10+i)%13,pre==1&&i==3||th,i);
    }
    if (preok) dp[pos][rem][th][pre]=ans;
    return ans;
}

int solve(int n)
{
    int cnt=0;
    do {
        b[cnt++]=n%10;
        n/=10;
    }while (n);
    return dfs(cnt-1,0,0,0,0);
}

int main()
{
    memset(dp,-1,sizeof(dp));
    int n;
    while (~scanf("%d",&n))
    {
        printf("%d\n",solve(n));
    }
    return 0;
}
时间: 2024-08-06 03:39:38

[hdu 3652]数位dp解决数的倍数问题的相关文章

hdu 3652数位dp

/* 数位dp 题意:找到1-n之间包含13这个子串并且能够整除13的数 解:刚开始dp[N][N][2]这里的2用来记录是否为13表示当前位是否为13,我把上一位为1当前位为13和上一位部位1 这种情况在数组中没有记录. */ #include<stdio.h> #include<string.h> #define N 14 int dp[N][N][3]; int digit[N]; int dfs(int len,int mod,int cnt,int ok) { if(!l

HDU 3652 数位dp

B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5894    Accepted Submission(s): 3389 Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了.dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数. #include <bits/stdc++.h> using namespace std; typedef long

HDU 4352 数位dp

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3081    Accepted Submission(s): 1291 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefu