[暑假集训--数位dp]hdu5898 odd-even number

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

First line a t,then t cases.every line contains two integers L and R.

Output

Print the output for each case on one line in the format as shown below.

Sample Input

2
1 100
110 220

Sample Output

Case #1: 29
Case #2: 36

题意是要找x,x当中所有连续的奇数段长度是偶数,连续的偶数段长度是奇数,比如1357246

记一下当前这段是奇数还是偶数,这段已经有多长,有没有前导零。如果有前导零那么0可以也出现奇数次

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<LL,LL>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
22     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
23     return x*f;
24 }
25 int len;
26 LL l,r;
27 LL f[20][10][20][2];
28 int d[110];
29 inline LL dfs(int now,int dat,int lst,int lead,int fp)
30 {
31     if (now==1)return (lst&1)^(dat&1);
32     if (!fp&&f[now][dat][lst][lead]!=-1)return f[now][dat][lst][lead];
33     LL ans=0;
34     int mx=fp?d[now-1]:9;
35     for (int i=0;i<=mx;i++)
36     {
37         if (i==0)
38         {
39             if (lead)
40             {
41                 if (lead&&now-1!=1)ans+=dfs(now-1,0,0,1,fp&&mx==0);
42                 else if (lead&&now-1==1)ans+=dfs(now-1,0,1,0,fp&&mx==0);
43                 continue;
44             }
45         }
46         if ((i&1)==(dat&1))ans+=dfs(now-1,i,lst+1,0,fp&&i==mx);
47         else if ((dat&1)^(lst&1)||lead)
48         {
49             ans+=dfs(now-1,i,1,0,fp&&i==mx);
50         }
51     }
52     if (!fp)f[now][dat][lst][lead]=ans;
53     return ans;
54 }
55 inline LL calc(LL x)
56 {
57     if (x==-1)return 0;
58     if (x==0)return 1;
59     LL xxx=x;
60     len=0;
61     while (xxx)
62     {
63         d[++len]=xxx%10;
64         xxx/=10;
65     }
66     LL sum=0;
67     sum+=dfs(len,0,len==1,len!=1,d[len]==0);
68     for (LL i=1;i<=d[len];i++)
69     {
70         sum+=dfs(len,i,1,0,i==d[len]);
71     }
72     return sum;
73 }
74 int main()
75 {
76     LL T=read();int cnt=0;
77     memset(f,-1,sizeof(f));
78     while (T--)
79     {
80         l=read();
81         r=read();
82         if (r<l)swap(l,r);
83         printf("Case #%d: %lld\n",++cnt,calc(r)-calc(l-1));
84     }
85 }

hdu 5898

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

First line a t,then t cases.every line contains two integers L and R.

Output

Print the output for each case on one line in the format as shown below.

Sample Input

2
1 100
110 220

Sample Output

Case #1: 29
Case #2: 36

时间: 2024-10-12 19:16:27

[暑假集训--数位dp]hdu5898 odd-even number的相关文章

[暑假集训--数位dp]LightOj1205 Palindromic Numbers

A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j

[暑假集训--数位dp]hdu3652 B-number

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb

[暑假集训--数位dp]hdu5787 K-wolf Number

Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation of x is pairwised different. Given (L,R,K), please count how many K-wolf numbers in range of L,RL,R . Input The input contains multiple test cases. The

[暑假集训--数位dp]cf55D Beautiful numbers

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

[暑假集训--数位dp]LightOj1032 Fast Bit Calculations

A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true" or "false" respectively). And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is 1 a

[暑假集训--数位dp]LightOJ1140 How Many Zeroes?

Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down? Input Input starts with an integer T (≤ 11000), denoting the number of test cases. Each case contains two

[暑假集训--数位dp]UESTC250 windy数

windy定义了一种windy数. 不含前导零且相邻两个数字之差至少为22 的正整数被称为windy数. windy想知道,在AA 和BB 之间,包括AA 和BB ,总共有多少个windy数? Input 包含两个整数,AA BB . 满足 1≤A≤B≤20000000001≤A≤B≤2000000000 . OutputSample Input 1 10 Sample Output 9 记一下上一位出现的数是啥 1 #include<cstdio> 2 #include<iostrea

数位DP || Gym 101653R Ramp Number

每一位都大于等于前一位的数叫Ramp Number 给一个数,如果不是Ramp Number输出-1,如果是Ramp Number输出比它小的Ramp Number的个数 只和每一位上的数字有关 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <queue> #include &l

数位dp HDU - 5898 odd-even number

http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:求两个数中间的满足连续位是奇数的长度是偶数,连续位上是偶数的长度位奇数的数量. 分析:就是数位dp基本写法,dfs 的参数多加了个 a 表示连续位数的长度,flag表示上一位是否为奇数.  这里当奇偶改变时,a 就要变成1 ,并且可以用 a=0 来表示区分是前导0 .然后 记忆化搜索的过程中 ,用continue 表示不满足条件,暂时满足才往下接着搜索. 然后!!     &运算的优先程度比 ==