2017多校1 1006Function

来源:互联网 发布:微博域名修改 编辑:程序博客网 时间:2024/05/24 05:55

Function

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


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 from0 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 #x:y" 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
 


给2组数求不同函数关系的种类,根据题目的意思,就是求2组数列中不同环的数量,因为根据多对一的关系,将第二个数组中将 是第一个数组中不用数量的环的因子数的环相加,再最终将结果相乘即可。可以用vector容器来存不同数量的环



#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<iomanip>#include<queue>#include<vector>using namespace std;const long long int MOD=7+1e9;vector<int> al,bl;int a[111111],b[111111];bool v[111111];int main(){    int n,m;    int cs=0;    ios::sync_with_stdio(false);    while(cin>>n>>m)    {        al.clear();        bl.clear();        int i;        for(i=0;i<n;i++)        cin>>a[i];        for(i=0;i<m;i++)        cin>>b[i];        memset(v,0,sizeof(v));        for(i=0;i<n;i++)        {            if(!v[i])            {                int now=a[i];                int len=0;                while(!v[now])                {                    v[now]=1;                    len++;                    now=a[now];                }                al.push_back(len);            }        }        memset(v,0,sizeof(v));        for(i=0;i<m;i++)        {            if(!v[i])            {                int now=b[i];                int len=0;                while(!v[now])                {                    v[now]=1;                    ++len;                    now=b[now];                }                bl.push_back(len);            }        }        int j;        long long int ans=1;        for(i=0;i<al.size();i++)        {            long long int ans1=0;            for(j=0;j<bl.size();j++)             if(al[i]%bl[j]==0)              ans1=(ans1+bl[j])%MOD;              ans=(ans*ans1)%MOD;        }        cout<<"Case #"<<++cs<<": ";        cout<<ans%MOD<<endl;    }    return 0;} 


原创粉丝点击