Function
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1491 Accepted Submission(s): 700
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
题目大意:给你两个数列A,B让你求满足f(i)=bf(ai)的函数f有多少个。当f中至少有一个函数值不同那么这两个函数就为不同的函数。
解题思路:观察f(i)=bf(ai)这个式子可以根据ai写出n个式子根据这个式子可以画出数列下表i与ai构成的环和bi与下表i所构成的环。ai所构成的环的总数就是f(i)=bf(ai)的所有的对应关系而bi所构成的换就是f与bi所满足的关系下面就是找对应每一个ai的环有多少种bi构成的环满足其关系数,再把这些关系数相乘就是最后的答案。
题解:
AC代码:
#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; //A数列中每个环的元素的个数也就是这个环的长度
cnt++; //A数列中环的个数
}
}
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);///A数列中环的个数
int cntb=getp(b,B,m);///B数列中环的个数
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;
}