2017 Guangdong University of Technology Program design Competition final Problem G: equal concave number (Palindrome + digital DP) __ palindrome number bit DP

Source: Internet
Author: User

Problem G: Concave digits
Description
Define a number called a concave number, that is, from high to position, the number of each digit first does not increment again and again is not descending, and the number is a palindrome number, that is, from left to right and read from right to left is the same, only to form a concave peak, such as 543212345,5544334455 is a valid concave number , 543212346,123321,111111 is not a concave number. Now ask you [l,r] how many concave figures there are.

Input
The first line is an integer t that represents the number of groups of data.
Next T line each row two numbers L and R, (1<=L<=R<=1E18)
Output
Output An integer representing the number of concave digits in [l,r]
Sample Input
2
1 100
101 200
Sample Output
0
1
HINT

Numbers less than or equal to 2 digits without concave peaks

Set back to the digital DP template, plus 4 dimensions (ascent, descent, leading 0, the first number) looked at the other people to solve ideas, know how to open these several dimensions before writing out Qaq
The code has its own annotations.
AC Code

#include <stdio.h> #include <string.h> int degth[20];
int temp[20];
Long Long dp[20][20][10][3][3][3]; Long Long dfs (int pos,int len,int pre,int up,int down,int flag,int f,int k)//length, current bit, previous digit, ascending, descending, palindrome {if (len<0) retur N up&&down&&flag; Rise plus drop Plus is palindrome if (!f&&dp[pos][len][pre][up][down][flag]!=-1&&!k) return dp[pos][len][pre][up][down][
    Flag];
    int max=f?degth[len]:9;
    Long Long ret=0;
        for (int i=0;i<=max;i++) {temp[len]=i; if (k)//leading 0 o'clock directly reduces the length, otherwise executes the next bit (because the starting bit cannot be judged up and down) Ret+=dfs (pos-(k&&i==0), LEN-1,I,0,0,FLAG,F&AMP;&AMP;I==MAX,K&A
        mp;&i==0); else if (pre==i) {if (flag&& (pos+1)/2>len) Ret+=dfs (pos,len-1,i,up,down,i==t 
            emp[pos-len],f&&i==max,k&&i==0);
        else Ret+=dfs (pos,len-1,i,up,down,flag,f&&i==max,k&&i==0); else if (pre<i)//increments {if (!down) continue;/No Drop on Rise if (flag&& (pos+1)/2>len) Ret+=dfs (pos,len-1,i,1,down,i==temp[pos-len],f&amp
            ; &i==max,k&&i==0);
        else Ret+=dfs (pos,len-1,i,1,down,flag,f&&i==max,k&&i==0); The else if (pre>i)//decrement {if (UP) continue;//has risen over if (flag&& (pos+1)/2>le
            N) Ret+=dfs (pos,len-1,i,up,1,i==temp[pos-len],f&&i==max,k&&i==0);
        else Ret+=dfs (pos,len-1,i,up,1,flag,f&&i==max,k&&i==0);
    } if (!f&&!k) Dp[pos][len][pre][up][down][flag]=ret;
return ret;
    Long long solve (long long x) {int len=0;
        while (x) {degth[len++]=x%10;
    x/=10;
Return Dfs (len-1,len-1,0,0,0,1,1,1);
    int main () {memset (dp,-1,sizeof (DP));
    int T;
    scanf ("%d", &t);
        while (t--) {Long long l,r;
        scanf ("%lld%lld", &l,&r); printf ("%lld\n", Solve (R)-Solve (L-1)); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.