hdu3652 数位dp

dp[i][j][k]  i-i位数,j-开头是j,k-除13的余数

要注意数位dp的时候一定不能直接判断对的来做,必须是判断不对的,然后用全部减

这一题先算出所有%13==0的个数,然后算出所以%13并且不含13的个数,然后相减就是答案了

#include<bits/stdc++.h>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 2520
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-7;
const int N=10+10,maxn=1000000+10,inf=0x3f3f3f;

int dp1[N][10][20],dp2[N][10][20],digit[N];
int dfs1(int len,int before,int left,bool fp)
{
    if(!len)return left==0;
    if(!fp&&dp1[len][before][left]!=-1)return dp1[len][before][left];
    int ans=0,fpmax=fp ? digit[len] : 9;
    for(int i=0;i<=fpmax;i++)
    {
        if(i==3&&before==1)continue;
        ans+=dfs1(len-1,i,(left*10+i)%13,fp&&i==fpmax);
    }
    if(!fp)dp1[len][before][left]=ans;
    return ans;
}
int dfs2(int len,int before,int left,bool fp)
{
    if(!len)return left==0;
    if(!fp&&dp2[len][before][left]!=-1)return dp2[len][before][left];
    int ans=0,fpmax=fp ? digit[len] : 9;
    for(int i=0;i<=fpmax;i++)
    {
        ans+=dfs2(len-1,i,(left*10+i)%13,fp&&i==fpmax);
    }
    if(!fp)dp2[len][before][left]=ans;
    return ans;
}
int solve(int x)
{
    int len=0;
    while(x)
    {
        digit[++len]=x%10;
        x/=10;
    }
    return dfs2(len,0,0,1)-dfs1(len,0,0,1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    memset(dp1,-1,sizeof dp1);
    memset(dp2,-1,sizeof dp2);
    while(cin>>n)cout<<solve(n)<<endl;
    return 0;
}
/********************
dp[i][j][k]  i-i位数,j-开头是j,k-除13的余数
********************/

时间: 2024-10-11 05:53:55

hdu3652 数位dp的相关文章

hdu3652 数位dp(含13且被能被13整除的数)

http://acm.hdu.edu.cn/showproblem.php?pid=3652 B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2815    Accepted Submission(s): 1552 Problem Description A wqb-number, or B-number for sh

hdu3652 数位dp记忆化搜索

从未见过的船新版本数位dp,,省去了预处理过程,直接进行计算 #include<bits/stdc++.h> using namespace std; #define ll long long int dp[20][20][5],n,len,bit[20]; //dp[i][j][k]:到i位数,前面模13=j,前面是1|不是1|有13的状态已经确定,后面的数的个数 //mod:前面%13的余数,flag=0|1|2:pos+1位不是1|是1|有13出现过了,lim:数的限制 int dfs(

数位DP HDU3652

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

hdu3652(数位dp)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:求1~n含有13且能被13整除的数的个数. 分析:数位dp,dp数组加一维来维护到pos位模13的余数,则dp[pos][mod][2]表示非限制条件下到pos位模13余mod且已含有13的总个数,dp[pos][mod][1]表示没含有13但前一位是1且模13余mod的总个数,dp[pos][mod][0]表示没含有13前一位不为1模13余mod的总个数... #include <cs

[暑假集训--数位dp]hdu3652 B-number

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb

【hdu3652】B-number 数位DP

B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task

hdu3652 B-number(数位dp+dfs)

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

hdu3652---B-number(数位dp)

Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to ca

数位dp专题 (HDU 4352 3652 3709 4507 CodeForces 55D POJ 3252)

数位dp核心在于状态描述,因为阶段很简单. 一般都是求有多少个数,当然也有求平方的变态题. 因为比这个数小的范围定然是从左至右开始小的,那么什么样的前缀对后面子数有相同的结果? HDU 3652 题意:求能被13整除且含有13这样数的个数. 赤裸裸的两个条件,加上个pre标明下前缀,其实直接开状态也是一样的.整除这个条件可以用余数来表示.余数转移:(mod*10+i)%13 /* *********************************************** Author :bi