SDNU 1519.lmh’s Function 找循环节

1519.lmh’s Function

Problem Description

 

One day,lmh did a problem in HDU.But he doubted thisproblem is wrong.At last,he found the reason is he get the wrong meaning of thetitle.

He spent a lot of time to solve it,so he did not convincehimself.And then,he decide to change this title to satisfy his vanity.

You are given a permutation a from 0 to n−1 and a permutationb from 0 tom−1.

Please calculate the quantity of different functions f satisfying thatf(i)=bf(ai) for each i.(You just need to consider the value off from 0 to n−1.)

Two functions are different if and only if there existsat least one integer from 0 ton−1 mapped into different integers in these two functions.

The answer may be too large, so please output it inmodulo 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,the i-th number of which representsai−1.

The third line contains m numbers,the i-th number of which representsbi−1.

It is guaranteed that ∑n≤106, ∑m≤106.

 

 

Output

For each test case, output "Case #x: y" inone line (without quotes), wherex indicates thecase 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


    出这道题的原因是因为在做HDU6038的时候,我和海强题意限制都没看清是结束到n-1的,结果一个题讨论了一晚上我们觉得标程错了。最后师弟一句话点醒原来是题意没好好看,但是再怎么说也是讨论了一晚上,所以干脆我又出了一道无限制的题→_→

    题意以及前面的思路就不细说了,直接去看我上一篇博客就行了:http://blog.csdn.net/lmhacm/article/details/76160297

    这个题改动的地方就在于原题目无论是f还是a,数据都不会超过n-1,这里我改成了会超过,也就是可能出现a数组为1 2 3 4的可能,这样的话f函数就变成了,假设b给的数据为b[0]=0,因为只需要考虑0到n-1范围内的f,所以后面数字的取值不必考虑,这样f(4)已经没有限制了,所以在赋值的时候我们可以视为,也就是将这一组数视为一个长度为5的循环节,之后再赋值都可以等于0。

    所以与HDU6038的区别其实只在于循环的次数,这个因为可能外面会出现循环,这样循环节长度可能出现>n的情况,但最多也就是n+1,所以在跑循环的时候,只需要多跑一次循环,跑到n+1就可以将所有情况包含进去了。

    (PS:比赛提供的数据放OJ里会RE...所以暂时没放一些很大的数据,数据较水)

    下面AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
int a[100005];
int b[100005];
int vis[100005];
int cal[5][100005];
const int mod=1000000007;

int dfs(int t,int i,int *c,int k)
{
    if(vis[t])
    {
        cal[k][i]++;
        return 0;
    }
    vis[t]=1;
    dfs(c[t],i+1,c,k);
    return 0;
}

int main()
{
    int Case=0;
    int n,m;
    int i,j;
    long long ans;
    int lim;
    long long t;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        memset(cal,0,sizeof(cal));
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0;i<m;i++)
        {
            scanf("%d",&b[i]);
        }
        Case++;
        cout<<"Case #"<<Case<<": ";
        for(i=0;i<n;i++)
        {
            if(!vis[i])
                dfs(i,0,a,1);
        }
        memset(vis,0,sizeof(vis));
        for(j=0;j<m;j++)
        {
            if(!vis[j])
                dfs(j,0,b,0);
        }
        ans=1;
		for(int i=1;i<=n+1;i++)
        {
            lim=(int)sqrt(i+0.5);
            t=0;
            if(cal[1][i])
            {
                for(int j=1;j<=lim;j++)
                {
                    if(i%j==0)
                    {
                        t+=cal[0][j]%mod*j%mod;
                        t%=mod;
                        if(j*j!=i)
                            t+=cal[0][i/j]%mod*(i/j)%mod;
                    }
                    t%=mod;
                }
                for(int j=1;j<=cal[1][i];j++)
                {
                    ans*=t%mod;
                    ans%=mod;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值