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 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)=b
f(a
i
)![]()
for each i
from 0
to n−1
.
Two functions are different if and only if there exists at least one integer from0
to n−1
mapped into different integers in these two functions.
The answer may be too large, so please output it in modulo 10
9
+7
.
Define that the domain of function f
Please calculate the quantity of different functions f
Two functions are different if and only if there exists at least one integer from0
The answer may be too large, so please output it in modulo 10
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 a
i−1![]()
.
The third line contains m
numbers, ranged from 0
to m−1
,
the i
-th
number of which represents b
i−1![]()
.
It is guaranteed that ∑n≤10
6
,
∑m≤10
6![]()
.
For each case:
The first line contains two numbers n,
The second line contains n
The third line contains m
It is guaranteed that ∑n≤10
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
给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;
}
本文介绍了一种算法,用于解决给定两个排列数组时如何计算满足特定条件的不同函数数量的问题。通过寻找数组中的环并利用这些环的特性进行计算,最终得出答案。
1004

被折叠的 条评论
为什么被折叠?



