Too Simple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai thought this problem was too simple, sometimes naive. So she ask you for help.



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).
 
Input
For each test case, the first lines contains two numbers n,m(1≤n,m≤100).



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).
 
Output
For each test case print the answer modulo 109+7.
 
Sample Input
3 3
1 2 3
-1
3 2 1
 
Sample Output
1
Hint
The 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的更多相关文章

  1. HDU 5399 Too Simple (2015年多校比赛第9场)

    1.题目描写叙述:点击打开链接 2.解题思路:本题分情况讨论.比赛时候真是想的太简单了.以为就是(n!)^(cnt-1). 终于无限WA. 本题有几个特殊情况须要额外推断. 首先,假设输入的时候.有某 ...

  2. lua解析脚本过程中的关键数据结构介绍

    在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础.在整个过程中,比较重要的几个源码文件分别是:llex.h,lparse.h.l ...

  3. 使用javamail发信过程中的一些问题及解决方法

    http://www.blogjava.net/TrampEagle/archive/2006/05/26/48326.html 今天在研究javamail发信的过程中,出现了一些小问题,现总结如下, ...

  4. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

  6. Apache commons email 使用过程中遇到的问题

    apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...

  7. <转>lua解析脚本过程中的关键数据结构介绍

    在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础.在整个过程中,比较重要的几个源码文件分别是:llex.h,lparse.h.l ...

  8. cairosvg使用过程中需要注意的问题

    在使用pygal的过程中,图片默认保存的是svg格式,如果需要生成本地的图片需要进行一些配置.下面是在摸索时的一些流程: 1.查看pygal的函数,dir(pygal.bar),发现其支持保存为png ...

  9. pip install 执行过程中遇到的各种问题

    一.pip install 安装指定版本的包 要用 pip 安装指定版本的 Python 包,只需通过 == 操作符 指定. pip install robotframework == 2.8.7 将 ...

随机推荐

  1. C#高性能大容量SOCKET并发(转)

    C#高性能大容量SOCKET并发(零):代码结构说明 C#高性能大容量SOCKET并发(一):IOCP完成端口例子介绍 C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs ...

  2. 用Qemu模拟vexpress-a9 (六) --- 多核

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  3. 【MySQL案例】mysql-libs-5.1.73-3.el6_5.x86_64 conflicts with file from package Percona-Server-server

    假设遇到mysql-libs-5.1.73-3.el6_5.x86_64 conflicts with file from package Percona-Server-server报错,有两种情况导 ...

  4. 为 Tomcat 安装 apr

    apr 官方介绍: Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术. APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTT ...

  5. JavaScript 面向对象编程之一

    一:Class and private And public JS 中的类以 function 进行声明,同时 JS 也支持声明私有 private 和公有 public 成员,只不过跟 C# 不一样 ...

  6. 局部敏感哈希 Kernelized Locality-Sensitive Hashing Page

    Kernelized Locality-Sensitive Hashing Page   Brian Kulis (1) and Kristen Grauman (2)(1) UC Berkeley ...

  7. Java 从基础到进阶学习之路---类编写以及文档凝视.

    Java之前在学习过,基础知识还没有忘光,并且这些高级语言实在是太像,所以那些数据类型,或者循环控制流,以及标准设备等等就直接略过不说了. 只是一些重大概念会穿插在文章的介绍中. So,这些文章适合于 ...

  8. scala编程第17章学习笔记(2)——集和映射

    默认情况下在使用“Set”或“Map”的时候,获得的都是不可变对象.如果需要的是可变版本,需要先写明引用. 如果同一个源文件中既要用到可变版本,也要用到不可变版本的集合或映射,方法之一是引用包含了可变 ...

  9. html调用servlet(JDBC在Servlet中的使用)(1)

    1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...

  10. 企业版Oracle10g的安装-过程

    ylbtech-Oracle:企业版Oracle10g的安装-过程 Oracle10g的安装 在Windows操作系统上安装Oracle10g数据库的步骤如下: 0.1)从Oracle的官方网站上下载 ...