未加强传送门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:

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int mod = , maxn = + ;
int n;
long long status[maxn][];
void read(int& x){
x = ; int sig = ;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') sig = -; ch = getchar();}
while(isdigit(ch)) {x = * x + ch - ''; ch = getchar();}
return ;
}
int solve(){
for (int i = ; i < ; i ++)
status[][i] = ;
for (int i = ; i <= n; i ++){
status[i][] = ;
status[i][] = (status[i - ][] * + status[i - ][]) % mod;
status[i][] = (status[i - ][] + status[i - ][]) % mod;
status[i][] = (status[i - ][] * + status[i - ][]) % mod;
status[i][] = (status[i - ][] * + status[i - ][] + status[i - ][]) % mod;
status[i][] = (status[i - ][] * + status[i - ][] + status[i - ][]) % mod;
}
return status[n][];
}
int main(){
read(n);
printf("%d\n", solve());
return ;
}

然后再改成滚动的DP:

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int mod = ;
int n;
long long status[][];
void read(int& x){
x = ; int sig = ;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') sig = -; ch = getchar();}
while(isdigit(ch)) {x = * x + ch - ''; ch = getchar();}
return ;
}
int cur = ;
int solve(){
for (int i = ; i < ; i ++)
status[cur][i] = ;
for (int i = ; i <= n; i ++){
status[cur][] = ;
status[cur][] = (status[cur ^ ][] * + status[cur ^ ][]) % mod;
status[cur][] = (status[cur ^ ][] + status[cur ^ ][]) % mod;
status[cur][] = (status[cur ^ ][] * + status[cur ^ ][]) % mod;
status[cur][] = (status[cur ^ ][] * + status[cur ^ ][] + status[cur ^ ][]) % mod;
status[cur][] = (status[cur ^ ][] * + status[cur ^ ][] + status[cur ^ ][]) % mod;
cur ^= ;
}
return status[cur ^ ][];
}
int main(){
read(n);
printf("%d\n", solve());
return ;
}

复杂度O(n)。

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

 #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MOD = ;
int n;
void read(int& x){
x = ; int sig = ; char ch = getchar();
while(!isdigit(ch)) { if(ch = '-') sig = -; ch = getchar(); }
while(isdigit(ch)) { x = * x + ch - ''; ch = getchar(); }
return ;
}
struct Matrix{
long long A[][];
Matrix operator * (const Matrix& ths) const{
Matrix c;
for(int i = ; i < ; i ++)
for(int j = ; j < ; j ++){
c.A[i][j] = ;
for(int k = ; k < ; k ++) c.A[i][j] += A[i][k] * ths.A[k][j];
c.A[i][j] %= MOD;
}
return c;
}
};
void Matrix_Pow(Matrix& ans, int n){
Matrix tmp = ans;
n --;
while(n){
if(n & ) ans = ans * tmp;
tmp = tmp * tmp;
n = n >> ;
}
return ;
}
int A[][] = { //前面有逗号,最后有分号,每个括号最后没东西
{ , , , , , },
{ , , , , , },
{ , , , , , },
{ , , , , , },
{ , , , , , },
{ , , , , , }
/*
status[cur][0] = 1;
status[cur][1] = (status[cur ^ 1][1] * 2 + status[cur ^ 1][0]) % mod;
status[cur][2] = (status[cur ^ 1][2] + status[cur ^ 1][0]) % mod;
status[cur][3] = (status[cur ^ 1][3] * 2 + status[cur ^ 1][1]) % mod;
status[cur][4] = (status[cur ^ 1][4] * 2 + status[cur ^ 1][2] + status[cur ^ 1][1]) % mod;
status[cur][5] = (status[cur ^ 1][5] * 2 + status[cur ^ 1][4] + status[cur ^ 1][3]) % mod;
*/
};
Matrix ans;
int main(){
for(int i = ; i < ; i ++)
for(int j = ; j < ; j ++)
ans.A[i][j] = A[i][j];
int n; read(n);
Matrix_Pow(ans, n - );
printf("%lld\n", ans.A[][]);
return ;
}

