HDU-Function

来源:互联网 发布:windows 7 sp1 32位 编辑:程序博客网 时间:2024/06/05 11:12

Function

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

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 21 0 20 13 42 0 10 2 3 1

Sample Output

Case #1: 4Case #2: 4

翻译:

题目描述
你被赋予了一个排列 一个 从 0 到 n-1 和置换 b 从 0 到 m-1 。

定义功能域 f 是来自的整数集合 0 到 n-1 ,其范围是来自的整数 0 到 m-1 。

请计算不同功能的数量 f 满足 F(1)= BF(AI) 为每个 我 来自 0 到 n-1 。

当且仅当存在至少一个整数时,两个函数是不同的 0 到 n-1 映射到这两个函数中的不同整数。

答案可能太大了,所以请输出模 109 + 7 。
输入
输入包含多个测试用例。

对于每种情况:

第一行包含两个数字 N, m 。 (1≤n≤100000,1≤m≤100000)

第二行包含 n个 数字,范围从 0 到 n-1 , 第 i 个数字代表 ai-1 。

第三行包含 m 数字,范围从 0 到 m-1 , 第 i 个数字代表 bi-1 。

这是有保证的 Σn≤106, Σm≤106 。
输出
对于每个测试用例,输出“ Case#X: y “一行(不含引号),其中 x 表示案件>编号 1 和 y 表示相应案例的答案。

代码:

#include<stdio.h>#include<string.h>#include<algorithm>#define ll long longusing namespace std;ll eular(ll n){    ll i,j,ans=n;    for(i=2;i*i<=n;i++)    {        if(n%i==0)        {            ans=ans/i*(i-1);            while(n%i==0)                n/=i;        }    }    if(n>1)        ans=ans/n*(n-1);    return ans;}int main(){    ll n,x;    while(scanf("%lld%lld",&n,&x)!=EOF)        printf("%lld\n",eular(n+x+1));    return 0;}