2017 Multi-University Training Contest

来源:互联网 发布:淘宝联盟能用电脑吗 编辑:程序博客网 时间:2024/04/30 23:07
欢迎参加——2017"百度之星"程序设计大赛
欢迎参加——2017“云上贵州”创新大赛 !

Function

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


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

a[]的排列确定出f[i]的递推关系,构成几个递推环,b[]的排列构成答案环,如果b[]中的环长度能作为a[]环的因子,那么b[]环作为结果代入,就没有矛盾

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const long long MOD= 1e9+7;int n,m;int a[111111],b[111111];int f[111111];vector<int>Va,Vb;int ncase=0;int main(){    while(scanf("%d%d",&n,&m)==2)    {        Va.clear();Vb.clear();        for(int i=0;i<n;i++)scanf("%d",&a[i]);        for(int i=0;i<m;i++)scanf("%d",&b[i]);        memset(f,-1,sizeof(f));        for(int i=0;i<n;i++)        {            if(f[a[i]]==-1)            {                //f[a[i]]=0;                int cnt=0;                int k=a[i];                while(f[a[k]]==-1)                {                    f[a[k]]=0;                    k=a[k];                    cnt++;                }                if(cnt){Va.push_back(cnt);}            }        }        //for(int i=0;i<Va.size();i++)cout<<Va[i]<<" ";        memset(f,-1,sizeof(f));        for(int i=0;i<m;i++)        {            if(f[b[i]]==-1)            {                //f[a[i]]=0;                int cnt=0;                int k=b[i];                while(f[b[k]]==-1)                {                    f[b[k]]=0;                    k=b[k];                    cnt++;                }                if(cnt){Vb.push_back(cnt);}            }        }        sort(Va.begin(),Va.end());        sort(Vb.begin(),Vb.end());        int lena=Va.size();        int lenb=Vb.size();        long long ans=1;        int  flag=0;        for(int i=0;i<lena;i++)        {            long long mul=0;            for(int j=0;j<lenb;j++)            {                if(Va[i]<Vb[j])break;                if(Va[i]%Vb[j]==0){mul+=Vb[j];}            }            if(mul){flag++;ans=ans*mul%MOD;}            else {ans=0;break;}        }        //if(flag<lena)ans=0;        printf("Case #%d: %lld\n",++ncase,ans);    }    return 0;}