HDU6038 & 2017 Multi-University Training Contest - Team 1

Function

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

这道题就是想两个数列n,m;问有几种方法能使f(i)=f(ai)=f(f(ai))成立,也就是寻找两个数列中的循环节,这里可以把循环节想象成两个大小不一的环,如果要使一个n的循环节n[i]和一个m的循环节m[j]成立,就要使n[i]%m[j]成立,这样一个n[i]对应多个m[j]时,两者的每个数都可以相互对应,,因为有可能n,m有很多循环节,所以成立条件要相乘

#include<bits/stdc++.h>
using namespace std;
using LL=int64_t;
const int maxn=1e6+5;
const int mod=1e9+7;
int x[maxn],y[maxn];
vector<int> A,B;
bool vis[maxn];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m,kase=1;
    while(cin>>n>>m){
        for(int i=0;i<n;i++) cin>>x[i];
        for(int i=0;i<m;i++) cin>>y[i];
        memset(vis,false,sizeof(vis));
        A.clear();
        for(int i=0;i<n;i++) {
            int sum=1,cnt=i;
            if(!vis[i]) {
                vis[i]=true;
                while(x[cnt]!=i) {
                    cnt=x[cnt];
                    vis[cnt]=true;
                    sum++;
                }
                A.push_back(sum);
            }
        }
        memset(vis,false,sizeof(vis));
        B.clear();
        for(int i=0;i<m;i++) {
            int sum=1,cnt=i;
            if(!vis[i]) {
                vis[i]=true;
                while(y[cnt]!=i) {
                    cnt=y[cnt];
                    vis[cnt]=true;
                    sum++;
                }
                B.push_back(sum);
            }
        }
        LL sum=1;
        for(int i=0;i<A.size();i++) {
            LL temp_sum=0;
            for(int j=0;j<B.size();j++) {
                if(A[i]%B[j]==0) {
                    temp_sum=(temp_sum+B[j])%mod;
                }
            }
        sum=(sum*temp_sum)%mod;
        }
        cout<<"Case #"<<kase++<<": "<<sum<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值