22616_FactorialSimplification

2022-5-16 18:22| 发布者: Hocassian| 查看: 33| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-5-89506789171500-Problem List-采集的数据-后羿采集器.html

Pro.ID

22616

Title

Factorial Simplification

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=22616

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 20000/10000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    Peter is working on a combinatorial problem. He has carried out quite lengthy derivations and got a resulting formula that is a ratio of two products of factorials like this:

    This does not surprise Peter, since factorials appear quite often in various combinatorial formulae, because n! represents the number of transpositions of n elements — one of the basic combinatorial objects.

    However, Peter might have made a mistake in his derivations. He knows that the result should be an integer number and he needs to check this first. For an integer result Peter wants to simplify this formula to get a better feeling of its actual combinatorial significance. He wants to represent the same number as a product of factorials like this.

    where all ri are distinct integer numbers greater than one in the descending order (ri > ri+1 > 1), si and t are positive integers. Among all the possible representations in this form, Peter is interested in one where r1 is the largest possible number, among those in the one where s1 is the largest possible number; among those in the one where r2 is the largest possible number; among those in the one where s2 is the largest possible number; etc, until the remaining t cannot be further represented in this form. Peter does not care about the actual value of t. He wants to know what is the factorial-product part of his result.

    输入

    The first line of the input file contains two integer numbers n and m ( 1 ≤ n, m ≤ 1000 ). The second line of the input file contains n integer numbers pi ( 1 ≤ pi ≤ 10000 ) separated by spaces. The third line of the input file contains m integer numbers qi (1 ≤ qi ≤ 10000) separated by spaces.

    输出

    Description

    Peter is working on a combinatorial problem. He has carried out quite lengthy derivations and got a resulting formula that is a ratio of two products of factorials like this:

    This does not surprise Peter, since factorials appear quite often in various combinatorial formulae, because n! represents the number of transpositions of n elements — one of the basic combinatorial objects.

    However, Peter might have made a mistake in his derivations. He knows that the result should be an integer number and he needs to check this first. For an integer result Peter wants to simplify this formula to get a better feeling of its actual combinatorial significance. He wants to represent the same number as a product of factorials like this.

    where all ri are distinct integer numbers greater than one in the descending order (ri > ri+1 > 1), si and t are positive integers. Among all the possible representations in this form, Peter is interested in one where r1 is the largest possible number, among those in the one where s1 is the largest possible number; among those in the one where r2 is the largest possible number; among those in the one where s2 is the largest possible number; etc, until the remaining t cannot be further represented in this form. Peter does not care about the actual value of t. He wants to know what is the factorial-product part of his result.

    Input

    The first line of the input file contains two integer numbers n and m ( 1 ≤ n, m ≤ 1000 ). The second line of the input file contains n integer numbers pi ( 1 ≤ pi ≤ 10000 ) separated by spaces. The third line of the input file contains m integer numbers qi (1 ≤ qi ≤ 10000) separated by spaces.

    Output

    On the first line of the output write a single integer number k. Write k = -1 if the ratio of the given factorial products is not an integer. Write k = 0 if the ratio is an integer but it cannot be represented in the desired form. Write k > 0 followed by k lines if the ratio can be represented by a factorial product as described in the problem statement. On each of the following k lines write two integers ri and si (for i = 1 ... k) separated by a space.

    Sample Input

    Sample #1
    1 2
    6
    4 4

    Sample #2
    1 2
    6
    3 4

    Sample #3
    4 2
    9 2 2 2
    3 4

    Sample Output

    Sample #1
    -1

    Sample #2
    0

    Sample #3
    2
    7 1
    2 2

    Source

    样例输入

    Sample #1
    1 2
    6
    4 4

    Sample #2
    1 2
    6
    3 4

    Sample #3
    4 2
    9 2 2 2
    3 4

    样例输出

    Sample #1
    -1

    Sample #2
    0

    Sample #3
    2
    7 1
    2 2

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部