UVA580-Critical Mass

题意

有两种方块,L和U,有至少三个连续的U称为危险组合,问有多少个危险组合

solution:

至少这个概念比较难求 ,所以转化为(1ll<<n)-安全组合

dp[n][i]表示前n个数里以i个U结尾的个数

递推方程

dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][1];

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#define mp make_pair
#define pb push_back
#define first fi
#define second se
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define freopen freopen("in.txt","r",stdin);
#define cfin ifstream cin("in.txt");
#define lowbit(x) (x&(-x))
#define sqr(a) a*a
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define dd(x) cout << #x << " = " << (x) << ", "
#define de(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using namespace std;
//**********************************
const int maxn=;
int n;
ll dp[][];
//**********************************
void Init()
{
cl(dp,);
dp[][]=dp[][]=dp[][]=dp[][]=;dp[][]=;
FOR(i,,){
dp[i][]=dp[i-][]+dp[i-][]+dp[i-][];
dp[i][]=dp[i-][];
dp[i][]=dp[i-][];
// dd(i);dd(dp[i][0]);dd(dp[i][1]);de(dp[i][2]);
}
}
//**********************************
int main()
{
Init();
while(cin>>n,n){
ll ans=;
ans=dp[n][]+dp[n][]+dp[n][];
printf("%lld\n",(1ll<<n)-ans);
}
return ;
}

第二种实现方法

dp[i+j][1]+=dp[i][0];

dp[i+j][0]+=dp[i][1];

     /*************************************************************************
> File Name: a.cpp
> Author: QWX
> Mail:
> Created Time: Sun 30 Sep 2018 04:01:42 AM PDT
************************************************************************/ //{{{ #include
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<complex>
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define first fi
#define second se
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define dd(x) cout << #x << " = " << (x) << ","
#define de(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using namespace std;
//}}} ll dp[][];
int n; int main()
{
dp[][]=dp[][]=;
FOR(i,,){
FOR(j,,)dp[i+j][]+=dp[i][];
FOR(j,,)dp[i+j][]+=dp[i][];
}
while(cin>>n,n)cout<<(1ll<<n)-dp[n][]-dp[n][]<<endl;
return ;
}

类似题目

The Debut Album

题意:

给你一个串的长度n,串由1,2组成,1连续不超过a个,2连续不超过b个,问有多少种这样的串,对1e9+7取模。

solution:

递推方程

往后补值:dp[i+j][1]+=dp[i][0];

     dp[i+j][0]+=dp[i][1];

向前取值:dp[i][1]+=dp[j][0];

     dp[i][0]+=dp[j][1];

 /*************************************************************************
> File Name: j.cpp
> Author: QWX
> Mail:
> Created Time: Fri 05 Oct 2018 08:58:42 PM PDT
************************************************************************/ //{{{ #include
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<complex>
//#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define first fi
#define second se
#define vi vector<int>
#define pii pair<int,int>
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define dd(x) cout << #x << " = " << (x) << ","
#define de(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using namespace std;
//}}} int n;
const int maxn=5e5+;
int dp[maxn][];
const int mod=1e9+;
inline int add(int a,int b){a+=b;if(a>mod)a-=mod;return a;} int main()
{
int n,a,b;
cin>>n>>a>>b;
cl(dp,);
dp[][]=dp[][]=;
FOR(i,,n){
FOR(j,,a)dp[i+j][]=add(dp[i+j][],dp[i][]);
FOR(j,,b)dp[i+j][]=add(dp[i+j][],dp[i][]);
}
cout<<add(dp[n][],dp[n][])<<endl;
return ;
}

递推,求至少连续放置三个U的危险组合的更多相关文章

  1. NYOJ-301递推求值

    递推求值 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f ...

  2. 算法笔记_091:蓝桥杯练习 递推求值(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) + 3F(n- ...

  3. 查找至少连续出现三次的所有数字/连续3天的日期【LeetCode】

    编写一个SQL查询,查找至少连续出现三次的所有数字.+----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | ...

  4. NYOJ——301递推求值(矩阵快速幂)

    递推求值 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f(n)的 ...

  5. poj 3744 Scout YYF I(递推求期望)

    poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...

  6. Java实现 蓝桥杯 算法提高 递推求值

    算法提高 递推求值 时间限制:1.0s 内存限制:256.0MB 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) ...

  7. Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)

    有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...

  8. poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3523   Accepted: 1740 ...

  9. attack on titans(动态规划递推,限制条件,至少转至多方法,进击的巨人)

    题目意思: 给n个士兵排队,每个士兵三种G.R.P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数. 原题 Attack on Titans Time Limit: 2 Seconds ...

随机推荐

  1. Django中间件理解

    一.中间件 https://www.cnblogs.com/maple-shaw/articles/9333824.html 中间件:是一个类处理django的请求和响应,本质上就是一个类,在类里面定 ...

  2. 四款免费好用的Bootstrap ui编辑器

    Bootstrap带来了设计革命,本文介绍的四种免费Bootstrap在线设计工具,可视化所见所得设计网页,然后输出Html/CSS代码,其中有些甚至可以实现拖曳,也有可以设定自己的主题模板Theme ...

  3. Lab1 Report

    Lab1 report A) The brief description that you install junit, hamcrest and eclemma. a)  install junit ...

  4. CDH5.16.1的kafka集群报错: No broker in ISR for partition [getngo_collect_apm_test,0]

    1 详细错误 kafka.common.NoReplicaOnlineException: No broker in ISR for partition [getngo_collect_apm_tes ...

  5. python 之禅 又名 蛇宗三字经

    打开cmd 输入python回车 import this Beautiful is better than ugly. Explicit is better than implicit. Simple ...

  6. Sourcetree 出现错误提示git -c diff.mnemonicprefix=false -c core.quotepath=false fetch origin

    具体表现为:sourcetree无法和gitlab远程仓库进行交互,但使用本地cmd,可以使用git命令和远程仓库交互通过各种账户.秘钥等操作,都无法解决该问题具体信息如下: 解决方式:点击 工具–& ...

  7. 使用pipenv管理你的python项目

    怎么使用pipenv管理你的python项目   原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with- ...

  8. 微信小程序中concat 和push的区别

    push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(1); arr.push(2); arr.push([3, 4]) arr.push ...

  9. Linux部署Django:报错 nohup: ignoring input and appending output to ‘nohup.out’

    一.部署 Django 到远程 Linux 服务器 利用 xshell 通过 ssh 连接到 Linux服务器,常规的启动命令是 python3 manage.py runserver 但是,关闭 x ...

  10. Git 分支开发规范

    您必须知道的 Git 分支开发规范 Git 是目前最流行的源代码管理工具. 为规范开发,保持代码提交记录以及 git 分支结构清晰,方便后续维护,现规范 git 的相关操作. 分支管理 分支命名 ma ...