Function
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1389 Accepted Submission(s): 644
Problem Description
You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1.
Define that the domain of function f is the set of integers from 0 to n−1, and the range of it is the set of integers from 0 to m−1.
Please calculate the quantity of different functions f satisfying that f(i)=bf(ai) for each i from 0 to n−1.
Two functions are different if and only if there exists at least one integer from 0 to n−1 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. (1≤n≤100000,1≤m≤100000)
The second line contains n numbers, ranged from 0 to n−1, the i-th number of which represents ai−1.
The third line contains m numbers, ranged from 0 to m−1, the i-th number of which represents bi−1.
It is guaranteed that ∑n≤106, ∑m≤106.
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 2
1 0 2
0 1
3 4
2 0 1
0 2 3 1
Sample Output
Case #1: 4
Case #2: 4
Source
2017 Multi-University Training Contest - Team 1
题意:
给定两个数列 a 和 b, 求有多少个 f 映射满足: f(i)=bf(ai);
思路:
写了好久,结果样例画错一个群QAQ。要满足这种映射,来回之间的转换,需要用到置换群的思想,那么就需要求群和循环节,但是本题题意显然并不需要每个元素的循环节,而是把同在一个群的元素整合成一个群,这个群的长度便是这个群的循环节。合并之后,就要A映射B,当B序列某个群的循环节大于A序列某个群的循环节时,映射后的f函数最后一项是错的。换句话说,只有当B序列某个置换群循环节是A的因子,才能满足条件,B的这个子群的循环节就是此时满足映射关系的个数;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define max_n 100010
const int mod=1e9+7;
using namespace std;
typedef long long LL;
int a[max_n],b[max_n],A[max_n],B[max_n],vis[max_n];
int getp(int a[],int A[],int n) //求置换群的个数以及每个群的循环节
{
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=0;i<n;i++)
{
int t=a[i],res=0;//编号是0~n-1,所以起始res=0
if(!vis[i])
{
while(!vis[t])
{
vis[t]=1;
t=a[t];
res++;
}
A[cnt]=res; //每个群元素的个数也就是这个群的循环节
cnt++; //群的个数
}
}
return cnt;
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
int cnta=getp(a,A,n);
int cntb=getp(b,B,m);
LL ans=1;
for(int i=0;i<cnta;i++)
{
LL res=0;
for(int j=0;j<cntb;j++)
{
if(A[i]%B[j]==0)//当B序列是A序列的因子时,B序列长度就是方案数
res+=B[j];
}
ans=(ans*res)%mod;
}
static int p=1;
printf("Case #%d: %lld\n",p++,ans);
}
return 0;
}