HDU6038-Function(置换群)

探讨给定两个排列a和b时,如何计算满足特定条件f(i)=b(f(a[i]))的不同函数f的数量。通过分析循环节特性,利用约数关系进行有效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Function

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


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 from  0  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 2 1 0 2 0 1 3 4 2 0 1 0 2 3 1
 

Sample Output
  
Case #1: 4 Case #2: 4
 

Source
 

题意:给两个数列a(元素个数为n)和b(元素个数为m),求满足f(i) = b[f(a[i])] 的组合情况有多少种

题解:由第二个样例:

对于这组数来说,假如我们先指定了f(0)对应的在b中的值,那么根据第2个式子,就可以得出f(1),根据f(1)就又可以得出f(2),最后根据f(2)就可以检验f(0)的值是否正确。

这也就是说,对于a中的一个循环节,只要确定了其中一个数所映射的值,那么其它数就都被相应的确定了。


所以需要先计算出a和b中的循环节个数和每个循环节对应的个数,然后根据循环节的约数关系就可以判断是否成立

答案就是:当b中的循环节的长度是a的循环节长度的约数的时候,a循环节可以指定数字的个数是b的循环节长度

这里有一个规律就是:0~n 的排列,是一定存在循环节的,而且多个循环节是不会有交叉的,所以最后的结果应该相乘



#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <map>  
#include <cmath>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  
using namespace std;
#define LL long long  
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;

int n, m;
int a[1000009], b[1000009];
int xa[1000009], xb[1000009];

struct node
{
	int x, sum;
}x[1000009], y[1000009];

LL mpow(LL x, LL y, LL p)
{
	LL ans = 1;
	while (y)
	{
		if (y & 1) ans = (ans * x) % p;
		x = (x * x) % p;
		y >>= 1;
	}
	return ans;
}

int main()
{
	int cas = 0;
	while (~scanf("%d %d", &n, &m))
	{
		for (int i = 0; i < n; i++) scanf("%d", &a[i]);
		for (int i = 0; i < m; i++) scanf("%d", &b[i]);
		int cnt1 = 0, cnt2 = 0;
		for (int i = 0; i < n; i++)
		{
			if (a[i] == -1) continue;
			int cnt = 0;
			if (a[i] != -1)
			{
				int k = a[i];
				while (a[k] != -1)
				{
					cnt++;
					int kk = k;
					k = a[k];
					a[kk] = -1;
				}
			}
			xa[cnt1++] = cnt;
		}
		for (int i = 0; i < m; i++)
		{
			if (b[i] == -1) continue;
			int cnt = 0;
			if (b[i] != -1)
			{
				int k = b[i];
				while (b[k] != -1)
				{
					cnt++;
					int kk = k;
					k = b[k];
					b[kk] = -1;
				}
			}
			xb[cnt2++] = cnt;
		}
		sort(xa, xa + cnt1);
		sort(xb, xb + cnt2);
		int tot1 = -1,tot2=-1;
		for (int i = 0; i<cnt1; i++)
		{
			if (i == 0 || xa[i] != xa[i - 1]) x[++tot1].x = xa[i], x[tot1].sum = 1;
			else x[tot1].sum++;
		}
		for (int i = 0; i<cnt2; i++)
		{
			if (i == 0 || xb[i] != xb[i - 1]) y[++tot2].x = xb[i], y[tot2].sum = 1;
			else y[tot2].sum++;
		}
		LL ans = 1;
		for (int i = 0; i <= tot1; i++)
		{
			LL sum = 0;
			for (int j = 0; j <= tot2&&x[i].x>=y[j].x; j++)
				if (x[i].x % y[j].x == 0) sum = (sum + 1LL * y[j].x * y[j].sum) % mod;
			ans = (ans * mpow(sum, x[i].sum, mod)) % mod;
		}
		printf("Case #%d: %lld\n", ++cas, ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值