hdu 6038 Function(思维)

来源:互联网 发布:python telnetlib详解 编辑:程序博客网 时间:2024/06/09 17:52

Function

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 501    Accepted Submission(s): 195


Problem Description
You are given a permutation a from 0 to n1 and a permutation b from 0 to m1.

Define that the domain of function f is the set of integers from 0 to n1, and the range of it is the set of integers from 0 to m1.

Please calculate the quantity of different functions f satisfying that f(i)=bf(ai) for each i from 0 to n1.

Two functions are different if and only if there exists at least one integer from 0 to n1 mapped into different integers in these two functions.

The answer may be too large, so please output it in modulo 109+7.
 

Input
The input contains multiple test cases.

For each case:

The first line contains two numbers n, m(1n100000,1m100000)

The second line contains n numbers, ranged from 0 to n1, the i-th number of which represents ai1.

The third line contains m numbers, ranged from 0 to m1, the i-th number of which represents bi1.

It is guaranteed that n106, m106.
 

Output
For each test case, output "Case #xy" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 

Sample Input
3 21 0 20 13 42 0 10 2 3 1
 

Sample Output
Case #1: 4Case #2: 4

通过观察f[i]=bf(a[i]),如果知道了f[i]的值就知道了a[i[=i的值,就知道了bf(i)的值,如果形成环,就在b数组里用同样的方法找环并且环的大小是a的环的约数

即可以使a中的环成立即为一种方案,由于是定义域到值域的映射,每一个x->y是不同的,所以一定会有环


#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <bits/stdc++.h>using namespace std;const int N = 1e6+7;typedef long long LL;const LL mod = 1e9+7;vector<int>p1[N],p2[N];LL a[N], b[N];int d1[N], d2[N];int vis1[N], vis2[N], cnt1, cnt2;void dfs1(int u,int step,int fa){    vis1[u]=1, d1[u]=step;    for(int i=0;i<p1[u].size();i++)    {        int v=p1[u][i];        //if(v==fa) continue;        if(vis1[v])        {            a[cnt1++]=d1[u]-d1[v]+1;        }        else dfs1(v,step+1,u);    }    return ;}void dfs2(int u,int step,int fa){    vis2[u]=1, d2[u]=step;    for(int i=0;i<p2[u].size();i++)    {        int v=p2[u][i];       // if(v==fa) continue;        if(vis2[v])        {            b[cnt2++]=d2[u]-d2[v]+1;        }        else dfs2(v,step+1,u);    }    return ;}int main(){    int ncase=1;    int n, m;    while(scanf("%d %d", &n, &m)!=EOF)    {        for(int i=0;i<=max(n,m);i++) p1[i].clear(),p2[i].clear();        for(int i=0;i<n;i++)        {            int x;            scanf("%d", &x);            //p1[x].push_back(i);            p1[i].push_back(x);        }        for(int i=0;i<m;i++)        {            int x;            scanf("%d", &x);            //p2[x].push_back(i);            p2[i].push_back(x);        }        memset(vis1,0,sizeof(vis1));        memset(vis2,0,sizeof(vis2));        cnt1=0, cnt2=0;        for(int i=0;i<n;i++)            if(!vis1[i]) dfs1(i,0,-1);        for(int i=0;i<m;i++)            if(!vis2[i]) dfs2(i,0,-1);        LL sum=1;        for(int i=0;i<cnt1;i++)        {            LL cnt=0;            for(int j=0;j<cnt2;j++)            {                if(a[i]%b[j]==0) cnt=(cnt+b[j])%mod;            }            sum=(sum*cnt)%mod;        }        printf("Case #%d: %lld\n",ncase++,sum);    }    return 0;}





原创粉丝点击