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(!len) {
    if(mod==0&&cnt==2)return 1;
    return 0;
 }
 if(!ok&&dp[len][mod][cnt]!=-1)
    return dp[len][mod][cnt];
 int ans=0,i,maxx=ok?digit[len]:9;
 for(i=0;i<=maxx;i++)  {
     if(cnt==2||(cnt==1&&i==3))
    ans+=dfs(len-1,(mod*10+i)%13,2,ok&&i==maxx);
     else
     if(i==1)//刚开始这里判断条件写成if(cnt==0&&i==1)这样是不对的因为漏掉了一种情况(i==1&&cnt==1)
        ans+=dfs(len-1,(mod*10+i)%13,1,ok&&i==maxx);
 else
      ans+=dfs(len-1,(mod*10+i)%13,0,ok&&i==maxx);
 }
 if(!ok)
    dp[len][mod][cnt]=ans;
 return ans;
}
int f(int n) {
 int len=0;
 while(n) {
     digit[++len]=n%10;
     n/=10;
 }
 return dfs(len,0,0,1);
}
int main() {
  int n;
  memset(dp,-1,sizeof(dp));
  while(scanf("%d",&n)!=EOF) {
    printf("%d\n",f(n));
  }
return 0;}
<pre name="code" class="cpp">/*
我原来的思路,刚开始写的时候少开了一维记录pre的情况
*/
#include<stdio.h>
#include<string.h>
#define N 14
int dp[N][N][2][2];
int digit[N];
int dfs(int len,int mod,int pre,int cnt,int ok) {
 if(!len) {
    if(mod==0&&cnt==1)return 1;
    return 0;
 }
 if(!ok&&dp[len][mod][pre][cnt]!=-1)
    return dp[len][mod][pre][cnt];
 int ans=0,i,maxx=ok?digit[len]:9;
 for(i=0;i<=maxx;i++)  {
        if(cnt||(pre&&i==3))
        ans+=dfs(len-1,(mod*10+i)%13,i==1,1,ok&&i==maxx);
        else
            ans+=dfs(len-1,(mod*10+i)%13,i==1,0,ok&&i==maxx);
 }
 if(!ok)
    dp[len][mod][pre][cnt]=ans;
 return ans;
}
int f(int n) {
 int len=0;
 while(n) {
     digit[++len]=n%10;
     n/=10;
 }
 return dfs(len,0,0,0,1);
}
int main() {
  int n;
  memset(dp,-1,sizeof(dp));
  while(scanf("%d",&n)!=EOF) {
    printf("%d\n",f(n));
  }
return 0;}

时间: 2024-12-15 01:33:29

hdu 3652数位dp的相关文章

[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==

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 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

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个条件之一,那么我们就说

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 5179 数位dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1