zoukankan      html  css  js  c++  java
  • [HDOJ2089]不要62(数位DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089

    题意:~~~

    数位dp,dp(i,j)表示i位数最后一位是6时或者不是6时的符合条件的数量,从高位到低位扩展。

    假如i位数的最后一位是6,那么再扩展到i+1位的时候2是一定不能紧随其后的,否则2是可以出现的。但是这两种情况里4是都不允许的。

    要求一个区间内满足条件的数字个数,可以用区间相减来得到结果。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define fr first
     4 #define sc second
     5 #define cl clear
     6 #define BUG puts("here!!!")
     7 #define W(a) while(a--)
     8 #define pb(a) push_back(a)
     9 #define Rint(a) scanf("%d", &a)
    10 #define Rll(a) scanf("%I64d", &a)
    11 #define Rs(a) scanf("%s", a)
    12 #define Cin(a) cin >> a
    13 #define FRead() freopen("in", "r", stdin)
    14 #define FWrite() freopen("out", "w", stdout)
    15 #define Rep(i, len) for(int i = 0; i < (len); i++)
    16 #define For(i, a, len) for(int i = (a); i < (len); i++)
    17 #define Cls(a) memset((a), 0, sizeof(a))
    18 #define Clr(a, x) memset((a), (x), sizeof(a))
    19 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
    20 #define lrt rt << 1
    21 #define rrt rt << 1 | 1
    22 #define pi 3.14159265359
    23 #define RT return
    24 #define lowbit(x) x & (-x)
    25 #define onecnt(x) __builtin_popcount(x)
    26 typedef long long LL;
    27 typedef long double LD;
    28 typedef unsigned long long ULL;
    29 typedef pair<int, int> pii;
    30 typedef pair<string, int> psi;
    31 typedef pair<LL, LL> pll;
    32 typedef map<string, int> msi;
    33 typedef vector<int> vi;
    34 typedef vector<LL> vl;
    35 typedef vector<vl> vvl;
    36 typedef vector<bool> vb;
    37 
    38 const int maxn = 25;
    39 
    40 LL digit[maxn];
    41 LL dp[maxn][2];
    42 
    43 LL dfs(int l, bool num, bool jud) {
    44   if(l == 0) return dp[l][num] = 1;
    45   if(!jud && ~dp[l][num]) return dp[l][num];
    46   LL ret = 0;
    47   int nex = jud ? digit[l] : 9;
    48   Rep(i, nex+1) {
    49     if((num && i == 2) || i == 4) continue;
    50     ret += dfs(l-1, i==6, jud&&i==nex);
    51   }
    52   if(!jud) dp[l][num] = ret;
    53   return ret;
    54 }
    55 
    56 LL f(LL num) {
    57   int pos = 0;
    58   while(num) {
    59     digit[++pos] = num % 10;
    60     num /= 10;
    61   }
    62   return dfs(pos, false, true);
    63 }
    64 
    65 LL l, r;
    66 
    67 signed main() {
    68   //FRead();
    69   Clr(dp, -1);
    70   while(cin >> l >> r && l + r) {
    71     cout << f(r) - f(l-1) << endl;
    72   }
    73   RT 0;
    74 }
  • 相关阅读:
    Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
    洛谷P1002 过河卒 题解 动态规划
    python 查看数据类型
    python 输出小技巧
    python 基础定义变量
    python 关键字
    flask restful 反查询
    Please edit configuration/connection/logging settings in '/home/mac126/Desktop/13/test3-1/migrations/alembic.ini' before proceeding.
    KeyError: 'migrate'错误解决办法
    flask 的简单拆分升级版2
  • 原文地址:https://www.cnblogs.com/kirai/p/5890662.html
Copyright © 2011-2022 走看看