Topic links
problem 2109 Mountain numberaccept:139 submit:357
Time limit:1000 mSec Memory limit:32768 KB problem Description
One integer number x is called "Mountain number" if:
(1) x>0 and x is an integer;
(2) Assume x=a[0]a[1]...a[len-2]a[len-1] (0≤a[i]≤9, a[0] is positive). Any a[2i+1] was larger or equal to a[2i] and a[2i+2] (if exists).
For example, 111, 893, 7 is "Mountain number" while 123, and 76889 is not "Mountain number".
Now is given L and R, how many "Mountain number" can be found between L and R (inclusive)?
Input
The first line of the input contains an integer T (t≤100), indicating the number of the test cases.
Then T-cases, for any case, only integers L and R (1≤l≤r≤1,000,000,000).
Outputfor each test case, output the number of "Mountain number" between L and R. Sample INPUT31 101 1001-Output954384
This question asks to count the total number of "l,r" this interval Uchiyama. The number of mountains is the number of odd digits not less than the left and right sides of the number such as 1919191 45454 1111 These three are mountain numbers
Wrote a function san (int) to calculate the 0-n
#include <stdio.h> #include <cstdlib> #include <cstring> #include <climits> #include <cctype > #include <cmath> #include <string.h> #include <sstream> #include <iostream> #include < Algorithm> #include <iomanip>using namespace std; #include <queue> #include <stack> #include < vector> #include <deque> #include <set> #include <map>int dp[15][15];//two-D respectively represents the number of digits and digit digit= 10 represents this bit on the largest number int san (int n) {if (n==0) return 1;int tem=n;int l=0;int ji[15];for (int i=0;tem;i++) {ji[l++]=tem%10;tem/=10;} The for (int i=0;i<l/2;i++)//ji array is stored in Digit{swap (Ji[i],ji[l-1-i]) on each bit;} if (l==1) return Ji[0]+1;memset (dp,0,sizeof DP); for (int i=1;i<ji[0];i++) {dp[0][i]=1;} Dp[0][10]=1;int jo=1;for (int i=1;i<l;i++) {if (ji[i]>=ji[i-1]&&jo==1)//The maximum number of previous maximum transfers current bit dp[i][10]=dp[ I-1][10];else if (ji[i]<=ji[i-1]&&jo==0) dp[i][10]=dp[i-1][10];if (jo==0)//Previous maximum number transfer current bit other digits for (int j=0;j <=ji[i-1]&&j<ji[i];j++) {dp[i][j]+=DP[I-1][10];} if (jo==1) for (int j=ji[i-1];j<ji[i];j++) {dp[i][j]+=dp[i-1][10];} for (int j=0;j<=9;j++)//previous digit transfer current bit other number {if (jo==1) for (int k=0;k<=j;k++) {dp[i][j]+=dp[i-1][k];} if (jo==0) for (int k=j;k<=9;k++) {dp[i][j]+=dp[i-1][k];}} Jo^=1;} int ans=0;for (int i=0;i<=10;i++) {ans+=dp[l-1][i];} int zong=0;l--;//Because the preamble 0 is not counted so it is assumed that the remaining 0-99999while (l--) {zong*=10;zong+=9;} Return Ans+san (zong);} int main () {int t,n,l,r;scanf ("%d", &t), while (t--) {//scanf ("%d", &l),//printf ("%d\n", san (L)), scanf ("%d%d", &L,&R); printf ("%d\n", San (R)-san (L-1));} return 0;}
FOJ 2109 Mountain number digits DP