【HDU 5399】Too Simple
2016-02-06 21:21
495 查看
题
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(i≤1≤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)
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)
1 2 3
-1
3 2 1
但是要注意只有一个-1时,答案可能是0或1,而当f里1..n没有全部出现时,即有重复数字时,答案也是0。
这题说是too simple,然而好多坑啊!样例只有一组数据,但是实际上可能有多组数据,除此,要注意每次处理新的一组时,哪些变量要清零,还有这题要用long long,n阶乘可以在一开始初始化。
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(i≤1≤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 31 2 3
-1
3 2 1
Sample Output
1Hint
The order in the function series is determined. What she can do is to assign the values to the unknown functions.
题意:
求满足f1(f2(...(fm(i))...))=i的未知的函数有多少种可能。分析:
因为只要最后的那个fi确定了,其它的任意都可以,答案是(n!)^(m-1)再mod 109+7,m为-1的个数但是要注意只有一个-1时,答案可能是0或1,而当f里1..n没有全部出现时,即有重复数字时,答案也是0。
这题说是too simple,然而好多坑啊!样例只有一组数据,但是实际上可能有多组数据,除此,要注意每次处理新的一组时,哪些变量要清零,还有这题要用long long,n阶乘可以在一开始初始化。
代码:
#include<stdio.h> #define M 1000000007LL #define ll long long #define N 105 #define F(a,b,c) for(int a=b;a<=c;a++) ll n,m,d,f ,y ,jc ={1,1},ans; int main() { F(i,2,100)jc[i]=jc[i-1]*i%M;//初始化阶乘 while(~scanf("%lld%lld",&n,&m)) { d=0;ans=1;//初始化 F(i,1,m) { scanf("%lld",&f[i][1]); if(f[i][1]==-1)d++; else F(j,2,n) { scanf("%lld",&f[i][j]); if(ans)F(k,1,j-1) if(f[i][j]==f[i][k])ans=0; } } if(ans) { if(d==0) { F(i,1,n)y[i]=i; for(int i=m; i; i--) F(j,1,n)y[j]=f[i][y[j]];//一层层推到f1 F(i,1,n&&ans)if(y[i]!=i)ans=0; } else F(i,1,d-1)ans=ans*jc %M; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- c3p0数据库连接池的使用详解
- 表格SQL参数定义
- linux系统的ssh服务开启方法
- junit4X系列--Assert与Hamcrest
- ACCESS网站示例-连载-数据库备份与还原类文件
- 刷题、OJ 1330 数字密码发生器
- Flask 随记
- 河南第四届ACM省赛(BOBSLEDDING)
- 表格属性定义
- linux内核数据结构-哈希表理解
- 算法设计思想
- poj 1111 Image Perimeters
- Android Studio一步步教你集成发布适配
- django 自定义 密码加密方式 及自定义验证方式
- ACCESS网站示例-连载-数据库操作类文件
- 菜鸟读JAVA²核心技术卷Ⅱ:高级特性(原书第7版)- 线程
- Project甘特图使用方法
- C字符串练习题心得
- azure云迁移后expdp报错ORA-01110: data file 201: '/home/oradata/powerdes/temp01.dbf'
- Spider 之 深入解析 Python 正则表达式