HDU5399-多校-模拟

Too Simple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1214    Accepted Submission(s): 406


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.
 
 
一开始看起来很复杂,想了想发现只要保证最后一个函数是任意的,中间的其他函数都没问题。
但是有一个坑的情况是,可能一个任意函数也没有,全都是固定的函数,这时要验证这组函数是否可行。我们当时验证的顺序弄反,卡了一场,真是too simple。
最近做的题都是模拟题啊,,,太没技术含量了,,,,
 
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <ctype.h>
 5 #include <cstdlib>
 6 #include <stack>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <string>
11 #include <cmath>
12 
13 using namespace std;
14 const long long MOD = 1e9+7;
15 
16 int N,T,M;
17 int func[200][200],vis[200];
18 long long nn;
19 
20 long long qpow(long long a,long long i,long long n)
21 {
22     if(i == 0) return 1 % n;
23     long long temp = qpow(a,i>>1,n);
24         temp = temp * temp % n;
25     if( i&1 ) temp = temp * a % n;
26     return temp;
27 }
28 
29 long long mi(long long a,int t)
30 {
31     long long ans = 1;
32     for(int i=0;i<t;i++) {ans *= a;ans %= MOD;}
33     return ans;
34 }
35 
36 long long solve()
37 {
38     for(int i=1;i<=N;i++)
39     {
40         int ans = i;
41         for(int j=M-1;j>=0;j--)
42         {
43             ans = func[j][ans-1];
44         }
45         if(ans != i) return 0LL;
46     }
47     return 1LL;
48 }
49 
50 int main()
51 {
52     while(~scanf("%d%d",&N,&M))
53     {
54         long long cnt = 0LL,ans = 0LL;
55         nn = 1LL;
56         int flag = 0;
57         for(int i=1; i <= N;i++) {nn *= i; nn %= MOD;}
58 
59         for(int i=0;i<M;i++)
60         {
61             memset(vis,0,sizeof vis);
62             if(scanf("%d",&func[i][0]) && (func[i][0] == -1))
63             {
64                 cnt++;
65             }
66             else 
67             {
68                 vis[func[i][0]]++;
69                 for(int j=1;j<N;j++) 
70                 {
71                     scanf("%d",&func[i][j]);
72                     if( vis[func[i][j]] ) flag = 1;
73                     else vis[func[i][j]]++;
74                 }
75             }
76         }
77 
78         if(flag) ans = 0LL;
79         else if(cnt > 0) { ans = mi(nn,cnt-1); ans %= MOD;}
80         else
81         {
82             ans = solve();
83         }
84 
85         printf("%I64d\n",ans%MOD);
86     }
87 }

 

 
posted @ 2015-09-05 18:08  Helica  阅读(200)  评论(0)    收藏  举报