深度优先搜索(dfs)

【题目描述】

sol:50pts随便写写,就是大众分了,直接n2dpOK,100分要找点规律,需要数学头脑

官方题解

//#include <bits/stdc++.h>
//using namespace std;
//typedef long long ll;
//inline ll read()
//{
// ll s=0;
// bool f=0;
// char ch=' ';
// while(!isdigit(ch))
// {
// f|=(ch=='-'); ch=getchar();
// }
// while(isdigit(ch))
// {
// s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
// }
// return (f)?(-s):(s);
//}
//#define R(x) x=read()
//inline void write(ll x)
//{
// if(x<0)
// {
// putchar('-'); x=-x;
// }
// if(x<10)
// {
// putchar(x+'0'); return;
// }
// write(x/10);
// putchar((x%10)+'0');
// return;
//}
//#define W(x) write(x),putchar(' ')
//#define Wl(x) write(x),putchar('\n')
//const int N=3005;
//const ll Mod=1000000007;
//int n,a[N];
//ll dp[N][N],Sum[N];
//inline void Ad(ll &x,ll y) {x=x+y; x-=(x>=Mod)?Mod:0;}
//int main()
//{
// freopen("dfs.in","r",stdin);
// freopen("dfs.out","w",stdout);
// int i,j;
// R(n);
// for(i=1;i<=n;i++) R(a[i]);
// if((a[1]!=-1)&&(a[1]!=0)) return puts("0"),0;
// dp[1][0]=1; Sum[n+1]=0; for(i=n;i>=0;i--) Ad(Sum[i],Sum[i+1]+dp[1][i]);
// for(i=2;i<=n;i++)
// {
// if(a[i]!=-1) Ad(dp[i][a[i]],Sum[a[i]-1]);
// else for(j=1;j<=n;j++) Ad(dp[i][j],Sum[j-1]);
// for(j=n;j>=0;j--) {Sum[j]=Sum[j+1]+dp[i][j]; Sum[j]-=((Sum[j]>=Mod)?Mod:0);}
// }
// ll ans=0;
// for(i=0;i<=n;i++) Ad(ans,dp[n][i]);
// Wl(ans);
// return 0;
//}
///*
//input
//2
//1 -1
//output
//0
//
//input
//4
//0 -1 1 -1
//output
//2
//
//input
//5
//-1 -1 -1 -1 -1
//output
//14
//*/ #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const ll Mod=;
const int N=;
int n,a[N];
ll Jiec[N],Invj[N];
inline ll Ksm(ll x,ll y)
{
ll ans=;
while(y)
{
if(y&) ans=ans*x%Mod;
x=x*x%Mod;
y>>=;
}
return ans;
}
inline ll C(ll n,ll m)
{
if(n<m||m<) return ;
return Jiec[n]*Invj[m]%Mod*Invj[n-m]%Mod;
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
ll i,Last,ans;
R(n);
for(i=;i<=n;i++) R(a[i]);
Jiec[]=Invj[]=1ll;
for(i=;i<=*n;i++)
{
Jiec[i]=Jiec[i-]*i%Mod; Invj[i]=Ksm(Jiec[i],Mod-);
}
if((a[]!=)&&(a[]!=-)) return puts(""),;
a[]=; Last=ans=a[n+]=;
for(i=;i<=n+;i++) if(a[i]!=-)
{
if(a[i]==) return puts(""),;
int s=i-Last-,x=i-Last--a[i]++a[Last];
if(x<) return puts(""),;
ans=1ll*ans*(C(s+x,x)-C(s+x,x-a[Last]-))%Mod;
Last=i;
}
ans+=(ans<)?Mod:;
Wl(ans);
return ;
}
/*
input
6
-1 -1 -1 -1 -1 -1
output
42
*/

7.6 T1 深度优先搜索(dfs)的更多相关文章

  1. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  2. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  3. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

  4. 【算法入门】深度优先搜索(DFS)

    深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...

  5. 深度优先搜索 DFS 学习笔记

    深度优先搜索 学习笔记 引入 深度优先搜索 DFS 是图论中最基础,最重要的算法之一.DFS 是一种盲目搜寻法,也就是在每个点 \(u\) 上,任选一条边 DFS,直到回溯到 \(u\) 时才选择别的 ...

  6. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  7. 算法总结—深度优先搜索DFS

    深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake C ...

  8. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  9. [LeetCode OJ] Word Search 深度优先搜索DFS

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. SQL SERVER中Datetime时间的范围与.net的DateTime对象的区别

    对于编写.net程序中我们一般写默认的时间,我们会自动创建一个new DateTime()对象.但与SQL SERVER连用我们就会出现一个时间范围的问题. 今天我就记录一下该时间问题. 我们创建的n ...

  2. postman传数组参数,二维数组,多维数组

    一维数组: 传递: 接收: 二维数组: 传递: 接收: 依此类推,

  3. macbook打印出现乱码解决方案

    系统偏好设置 --> 打印机与扫描仪 --> + (左下角的加号) --> IP --> 输入打印机的ip地址,然后最下面的 “使用选择” 中选中 普通PCL 打印机,(默认的 ...

  4. TVM使用问题记录

    1.numpy提示repeat错误 错误信息为 One method of fixing this is to repeatedly uninstall numpy until none is fou ...

  5. shell运行下的写日志

    tee 重定向输出到多个文件   在执行Linux命令时,我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令 要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读 ...

  6. Cannot create OpenGL context for 'eglMakeCurrent'.

    10.3.2编译的app,在小米手机上出这个问题,华为的正常. 解决方法: 窗口的Quality属性用SystemDefault,不要用HighQuality. 10.3.1也有此问题.

  7. GDI+ 绘图教程 验证码

    使用的 C# winform using System; using System.Collections.Generic; using System.ComponentModel; using Sy ...

  8. apache简介与安装

    1.1 apache简介 apache当前全世界排名点击这里 1.1.1 当前互联网主流web服务说明 静态服务 apache --->中小型静态web服务的主流,web服务器中的老大哥 ngi ...

  9. 将 spring boot 安装为 systemd 服务

    [root@ecs-11-132 system]# cat /etc/systemd/system/push-gateway-3.0.0.service [Unit] Description=app- ...

  10. CentOS7连接无线网络

    背景  CentOS7.6最小化安装,没有网线,幸好有无线网卡.下面我们直接进入主题.  附:安装教程 主题--连接无线网络 最小化安装后,裸机没有ifconfig,没有iw,最可怕的是没有网线 但我 ...