【HDOJ 3652】B-number

【HDOJ 3652】B-number

给一整数n 找<=n的整数中能被13整除且含有13的 数位dp 记忆化!

一入记忆化深似海。

。。再也不想用递推了。。。发现真的非常好想 仅仅要保证满足条件把未选高位(即能任意挑数的)记录下来 不断搜索递归即可

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int dp[10][13][3];
int digit[10];

/*
hs =
0 无13
1 无13 前位为1
2 出现13
mod表示高位取余后的结果 遍历完后mod == 0说名能被13整除 按位取余
*/

int dfs(int pos,int mod,int hs,bool high)
{
    if(pos == -1) return hs == 2 && !mod;
    if(!high && ~dp[pos][mod][hs]) return dp[pos][mod][hs];

    int i,en,ans = 0,nhs,nmd;
    en = high? digit[pos]: 9;

    for(i = 0; i <= en; ++i)
    {
        nmd = (mod*10+i)%13;
        nhs = hs;
        if(nhs == 1 && i == 3) nhs = 2;
        else if(nhs != 2) nhs = (i == 1)? 1: 0;
        ans += dfs(pos-1,nmd,nhs,high && i == en);
    }

    if(!high) dp[pos][mod][hs] = ans;
    return ans;
}

int Solve(int x)
{
    int len = 0;
    while(x)
    {
        digit[len++] = x%10;
        x /= 10;
    }
    return dfs(len-1,0,0,1);
}

int main()
{
    memset(dp,-1,sizeof(dp));
    int n;
    while(~scanf("%d",&n)) printf("%d\n",Solve(n));
    return 0;
}
时间: 2024-10-11 03:53:10

【HDOJ 3652】B-number的相关文章

【HDOJ 4763】 Theme Section (KMP+strstr)

[HDOJ 4763] Theme Section Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1999    Accepted Submission(s): 947 Problem Description It's time for music! A lot of popular musicians a

【HDOJ 4768】 Flyer (等差数列+二分)

[HDOJ 4768] Flyer (等差数列+二分) Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2022    Accepted Submission(s): 743 Problem Description The new semester begins! Different kinds of student soc

【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon candy Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 233    Accepted Submission(s): 61 Problem Des

【HDOJ 5838】Mountain(局部极小值)

[HDOJ 5838]Mountain(局部极小值) Mountain Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Zhu found a map which is a N * M rectangular grid.Each cell has a height and there are no two cells which have

【HDOJ 5653】 Bomber Man wants to bomb an Array.(DP)

[HDOJ 5653] Bomber Man wants to bomb an Array.(DP) Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 389    Accepted Submission(s): 117 Problem Description Give

【HDOJ 5652】 India and China Origins(并查集)

[HDOJ 5652] India and China Origins(并查集) India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 805    Accepted Submission(s): 272 Problem Description A long time ago there are

【HDOJ 4764】 Stone (博弈)

[HDOJ 4764] Stone (博弈) Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1090    Accepted Submission(s): 761 Problem Description Tang and Jiang are good friends. To decide whose treat it is

【HDOJ 5379】 Mahjong tree

[HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根结点1可以标1或n 否则其他情况无法让下面的子树满足各自连续并且该根的儿子节点都要连续 根结点下的节点平分其他标号 画一画可以发现 每个根下最多有两颗子树 否则无法满足条件 并且两颗子树占据剩余标号的左右两边 中间夹的必须是叶子 这样才能满足该根下的儿子节点标号连续 若根下只有一颗子树 同样可以选择占剩

【HDOJ 5384】Danganronpa

[HDOJ 5384]Danganronpa AC自动机...当时感觉用字典树 标神也往自动机想来着..手太生加上时间紧迫也没敲--回来一看题解什么AB同时建自动机...顿时愣了 什么叫同时建= =问了问财神说普通自动机...B串单建 立马疯了--这不就是模板题么... B串建自动机 A串枚举查询 写完兴冲冲1T--立马想法优化 建fail时压缩一下 查询时直接累计 不再循环找fail 171ms...第二个自动机的题..距上次蛮久了 这次一复习 感觉印象差不多有了 代码(模板)如下: #inc