HDU 5399 Too Simple(数学 + 找规律)——2015 Multi-University Training Contest 9

227 篇文章 0 订阅
97 篇文章 0 订阅

传送门

Too Simple

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


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(1in) , 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(1im),j(1jn) , fi(j)gi(j) .
 

Input
For each test case, the first lines contains two numbers n,m(1n,m100) .

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.
 

Author
xudyh
 

Source

题目大意:

这个题目的意思是很重要的,如果一旦读错了那这个题目就可以宣布不用做啦,刚开始我们就是读错题了,所以就…,后来通过上网看一下题解结果发现题目读错了。。。

题目的意思是:现在有 m {1,2...n}  {1,2...,n} 的映射,其中有的映射是已经知道的,还有一些是未知的记作 1 ,求对任意

i{1,2,...,n}f1(f2(...(fm(i)))=i 的方案总数。

解题思路;

其实这个题目经过观察之后就会发现,基本上方案数只与 1 的个数有关系,因为当 m1 个方程确定完之后,剩下的一个方程肯定就是确定的了,

也就是说假设 1 的个数是 cnt 那么答案就是 (N!)cnt1 ,然后特判一下,因为这个函数必须是 1n 的,如果不是这样的就是直接输出 0

cnt = 0 的时候需要特判一下, f1(f2(...(fm(i)))=i ,从内到外一次判断一下就行了。

/**
2016 - 09 - 01 晚上
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1e9+5;
const int MAXN = 1e2+5;
const LL MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
    LL res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}

void Out(LL a)///输出外挂
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}
LL fac[MAXN];
void Init()
{
    fac[0] = 1;
    fac[1] = 1;
    for(LL i=2; i<MAXN; i++)
        fac[i] = (fac[i-1]*i)%MOD;
}
LL quick_mod(LL a, LL b)
{
    LL ans = 1;
    while(b)
    {
        if(b & 1)
            ans = (ans*a)%MOD;
        b>>=1;
        a = (a*a)%MOD;
    }
    return ans;
}


int a[MAXN][MAXN];
int vis[MAXN], ans[MAXN];
int main()
{
    Init();
    int n, m;
    while(cin>>n>>m)
    {
        LL cnt = 0;
        for(int i=1; i<=m; i++)
        {
            cin>>a[i][1];
            if(a[i][1] == -1)
            {
                cnt++;
                continue;
            }
            for(int j=2; j<=n; j++)
                cin>>a[i][j];
        }
        for(int i=1; i<=m; i++)
        {
            memset(vis, 0, sizeof(vis));
            if(a[i][1] == -1)
                continue;
            for(int j=1; j<=n; j++)
                vis[a[i][j]] = 1;
            for(int i=1; i<=n; i++)
                if(!vis[i])
                {
                    puts("0");
                    goto endW;
                }
        }
        if(cnt == 0)///没有 -1 的时候
        {
            for(int i=1; i<=n; i++)
                ans[i] = i;
            for(int i=m; i>0; i--)
                for(int j=1; j<=n; j++)
                    ans[j] = a[i][ans[j]];
            for(int i=1; i<=n; i++)
            {
                if(ans[i] != i)
                {
                    puts("0");
                    goto endW;
                }
            }
            puts("1");
        }
        else
        {
            LL ret = fac[n];
            ret = quick_mod(ret, cnt-1);
            cout<<ret<<endl;
        }
        endW:;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值