就变成O(logn)的了。

COJ 0601&0602 动态规划(二)及加强的更多相关文章

  1. 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表

    [题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...

  2. 【洛谷】【动态规划(二维)】P1508 Likecloud-吃、吃、吃

    [题目描述:] 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏眼花之时,眼前突然闪现出了一个n*m(n and m<=200)的矩型的巨 ...

  3. 【动态规划/二维背包问题】mr355-三角形牧场

    应该也是USACO的题目?同样没有找到具体出处. [题目大意] 和所有人一样,奶牛喜欢变化.它们正在设想新造型牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板, ...

  4. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  5. 动态规划(二维背包问题):UVAoj 473

     Raucous Rockers  You just inherited the rights to n previously unreleased songs recorded by the pop ...

  6. 【洛谷】【动态规划/二维背包】P1855 榨取kkksc03

    [题目描述:] ... (宣传luogu2的内容被自动省略) 洛谷的运营组决定,如果...,那么他可以浪费掉kkksc03的一些时间的同时消耗掉kkksc03的一些金钱以满足自己的一个愿望. Kkks ...

  7. 动态规划(二)HDU1114

    1.题目来源HDU1114 Sample Input 3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4 Sample Output The ...

  8. 动态规划二:最长公共子序列(LCS)

    1.两个子序列:X={x1,x2....xm},Y={y1,y2....yn},设Z={z1,z2...zk}. 2.最优子结构: 1)如果xm=yn ,则zk=xm=yn且Zk-1是Xm-1和Yn- ...

  9. 【学习笔记】动态规划—各种 DP 优化

    [学习笔记]动态规划-各种 DP 优化 [大前言] 个人认为贪心,\(dp\) 是最难的,每次遇到题完全不知道该怎么办,看了题解后又瞬间恍然大悟(TAT).这篇文章也是花了我差不多一个月时间才全部完成 ...

随机推荐

  1. Linux Device Driver 3th 中的一些坑

    linux设备驱动第三版由于年代比较久远,有很多东西已过时.开一贴记录自己发现的一些问题. 4.3.1.4. seq_file接口 此节最后提到用 struct proc_dir_entry* cre ...

  2. 汉诺塔-Hanoi

    1. 问题来源: 汉诺塔(河内塔)问题是印度的一个古老的传说. 法国数学家爱德华·卢卡斯曾编写过一个印度的古老传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵 ...

  3. ssh登录很慢解决方法

    使用ssh客户端(如:putty)连接Linux服务器,可能会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析问题 ...

  4. 设置div中文字超出时自动换行

    一.对于div强制换行1.(IE浏览器)white-space:normal; word-break:break-all;这里前者是遵循标准.#wrap{white-space:normal; wid ...

  5. hadoop2集群中的datanode启动以后自动关闭的问题

    今天在启动前几天搭建成功的hadoop集群的时候,出现了datanode启动之后自动关闭的情况,经过查询之后发现问题产生的原因是:在第一次格式化dfs后,启动并使用了hadoop,后来又重新执行了格式 ...

  6. 【转】iOS使用NSMutableAttributedString实现富文本

    iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...

  7. TextField的文字距左边框的距离偏移

    默认情况下,当向textField输入文字时,文字会紧贴在textField左边框上. 我们可以通过设置textField的leftView,设置一个只有宽度的leftView. 这样还不够,因为默认 ...

  8. ILMerge合并程序

    在DOS窗口中,进入到ILMerge的安装目录 中 如图所示,之后写合并代码, 使用命令进行捆绑,以如图为例,将CSkin.dll和MyTool.exe捆绑成一个新的newtool.exe文件./ou ...

  9. opencv安装及学习资料

    第一次装时win7+VS2010+opencv3.0,结果不成功,原因解压出来的没有vc10,可能新版本不在支持vc的旧版本了.所以换了VS2013+opencv3.0,比较经典的安装时VS2010+ ...

  10. MVC埰坑日记 文件权限

    public static void DownLoadFile(string FileFullPath) { if (!string.IsNullOrEmpty(FileFullPath) & ...