Y

来源:互联网 发布:优酷怎么不能登录淘宝 编辑:程序博客网 时间:2024/04/26 04:44

Y - odd-even number

 
      要求一个数的每一位,每连续的奇数要是偶数个,连续的偶数要是奇数个。

代码如下:

#include<iostream>#include<cstring>using namespace std;typedef long long LL;int bit[20];LL dp[20][10][20];//   pos pre sta LL dfs(int pos, int pre, int sta, bool lead, bool limit){if (pos == -1){if (pre % 2 != sta % 2 && !lead)//判定条件return 1;return 0;}if (!limit && dp[pos][pre][sta] != -1)return dp[pos][pre][sta];int up = limit ? bit[pos] : 9;LL temp = 0;for (int i = 0; i <= up; i++){int sta2;if (lead && i == 0)//全为0sta2 = 0;else if (i % 2 == pre % 2)//pre 和 i 相同奇偶sta2 = sta + 1;else if (sta % 2 != pre % 2 || lead && i != 0)//不同奇偶  或者 第一次出现sta2 = 1;else if (sta != 0 || !limit)continue;temp += dfs(pos - 1, i, sta2, lead && i == 0, limit && i == bit[pos]);}if (!limit)dp[pos][pre][sta] = temp;return temp;}LL solve(LL x){memset(dp, -1, sizeof(dp));int pos = 0;while (x){bit[pos++] = x % 10;x /= 10;}return dfs(pos - 1, 0, 0, 1, 1);}int main(){int t;LL le, ri;cin >> t;int cnt = 1;while (t--){cin >> le >> ri;LL ans1 = solve(ri);LL ans2 = solve(le - 1);cout << "Case #" << cnt++ << ": " << ans1 - ans2 << endl;}return 0;}




原创粉丝点击