HDU 5399 Too Simple(过程中略微用了一下dfs)——多校练习9
Too Simple
Teacher Mai has m functions f1,f2,⋯,fm:{1,2,⋯,n}→{1,2,⋯,n}(that
means for all x∈{1,2,⋯,n},f(x)∈{1,2,⋯,n}).
But Rhason only knows some of these functions, and others are unknown.
She wants to know how many different function series f1,f2,⋯,fm there
are that for every i(1≤i≤n),f1(f2(⋯fm(i)))=i.
Two function series f1,f2,⋯,fm and g1,g2,⋯,gm are
considered different if and only if there exist i(1≤i≤m),j(1≤j≤n),fi(j)≠gi(j).
The following are m lines.
In i-th
line, there is one number −1 or n space-separated
numbers.
If there is only one number −1,
the function fi is
unknown. Otherwise the j-th
number in the i-th
line means fi(j).
3 3
1 2 3
-1
3 2 1
1HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions.
/*********************************************************************/
题意:给你m个函数f1,f2,⋯,fm:{1,2,⋯,n}→{1,2,⋯,n}(即全部的x∈{1,2,⋯,n},相应的f(x)∈{1,2,⋯,n})。已知当中一部分函数的函数值,问你有多少种不同的组合使得全部的i(1≤i≤n),满足f1(f2(⋯fm(i)))=i
对于函数集f1,f2,⋯,fm
and g1,g2,⋯,gm。当且仅当存在一个i(1≤i≤m),j(1≤j≤n),fi(j)≠gi(j),这种组合才视为不同。
假设还是不理解的话,我们来解释一下例子,
3 3
1 2 3
-1
3 2 1
例子写成函数的形式就是
n=3,m=3
f1(1)=1,f1(2)=2,f1(3)=3
f2(1)、f2(2)、f2(3)的值均未知
f3(1)=3,f3(2)=2,f3(3)=1
所以要使全部的i(1≤i≤n),满足f1(f2(⋯fm(i)))=i。仅仅有一种组合情况,即f2(1)=3,f2(2)=2,f2(3)=1这么一种情况
解题思路:事实上。细致想想。你就会发现,此题的解跟-1的个数有关,当仅仅有一个-1的时候,由于相应关系都已经决定了。所以仅仅有1种可行解,而当你有两个-1时,当中一个函数的值能够依据还有一个函数的改变而确定下来,故有n!种解。
依此类推,当有k个-1时,解为
所以,我们仅仅须要提前将n!
计算好取模存下来。剩下的就是套公式了
有一点须要提醒的是,当-1的个数为0时。即不存在-1的情况,解并不一定为0,若不存在-1的情况下仍满足对于全部的i(1≤i≤n),满足f1(f2(⋯fm(i)))=i,输出1。否则输出0。所以加个dfs推断一下方案是否可行。多校的时候就被这一点坑了。看来还是考虑得不够多。
若对上述有什么不理解的地方,欢迎提出。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 105;
const int inf = 1000000000;
const int mod = 1000000007;
__int64 s[N];
bool v[N];
int w[N][N];
int dfs(int t,int x)
{
if(t==1)
return w[t][x];
return dfs(t-1,w[t][x]);
}
int main()
{
int n,m,i,j,k,x;
bool flag;
__int64 ans;
s[0]=1;
for(i=1;i<N;i++)
s[i]=(s[i-1]*i)%mod;
while(~scanf("%d%d",&n,&m))
{
flag=true;
for(k=0,i=1;i<=m;i++)
{
scanf("%d",&x);
if(x==-1)
k++;
else
{
memset(v,false,sizeof(v));
v[x]=true;w[i][1]=x;
for(j=2;j<=n;j++)
{
scanf("%d",&x);
v[x]=true;
w[i][j]=x;
}
for(j=1;j<=n;j++)
if(!v[j])
break;
if(j<=n)
flag=false;
}
}
/*for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%d ",w[i][j]);
printf("\n");
}*/
if(!flag)
puts("0");
else if(!k)
{
for(i=1;i<=n;i++)
if(dfs(m,i)!=i)
break;
//printf("%d*\n",i);
if(i>n)
puts("1");
else
puts("0");
}
else
{
for(ans=1,i=1;i<k;i++)
ans=ans*s[n]%mod;
printf("%I64d\n",ans);
}
}
return 0;
}
菜鸟成长记
HDU 5399 Too Simple(过程中略微用了一下dfs)——多校练习9的更多相关文章
- HDU 5399 Too Simple (2015年多校比赛第9场)
1.题目描写叙述:点击打开链接 2.解题思路:本题分情况讨论.比赛时候真是想的太简单了.以为就是(n!)^(cnt-1). 终于无限WA. 本题有几个特殊情况须要额外推断. 首先,假设输入的时候.有某 ...
- lua解析脚本过程中的关键数据结构介绍
在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础.在整个过程中,比较重要的几个源码文件分别是:llex.h,lparse.h.l ...
- 使用javamail发信过程中的一些问题及解决方法
http://www.blogjava.net/TrampEagle/archive/2006/05/26/48326.html 今天在研究javamail发信的过程中,出现了一些小问题,现总结如下, ...
- HDU 5795 A Simple Nim(简单Nim)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
- Apache commons email 使用过程中遇到的问题
apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...
- <转>lua解析脚本过程中的关键数据结构介绍
在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础.在整个过程中,比较重要的几个源码文件分别是:llex.h,lparse.h.l ...
- cairosvg使用过程中需要注意的问题
在使用pygal的过程中,图片默认保存的是svg格式,如果需要生成本地的图片需要进行一些配置.下面是在摸索时的一些流程: 1.查看pygal的函数,dir(pygal.bar),发现其支持保存为png ...
- pip install 执行过程中遇到的各种问题
一.pip install 安装指定版本的包 要用 pip 安装指定版本的 Python 包,只需通过 == 操作符 指定. pip install robotframework == 2.8.7 将 ...
随机推荐
- Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 2012 Backup
Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 201 ...
- cocos2d-x3.0 RichText
.h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...
- 通过微软的HTML Help Workshop 利用.html文件 生成简单的chm帮助类的文件
1.下载并安装Microsoft HTML Help Workshop 下载链接:http://www.microsoft.com/en-us/download/details.aspx?id=211 ...
- Java发邮件带附件(且重命名附件)
环境:spring3.2.2+jquery 用户的附件管理要实现发送附件可以是单个也可以是多个.由于用户在上传附件的时候采用了重命名机制,所以存在服务器上的文件是重命名后的文件,如果用户要将文件以邮件 ...
- [翻译] The Amazing Audio Engine
The Amazing Audio Engine https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine The Amazing ...
- Dom4j学习笔记
一.Loading XML Data 以下代码从File中或一个URL中读取一个XML文件,并产生一个Document对象.一个Document对象表示了内存中的一棵XML树,可以在这个XML树中进行 ...
- 《Windows核心编程》第五章——作业
#include <windows.h> #include<iostream> #include <tchar.h> using namespace std; ty ...
- TCP的三次握手与四次挥手(详解+动图)
背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之间的通信.但是这并不具体,因为,真正进行通信的实体是在主机中的进程,是一个主机中的一个进程与另外一个主机中的一个进程在 ...
- 如何让Oracle表及字段显示为区分大小写(转)
http://www.itpub.net/thread-1703955-1-1.html
- 角点检测:Harris角点及Shi-Tomasi角点检测
角点 特征检测与匹配是Computer Vision 应用总重要的一部分,这需要寻找图像之间的特征建立对应关系.点,也就是图像中的特殊位置,是很常用的一类特征,点的局部特征也可以叫做“关键特征点”(k ...