题意:
给你一个a序列,代表0到n-1的排列;一个b序列代表0到m-1的排列。问你可以找出多少种函数关系,满足f(i)=b[f(a[i])];
分析:这个主要是找循环节
比如说:如果 a 序列是 2 0 1 那么我们可以发现
f(0) = b[f(a[0])] = b[f(2)]
f[1] = b[f(a[1])] = b[f(0)]
f[2] = b[f(a[2])] = b[f(1)]
那么f(0) f(1) f(2) 也是循环的 如果想找出这样的函数,必须值域里也存在同样长度的循环节或者存在其约数长度的循环节。
那么就是找两个序列的循环节,对于定义域里面的每一个循环节都找出来有多少种和他对应的。最后乘起来就是答案了
#include<cstdio>
#include<map>
#include<cstring>
#define mo 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
ll a[maxn];
ll b[maxn];
ll num1[maxn], num2[maxn];
int main(){
int n, m, kase = 0;
while(~scanf("%d%d", &n, &m)){
memset(num2, 0, sizeof(num2));
int totfa = 0;
for(int i = 0; i < n; i++)
scanf("%lld", &a[i]);
for(int i = 0; i < n; i++){
ll tot = 0;
int k = i;
while(a[k] != -1){
tot++;
int t = k;
k = a[k];
a[t] = -1;
// tot++;
}
if(tot)num1[++totfa] = tot;
}
for(int j = 0; j < m; j++)
scanf("%lld", &b[j]);
for(int i = 0; i < m; i++){
ll tot = 0;
int k = i;
while(b[k] != -1){
tot++;
int t = k;
k = b[k];
b[t] = -1;
// tot++;
}
if(tot) num2[tot]++;
}
ll ans = 1;
for(int i = 1; i <= totfa; i++){
ll ansl = 0;
for(int j = 1; j * j <= num1[i]; j++){
if(num1[i] % j == 0){
if(j * j == num1[i]){
ansl += num2[j] * j;
}else{
ansl += num2[j] * j + num2[num1[i] / j] * num1[i] / j;
}
}
}
ans = (ans * ansl) % mo;
}
printf("Case #%d: %lld\n", ++kase, ans);
}
}

题目要求找到满足f(i)=b[f(a[i])]的函数关系,其中a和b是两个排列序列。关键在于找到序列的循环节,并确定值域内的对应循环节。解题策略是找出两个序列的所有循环节,并计算每一种定义域循环节能对应多少值域循环节,最后将这些可能性相乘得到答案。
945

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



