UESTC Windy number (digital DP)

Source: Internet
Author: User

Test instructions: A positive integer that does not contain a leading 0 and has a difference of at least 2 of the adjacent two digits is called the windy number.

Windy would like to know the total number of windy between A and B, including a and B?

IDEA: Digital DP

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;intdp[ the][Ten];//Dp[i][j] Indicates the number of windy with a length of I and a maximum of Jvoidinit () {memset (DP,0,sizeof(DP)); inti,j,k;  for(j=0;j<Ten; ++j) dp[1][j]=1;  for(i=2; i<=Ten;++i) for(j=0; j<=9;++j) {             for(k=0; k<=j-2; ++k) dp[i][j]+=dp[i-1][k];  for(k=j+2; k<=9; ++k) dp[i][j]+=dp[i-1][k]; }}intbit[ the];intCalcintN) {    if(n==0)return 0; intlen=0, I,j;  while(n) {bit[++len]=n%Ten; N/=Ten; } Bit[len+1]=-Ten; BOOLflag=true; intans=0;  for(i=1; i<len;++i)//first count the length less than Len         for(j=1; j<=9;++j) Ans+=Dp[i][j];  for(j=1; j<bit[len];++j) Ans+=dp[len][j];//Highest bit     for(i=len-1; i>=1;--i) {         for(j=0; j<bit[i];++j)if(ABS (bit[i+1]-J) >=2) ans+=Dp[i][j]; if(ABS (bit[i+1]-bit[i]) <2) {flag=false;  Break; }    }    if(flag) ++ans;//Plus n itself    returnans;}intMain () {init (); intb;  while(~SCANF ("%d%d",&a,&b)) {printf ("%d\n", Calc (b)-calc (A-1)); }    return 0;}
View Code

UESTC Windy number (digital DP)

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.