HDU 6038 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的数列a[0…n-1]和长度为m的数列b[0…m-1],求有多少个这样的函数f使得 f(i)=bf(ai),i属于[0,n-1]。(f(ai)是b的下标)

比如第一组样例,先写出
f(0)=bf(a0)=bf(1),
f(1)=bf(a1)=bf(0),
f(2)=bf(a2)=bf(2),
再通过b的值,可以令f(0)=0,则f(1)=0,,同理f(0)=1的话,则f(1)=1。对于f(2),赋值为1或0都是可以的。
这样这些方法总结起来,就能知道总共有4种方法。
(f(0)=f{1}=f(2)=0;f(0)=f{1}=f(2)=1;f(0)=f{1}=1,f(2)=0;f(0)=f{1}=0,f(2)=1)

想法:

a数列可看作是以f(i)为自变量的函数,值域为b。
b数列可看作是以下标为自变量的函数,值域为b。

循环节长度:每种递推关系中包含的不同元素个数
循环节个数:有几种递推关系

1.因为a与b是有循环关系的,所以先找出a和b中的循环节个数及长度。
2.为了让a的值能循环得到b,应该让b循环节的长度为a循环节长度的因子
(比如len a=4,len b=2,两个两个一循环正好能结束没有剩余)。
3.对于a的某个循环节来说,结果=∑因子b循环节的长度×个数。因为a循环节之间是相互独立的,所以最后结果应该相乘得到。

优化技巧:
对于循环节,a的长度一定要比b长,而且b一定是a的因子,所以如果b不是a的因子可以直接跳过。在跑for循环的时候,以前筛选的方法可以继续使用,就是算出sqrt(i),跑循环只跑到sqrt即可得出一个因子,另一个因子就可直接通过i/j来得到,就不需要全部遍历搜寻一遍了。

最后举个栗子:
7 6
3 2 0 1 5 6 4
1 0 3 4 2 5
a的循环节有,(3 2 0 1)长度为4,个数为1;(5 6 4)长度为3,个数为1。
其中1,2为4的因子;1,3为3的因子。
结果为(1*1+2*1)*(1*1+3*1)=3*4=12。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=110000;
const int mod=1e9+7;
int arr[2][maxn];//arr[i][j]表示i数组中构成的循环节长度为j的个数
int a[maxn],b[maxn];
long long ans;
int main()
{
    int n,m;
    long long cnt;
    int cas=1;
    while(~scanf("%d %d",&n,&m))
    {
        memset(arr,0,sizeof arr);
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        for(int i=0; i<n; ++i)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0; i<n; ++i)
        {
            long long k=i;
            cnt=0;
            while(a[k]!=-1)//若已被标记 则形成循环
            {
                cnt++;
                int t=k;//t为当前查找的位置
                k=a[k];//根据循环公式a[位置]=下一个位置 查找下一个位置
                a[t]=-1;//标记走过的位置
            }
            if(cnt)
            {
                arr[0][cnt]++;
            }
        }
        for(int i=0; i<m; ++i)
        {
            scanf("%d",&b[i]);
        }
        for(int i=0; i<m; ++i)
        {
            long long k=i;
            cnt=0;
            while(b[k]!=-1)
            {
                cnt++;
                int t=k;
                k=b[k];
                b[t]=-1;
            }

            if(cnt)
            {
                arr[1][cnt]++;
            }
        }
        long long ans=1;
        for(int i=1; i<=n; ++i)
        {
            if(arr[0][i])
            {
                int lim=(int)sqrt(i+0.5);
                int sum=0;
                for(int j=1; j<=lim; ++j)//类似于筛法的优化
                {
                    if(i%j==0)
                    {
                        sum+=arr[1][j]%mod*j%mod;
                        sum%=mod;
                        if(j*j!=i)
                        {
                            sum+=arr[1][i/j]%mod*(i/j)%mod;
                            sum%=mod;
                        }
                    }

                }
                for(int j=1; j<=arr[0][i]; ++j)
                {
                    ans*=sum;
                    ans%=mod;
                }
            }

        }
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}

ps:理解题意贼重要啊QAQ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值