zoukankan      html  css  js  c++  java
  • COJ 0601&0602 动态规划(二)及加强

    未加强传送门0601:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=571

    加强传送门0602:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=572

    试题描述:

    我们把一个数称为有趣的,当且仅当:
    1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次。
    2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前。
    3. 最高位数字不为0。
    因此,符合我们定义的最小的有趣的数是2013。除此以外,4位的有趣的数还有两个:2031和2301。
    请计算恰好有n位的有趣的数的个数。由于答案可能非常大,只需要输出答案除以1000000007的余数。

    输入:

    输入只有一行,包括恰好一个正整数n (4 ≤ n ≤ 1000000(及100000000))。 

    输出:

    输出只有一行,包括恰好n 位的整数中有趣的数的个数除以1000000007的余数。 

    输入示例:

    4

    输出示例:

    3

    其他说明:

    数位DP+强制滚动

    题解:

    先考虑可持久化的解。首先我们还是得从题目分析
    0不能再首位是吧?那首位只能是1,2,3中的一个呗是吧?你放1看看行不,肯定不行啊,所有0得在1的前面,你放1了让0情何以堪?放2可以,放3的话你让2情何以堪?
    所以首位只能放2对吧?
    现在来说status这个二维数组,status[i][j]到第i个位置满足第j种状态的所有可能数,总共最多有6种状态:
    0 -- 0 1 (2) 3
    1 -- (0) 1 (2) 3
    2 -- 0 1 (2) (3)
    3 -- (0) (1) (2) 3
    4 -- (0) 1 (2) (3)
    5 -- (0) (1) (2) (3)
    在括号里的数说明前面的k位中,只出现了括号里的数,你可以发现6个状态中2都是被括号括起来的,没办法,刚刚说了2必须是首位啊

    比如说status[5][4] = 70的意思就是,到第5位(首位是第1位!)为止,保持第4种状态的数一共有70个(咳咳,当然数据是我瞎掰的)
    而且这是你在填第k+1位的时候能保持的仅有的6个状态,其他的状态都是无效的
    比如0(1)(2) 3就是无效的,那是不可能的啊,假如说你现在填的是第k位吧,那么你填完第k位的数后的状态是只包括1和2,那你想想,怎么可能只出现1和2呢?那你让0情何以堪?0就没地方放了。
    下面说转移:首先status[i][0] = 1;
    到第i位为止,第i位填的数字使整个数字要满足状态0,那么这样的可能有几个?1个呗,你要能填出2以外的数来我就去……
    status[i][1] = (status[i - 1][1] * 2 + status[i - 1][0]) % mod;
    到第i位为止,填第i位的数,使整个数保持状态1。要满足状态1,也就是只出现0和2,那你想前i-1位应该是个什么状态,要么是只有2,要么是0,2对吧?就两种状态
    然后如果前i-1位是状态0的话(只有2),那你这一位只能填0了嘛,所以status[i - 1][0]就是这样来的,如果前i-1位是状态1(有0,2),那么你现在有两种选择,填0或者是2两种情况,status[i - 1][1] * 2就是这样来的,然后把两种情况相加,就表示到第i位为止,状态为1的数共有status[i][1]这么多个
    然后后面的都差不多啦,因为每一位可能会进入6种不同的状态,所以都要算出来
    最后输出status[n][5],因为题目要求0,1,2,3必须至少出现一次,所有我们要输出状态5的数的个数

    中间结果不断取余绝对会爆int(最坏情况是3个INF加起来超了),所以用long long存决策。

    可持久化的DP:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 const int mod = 1000000007, maxn = 1000000 + 10;
     6 int n;
     7 long long status[maxn][6];
     8 void read(int& x){
     9     x = 0; int sig = 0;
    10     char ch = getchar();
    11     while(!isdigit(ch)) {if(ch == '-') sig = -1; ch = getchar();}
    12     while(isdigit(ch)) {x = 10 * x + ch - '0'; ch = getchar();}
    13     return ;
    14 }
    15 int solve(){
    16     for (int i = 0; i < 6; i ++)
    17         status[0][i] = 0;
    18     for (int i = 1; i <= n; i ++){
    19         status[i][0] = 1;
    20         status[i][1] = (status[i - 1][1] * 2 + status[i - 1][0]) % mod;
    21         status[i][2] = (status[i - 1][2] + status[i - 1][0]) % mod;
    22         status[i][3] = (status[i - 1][3] * 2 + status[i - 1][1]) % mod;
    23         status[i][4] = (status[i - 1][4] * 2 + status[i - 1][2] + status[i - 1][1]) % mod;
    24         status[i][5] = (status[i - 1][5] * 2 + status[i - 1][4] + status[i - 1][3]) % mod;
    25     }
    26     return status[n][5];
    27 }
    28 int main(){
    29     read(n);
    30     printf("%d
    ", solve());
    31     return 0;
    32 }

    然后再改成滚动的DP:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 const int mod = 1000000007;
     6 int n;
     7 long long status[2][6];
     8 void read(int& x){
     9     x = 0; int sig = 0;
    10     char ch = getchar();
    11     while(!isdigit(ch)) {if(ch == '-') sig = -1; ch = getchar();}
    12     while(isdigit(ch)) {x = 10 * x + ch - '0'; ch = getchar();}
    13     return ;
    14 }
    15 int cur = 0;
    16 int solve(){
    17     for (int i = 0; i < 6; i ++)
    18         status[cur][i] = 0;
    19     for (int i = 1; i <= n; i ++){
    20         status[cur][0] = 1;
    21         status[cur][1] = (status[cur ^ 1][1] * 2 + status[cur ^ 1][0]) % mod;
    22         status[cur][2] = (status[cur ^ 1][2] + status[cur ^ 1][0]) % mod;
    23         status[cur][3] = (status[cur ^ 1][3] * 2 + status[cur ^ 1][1]) % mod;
    24         status[cur][4] = (status[cur ^ 1][4] * 2 + status[cur ^ 1][2] + status[cur ^ 1][1]) % mod;
    25         status[cur][5] = (status[cur ^ 1][5] * 2 + status[cur ^ 1][4] + status[cur ^ 1][3]) % mod;
    26         cur ^= 1;
    27     }
    28     return status[cur ^ 1][5];
    29 }
    30 int main(){
    31     read(n);
    32     printf("%d
    ", solve());
    33     return 0;
    34 }

    复杂度O(n)。

    A掉加强版就改成矩阵快速幂就好了。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 const int MOD = 1000000007;
     6 int n;
     7 void read(int& x){
     8     x = 0; int sig = 1; char ch = getchar();
     9     while(!isdigit(ch)) { if(ch = '-') sig = -1; ch = getchar(); }
    10     while(isdigit(ch)) { x = 10 * x + ch - '0'; ch = getchar(); }
    11     return ;
    12 }
    13 struct Matrix{
    14     long long A[6][6];
    15     Matrix operator * (const Matrix& ths) const{
    16         Matrix c;
    17         for(int i = 0; i < 6; i ++)
    18            for(int j = 0; j < 6; j ++){
    19                   c.A[i][j] = 0;
    20                   for(int k = 0; k < 6; k ++) c.A[i][j] += A[i][k] * ths.A[k][j];
    21                   c.A[i][j] %= MOD;
    22            }
    23         return c;
    24     }
    25 };
    26 void Matrix_Pow(Matrix& ans, int n){
    27     Matrix tmp = ans;
    28     n --;
    29     while(n){
    30         if(n & 1) ans = ans * tmp;
    31         tmp = tmp * tmp;
    32         n = n >> 1;
    33     }
    34     return ;
    35 }
    36 int A[6][6] = { //前面有逗号,最后有分号,每个括号最后没东西 
    37     { 1, 0, 0, 0, 0, 0 },
    38     { 1, 2, 0, 0, 0, 0 },
    39     { 1, 0, 1, 0, 0, 0 },
    40     { 0, 1, 1, 2, 0, 0 },
    41     { 0, 1, 0, 0, 2, 0 },
    42     { 0, 0, 0, 1, 1, 2 }
    43     /*
    44     status[cur][0] = 1;
    45     status[cur][1] = (status[cur ^ 1][1] * 2 + status[cur ^ 1][0]) % mod;
    46     status[cur][2] = (status[cur ^ 1][2] + status[cur ^ 1][0]) % mod;
    47     status[cur][3] = (status[cur ^ 1][3] * 2 + status[cur ^ 1][1]) % mod;
    48     status[cur][4] = (status[cur ^ 1][4] * 2 + status[cur ^ 1][2] + status[cur ^ 1][1]) % mod;
    49     status[cur][5] = (status[cur ^ 1][5] * 2 + status[cur ^ 1][4] + status[cur ^ 1][3]) % mod;
    50     */ 
    51 };
    52 Matrix ans;
    53 int main(){
    54     for(int i = 0; i < 6; i ++)
    55       for(int j = 0; j < 6; j ++)
    56          ans.A[i][j] = A[i][j];
    57     int n; read(n);
    58     Matrix_Pow(ans, n - 1);
    59     printf("%lld
    ", ans.A[5][0]);
    60     return 0;
    61 }

    就变成O(logn)的了。

  • 相关阅读:
    BootstrapValidator验证规则、BootStrap表格:列参数
    使用JSONObject解析和生成json
    java.Math类常用方法
    Java内存溢出处理
    windows下Ubuntu虚拟机联网配置 + Ubuntu虚拟机代理配置
    C# 获取并判断操作系统版本,解决Win10、 Windows Server 2012 R2 读取失败的方案
    C++ 获取并判断操作系统版本,解决Win10、 Windows Server 2012 R2 读取失败的方案
    asp.net 程序,当发生找不到文件的错误时,如何正确定位是哪个文件?
    MVC 网站部署常见问题汇总
    ASP.NET windows验证IIS配置
  • 原文地址:https://www.cnblogs.com/chxer/p/4388215.html
Copyright © 2011-2022 走看看