hdu 5898 odd-even number

来源:互联网 发布:淘宝货到付款可以退吗 编辑:程序博客网 时间:2024/05/16 05:03


odd-even number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 84    Accepted Submission(s): 39


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
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: 29Case #2: 36
 

Source
2016 ACM/ICPC Asia Regional Shenyang Online
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5901 5900 5899 5897 5896 
题意:求l到r的数里面,有多少数符合奇数连续的数位长度是偶数,偶数连续的数位长度是奇数。

思路:很明显就是一个数位dp,我用dp[i][j]表示第i位数前一位数的状态是j。状态有4种,奇数长度为奇,奇数长度为偶,偶数长度为奇,偶数长度为偶,我外加一个状态0表示前面全是前导0,接下来的就是基本的数位dp思想,不多说了,下面给代码:

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>typedef long long LL;using namespace std;#define inf 0x3f3f3f3f#define maxn 55typedef long long LL;int num[25];LL dp[25][5];LL dfs(int pos, int limit, int status){if (pos < 1){if (status == 2 || status == 3)return 1;elsereturn 0;}if (!limit&&~dp[pos][status])return dp[pos][status];int end = limit ? num[pos] : 9;LL ans = 0;for (int i = 0; i <= end; i++){if (!status){if (!i){ans += dfs(pos - 1, 0, 0);}else if (i & 1){ans += dfs(pos - 1, limit&&i == end, 1);}else{ans += dfs(pos - 1, limit&&i == end, 3);}}else{if (status == 1){if (i & 1){ans += dfs(pos - 1, limit&&i == end, 2);}}else if (status == 2){if (i & 1){ans += dfs(pos - 1, limit&&i == end, 1);}else{ans += dfs(pos - 1, limit&&i == end, 3);}}else if (status == 3){if (i & 1){ans += dfs(pos - 1, limit&&i == end, 1);}else{ans += dfs(pos - 1, limit&&i == end, 4);}}else{if (!(i & 1)){ans += dfs(pos - 1, limit&&i == end, 3);}}}}dp[pos][status] = ans;return ans;}LL solve(LL x){memset(dp, -1, sizeof(dp));int len = 0;while (x){num[++len] = x % 10;x /= 10;}return dfs(len, 1, 0);}int main(){int t;scanf("%d", &t);for (int tcase = 1; tcase <= t; tcase++){LL l, r;scanf("%lld%lld", &l, &r);printf("Case #%d: %lld\n", tcase, solve(r) - solve(l - 1));}}


0 0
原创粉丝点击