hdu 6038

函数映射计数问题

Function

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


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

2017 Multi-University Training Contest - Team 1


题意:给出两个排列,a和b,求满足映射的f[i]=b_f[a[i]]的个数。

其实相当于排列b也是一个映射,把两个映射转化成两个图。

因为是1-n的排列,所以根据映射关系组成的一定是一个一个的环。

为了将f的映射和b的对应起来,对于f中的每个环,当b中的环的长度为f中的环的因数时,则可以匹配。最后再组合一下答案即可。

ac代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9+7;


int n, m;


int a[maxn], b[maxn];
int vis[maxn];


vector<int> A;
vector<int> B;


int main()
{
    //freopen("in.txt","r",stdin);
    int kase=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]);


        A.clear();
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            int cur=i,cnt=1;
            if(!vis[i])
            {
                vis[i]=1;
                while(a[cur]!=i)
                {
                    cur=a[cur];
                    vis[cur]=1;
                    cnt++;
                }
                A.push_back(cnt);
            }
        }


        B.clear();
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
        {
            int cur=i, cnt=1;
            if(!vis[i])
            {
                vis[i]=1;
                while(b[cur]!=i)
                {
                    cur=b[cur];
                    vis[cur]=1;
                    cnt++;
                }
                B.push_back(cnt);
            }
        }


        ll ans=1;
        for(int i=0;i<A.size();i++)
        {
            ll tmp=0;
            for(int j=0;j<B.size();j++)
            {
                if(A[i]%B[j]==0)  tmp=(tmp+B[j])%mod;
            }
            ans=ans*tmp%mod;
        }
        printf("Case #%d: %d\n",++kase,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值