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:
#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 动态规划(二)及加强的更多相关文章
- 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 【洛谷】【动态规划(二维)】P1508 Likecloud-吃、吃、吃
[题目描述:] 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏眼花之时,眼前突然闪现出了一个n*m(n and m<=200)的矩型的巨 ...
- 【动态规划/二维背包问题】mr355-三角形牧场
应该也是USACO的题目?同样没有找到具体出处. [题目大意] 和所有人一样,奶牛喜欢变化.它们正在设想新造型牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板, ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 动态规划(二维背包问题):UVAoj 473
Raucous Rockers You just inherited the rights to n previously unreleased songs recorded by the pop ...
- 【洛谷】【动态规划/二维背包】P1855 榨取kkksc03
[题目描述:] ... (宣传luogu2的内容被自动省略) 洛谷的运营组决定,如果...,那么他可以浪费掉kkksc03的一些时间的同时消耗掉kkksc03的一些金钱以满足自己的一个愿望. Kkks ...
- 动态规划(二)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 ...
- 动态规划二:最长公共子序列(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- ...
- 【学习笔记】动态规划—各种 DP 优化
[学习笔记]动态规划-各种 DP 优化 [大前言] 个人认为贪心,\(dp\) 是最难的,每次遇到题完全不知道该怎么办,看了题解后又瞬间恍然大悟(TAT).这篇文章也是花了我差不多一个月时间才全部完成 ...
随机推荐
- 学习微信小程序之css15解决父盒子高度塌陷
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML特效代码大全
1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a>1)贴 ...
- instanceof的用法②
其实这个问题以前也困扰过我.我个人理解的一个应用场合就是,当你拿到一个对象的引用时(例如参数), 你可能需要判断这个引用真正指向的类.所以你需要从该类继承树的最底层开始,使用instanceof操作符 ...
- Arcgis 9.3升级Arcgis10.1需要注重的一点
在项目启动时绑定一个证书文件: 在 Global.asax里面添加 void Application_Start(object sender, EventArgs e) { // Code that ...
- C#内存修改
先通过 System.Diagnostics.Process类获取想要编辑的进程 调用API [Flags] public enum ProcessAccessT ...
- Initializer block.
Ref: Initializing Fields Instance initializers are permitted to refer to the current object via the ...
- IOS 中得runloop 详细解释
1.Runloop基础知识- 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如触 ...
- JSONModel的基本使用
JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...
- DataSet与DataTable基本用法
http://files.cnblogs.com/files/monkeyZhong/DataSetExample.rar 在设计数据库操作或者XML操作等表格数据时,我们难免要跟DataSet和Da ...
- Swift中可选类型(Optional)的用法 以及? 和 ! 的区别 (转载博客,知识分享)
本文转载自:代码手工艺人的博客,原文名称:Swift之 ? 和 ! Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值,也就是说变量不会有默认值,所以要求使用变量之 ...