HDOJ 5399 Too Simple
每个函数都必须是一个排列,经过连续的一段确定函数后数字不能少.
满足上面的条件的话,仅仅要有一个-1函数特别的排列一下就能够满足要求,剩下的能够任意填
没有-1的话特判
Too Simple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 789 Accepted Submission(s): 267
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.
/* ***********************************************
Author :CKboss
Created Time :2015年08月19日 星期三 10时25分44秒
File Name :HDOJ5399.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL;
const LL mod=1e9+7LL;
const int INF=50000000; int n,m;
int f[110][110]; int cn;
int color[110]; bool COLOR(int x,int c)
{
if(color[x]==c) return false;
color[x]=c; cn++;
return true;
} LL QuickPow(LL a,LL n)
{
LL e=1;
while(n)
{
if(n&1) e=(a*e)%mod;
a=(a*a)%mod;
n/=2;
}
return e%mod;
} bool used[110]; LL jc(LL n)
{
LL ret=1;
for(int i=2;i<=n;i++)
ret=(ret*i)%mod;
return ret%mod;
} bool check_P(int x,int L=1,int R=m)
{
if(used[x]==true) return false;
int nx=x;
for(int i=R;i>=L;i--)
{
nx=f[i][nx];
}
if(nx==x)
{
if(used[x]==false)
{
used[x]=true;
return true;
}
else return false;
}
return false;
} bool check_Range(int L,int R)
{
memset(used,false,sizeof(used));
bool flag=true;
for(int i=1;i<=n&&flag;i++)
{
int nx=i;
for(int j=R;j>=L;j--)
{
nx=f[j][nx];
}
if(used[nx]==true) return false;
else used[nx]=true;
}
return true;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF)
{
int nig=0;
bool check=true;
memset(color,0,sizeof(color)); for(int i=1;i<=m;i++)
{
scanf("%d",&f[i][1]);
if(f[i][1]==-1) nig++;
else
{
cn=0;
check=COLOR(f[i][1],i);
for(int j=2;j<=n;j++)
{
scanf("%d",&f[i][j]);
check=COLOR(f[i][j],i);
}
if(cn!=n) check=false;
}
} if(check==false)
{
puts("0");
}
else if(check&&nig==0)
{
/// tePan
memset(used,false,sizeof(used));
bool flag=true;
for(int i=1;i<=n&&flag;i++)
{
if(check_P(i)==false) flag=false;
}
if(flag==true) puts("1");
else puts("0");
}
else
{
//// (n!^(nig-1))
bool flag=true;
int Left=INF,Right=-INF;
f[m+1][1]=-1;
for(int i=1;i<=m+1&&flag;i++)
{
if(f[i][1]==-1)
{
if(Left!=INF&&Right!=-INF)
{
/// check;
flag=check_Range(Left,Right);
}
Left=INF; Right=-INF;
}
else
{
Left=min(Left,i); Right=max(Right,i);
}
}
if(flag==false) puts("0");
else printf("%lld\n",QuickPow(jc(n),nig-1)%mod);
}
} return 0;
}
HDOJ 5399 Too Simple的更多相关文章
- hdoj 5399 Tpp simple
WA了一下午.... 1WA:T了,因为阶乘没打表所以时间超了.. 2WA,3WA:runtime error,检查的value数组开小了,应该是MAXN.. 4WA.5WA.6WA:改了改对cnt的 ...
- 构造 HDOJ 5399 Too Simple
题目传送门 题意:首先我是懂了的,然后我觉得很难讲清楚就懒得写了,关键理解f1(f2(fm(i)))=i,不懂的戳这里构造:如果fi(j)不是映射到(1~n),重复或者不在范围内的肯定无解.还有没有- ...
- 【HDOJ 5399】Too Simple
pid=5399">[HDOJ 5399]Too Simple 函数映射问题 给出m函数 里面有0~m个函数未知(-1) 问要求最后1~n分别相应仍映射1~n 有几种函数写法(已给定的 ...
- HDOJ 4974 A simple water problem
A simple water problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- HDU 5399 Too Simple(过程中略微用了一下dfs)——多校练习9
Too Simple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Probl ...
- HDOJ 4975 A simple Gaussian elimination problem.
和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...
- 【HDOJ】2451 Simple Addition Expression
递推,但是要注意细节.题目的意思,就是求s(x) = i+(i+1)+(i+2),i<n.该表达中计算过程中CA恒为0(包括中间值)的情况.根据所求可推得.1-10: 31-100: 3*41- ...
- 【HDOJ】1341 Simple Computers
注意PC要与31. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 40 ...
- 【HDOJ】1497 Simple Library Management System
链表. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 1001 #def ...
随机推荐
- head---显示文件的开头的内容
head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容. 语法 head(选项)(参数) 选项 -n<数字>:指定显示头部内容的行数: -c<字符数& ...
- [CQOI2013]新Nim游戏(线性基)
P4301 [CQOI2013]新Nim游戏 题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴. ...
- UnrealEngine4编码风格的思考
第一次拿到UE4源码,扫了一遍.各种宏定义,各种模板,各种类层次.杂乱无章. 后来慢慢明确其规律: UE4的编码风格是在匈牙利命名法的基础下做了改进,使其更适用游戏引擎业务(业务特点:数据可视编辑.脚 ...
- new期间的异常
new包含两步,调用operator new申请空间,以及调用构造函数. 如果第一步结束之后,第二步发生异常,需要归还第一步的空间. 编译器帮我们做了这件事情,并且会调用对应的delete. 另外 n ...
- 【HDU 5402】Travelling Salesman Problem(构造)
被某题卡SB了,结果这题也没读好...以为每一个格子能够有负数就当搜索做了.怎么想也搜只是去,后来发现每一个格子是非负数,那么肯定就是构造题. 题解例如以下: 首先假设nn为奇数或者mm为奇数,那么显 ...
- MountService整理
刚毕业时第一个接触的模块就是Vold.这个模块尽管小,但深入下去是有一定难度的. 花了点时间又一次整理了下这一块的逻辑,也当温习下这个模块. watermark/2/text/aHR0cDovL2Js ...
- 【Linux】JDK+Eclipse 搭建C/C++开发环境
注:本文所提供的参考示例是在CentOS Linux环境下的安装,不保证适用于其他版本的Linux系统. · 安装前的注意事项 编译源代码是需要对应的代码编译工具的,本文中安装的Eclipse只 ...
- 35.QQ大数据模型
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- HDU2665 kth number 线段树做法
题意:求区间第k小 思路: 线段树 每个节点上保存 当前区间已经排序好的序列 (归并一下就好了嘛 复杂度 O(l)的) 这样建树的时空复杂度都是 O(nlogn)的 对于 每次询问 二分一个答案 在树 ...
- POJ 1293 网络流 第一题
完全的模板,做多了就好了吧 反向流量真的很有意思,有这样一种说法比较容易理解.”正向是+,反向就是-,其实是等价的.因为每次找到的增广路不一定是最优解里面的,所以再进行后面的操作的时候要重新选择,而反 ...