未加强传送门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. 第二篇:从 GPU 的角度理解并行计算

    前言 本文从使用 GPU 编程技术的角度来了解计算中并行实现的方法思路. 并行计算中需要考虑的三个重要问题 1. 同步问题 在操作系统原理的相关课程中我们学习过进程间的死锁问题,以及由于资源共享带来的 ...

  2. Android 根据屏幕分辨率自动调整字体大小

    1.在oncreate 里获取手机屏幕宽和高度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDispl ...

  3. xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Deve

    以上错误是因为安装了 xcode , 但并不是系统默认的位置, 所以可以使用以下命令把 xcode 的路径修改为你安装的位置即可 sudo xcode-select --switch /Applica ...

  4. MD5加密 Java源代码

    package lwp; /** * * @author 梁WP */ public class MD5_Encoding { // RFC1321中定义的标准4*4矩阵的常量定义. static f ...

  5. 对象-关系映射ORM(Object Relational Mapping)(转)

    ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现 Hibernate在实现ORM功能的时候主要用到的文件有:映射类(*.java).映射文件(*.hbm.xml)和数据库配置文件 ...

  6. JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象

    //把url的参数部分转化成json对象 parseQueryString: function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_par ...

  7. .net 学习路线感想

    从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...

  8. 基于nodejs的消息中心

    参考:http://t42dw.iteye.com/blog/1767013

  9. 2016.7.13final 修饰符使用

    final修饰符可以修饰类.变量.函数: 1.被final所修饰的类不能被继承,函数不能被继承,成员变量不能再次被赋值并且被称为常量: 2.被final 修饰的成员变量 .它通常被static所修饰, ...

  10. 泛型? extents super

    ?可以接受任何泛型集合,但是不能编辑集合值.所以一般只在方法参数中用 例子: ? extends Number  则类型只能是Number类的子孙类 ? super String  则类型只能是Str ...