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

發佈了87 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章