B-number——数位DP

  这一题我个人认为是比较难的了。

  暴力去拿部分分也是可以的。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        int ans=0;
        for(int i=13;i<=n;i+=13){
            int j=i,x,y=-1;
            while(j){
                x=j%10;j/=10;
                if(x==1&&y==3){ans++;break;}
                y=x;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
//超级慢 

  然而这样子绝对是不好的,打表都比这好。

  不过还是正解最好了。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int f[15][10][2][13];
int md[15];//md用于保存10^n
void init(){//初始化
    md[0]=1;
    for(int i=1;i<=13;i++)
        md[i]=(md[i-1]*10)%13;
    memset(f,0,sizeof(f));
    f[0][0][0][0]=1;//切记是f[0][0][0][0]而不是f[0][0][1][0]
    //因为一开始没有包含13
    for(int i=1;i<=13;i++)
    for(int j=0;j<=9;j++)
    for(int k=0;k<=9;k++)
    for(int l=0;l<13;l++){
        int val=(j*md[i-1])%13;
        f[i][j][1][l]+=
            f[i-1][k][1][(13+l-val)%13];
        if(j==1&&k==3) f[i][j][1][l]+=
            f[i-1][k][0][(13+l-val)%13];
        else f[i][j][0][l]+=
            f[i-1][k][0][(13+l-val)%13];
        //这里用(13+l-val)因为有可能l<val
    }
}
int solve(int n){
    int digit[15];//从低到高保存n的每一位
    int len=0,ans=0,mod=0;
    int t=0;//t保存是否在高位出现过13
    while(n!=0){//统计n的位数
        digit[++len]=n%10;n/=10;
    }
    digit[len+1]=0;//方便处理
    for(int i=len;i>=1;i--){//枚举当前位数
        for(int j=0;j<digit[i];j++){
            ans+=f[i][j][1][(13-(mod*md[i]%13))%13];
            if(t||(j==3&&digit[i+1]==1))
                ans+=f[i][j][0][(13-(mod*md[i]%13))%13];
        }
        if(digit[i]==3&&digit[i+1]==1)t=1;//如果高位出现过13,t=1
        mod=(mod*10+digit[i])%13;//更改mod
    }
    return ans;
}
int main()
{
    int n;
    init();
    while(scanf("%d",&n)!=EOF){
        printf("%d\n",solve(n+1));
        //因为是solve是计算区间[0,n)的,所以要n+1
    }
    return 0;
}

  本文由Yzyet编写,网址为www.cnblogs.com/Yzyet。非Yzyet同意,禁止转载,侵权者必究。

时间: 2024-10-24 01:46:35

B-number——数位DP的相关文章

Hdu3079Balanced Number数位dp

枚举支点,然后就搞,记录之前的点的力矩和. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <s

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

多校5 HDU5787 K-wolf Number 数位DP

1 // 多校5 HDU5787 K-wolf Number 数位DP 2 // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d 3 // f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小 4 // 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位 5 // http://blog.csdn.net/weizhuwyzc000/article/details/5209769

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

codeforces Hill Number 数位dp

http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl

fzu 2109 Mountain Number 数位DP

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2109 题意: 如果一个>0的整数x,满足a[2*i+1] >= a[2*i]和a[2*i+2],则这个数为Mountain Number. 给出L, R,求区间[L, R]有多少个Mountain Number. 思路: 数位DP,判断当前是偶数位还是奇数位(从0开始),如果是偶数位,那么它要比前一个数的值小, 如果是奇数位,那么它要比前一个数的值大. 1 #include <iostream>

hdu3709---Balanced Number(数位dp)

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. When a pivot is placed at some digit of the nu

HDU 3709 Balanced Number (数位DP)

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

hdu 5898 odd-even number(数位dp)

Problem Description For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<= 9*10^18). Input Fir