CodeForce 222C Reducing Fractions

To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractions are represented as two sets of integers. The product of numbers from the first set gives the fraction numerator, the product of numbers from the second set gives the fraction denominator. However, it turned out that the programs that work with fractions in this representations aren't complete, they lack supporting the operation of reducing fractions. Implement this operation and the Empire won't forget you.


Input

The first input line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) that show how many numbers the first set (the numerator) and the second set (the denominator) contain, correspondingly.

The second line contains n space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 107) — the numbers that are multiplied to produce the numerator.

The third line contains m space-separated integers: b1, b2, ..., bm (1 ≤ bi ≤ 107) — the numbers that are multiplied to produce the denominator.

Output

Print the answer to the problem in the form, similar to the form of the input data. The number of values in the sets you print nout, mout must satisfy the inequality 1 ≤ nout, mout ≤ 105, and the actual values in the sets aout, i and bout, i must satisfy the inequality 1 ≤ aout, i, bout, i ≤ 107.

Separate the values in the lines by spaces. The printed fraction must be reduced, that is, there mustn't be such integer x (x > 1), that the numerator and the denominator of the printed fraction are divisible by x. If there are several matching answers, print any of them.

Examples
Input
3 2
100 5 2
50 10
Output
2 3
2 1
1 1 1
Input
4 3
2 5 10 20
100 1 3
Output
1 1
20
3
Note

In the first test sample the numerator equals 1000, the denominator equals 500. If we reduce fraction 1000/500 by the greatest common divisor of the numerator and the denominator (by 500), we obtain fraction 2/1.

In the second test sample the numerator equals 2000, the denominator equals 300. If we reduce fraction 2000/300 by the greatest common divisor of the numerator and the denominator (by 100),

OJ-ID:
CodeForce 222C

author:
Caution_X

date of submission:
20191012

tags:
分解质因数

description modelling:
给出分子分母,求通分。(分子分母以一系列数的乘积给出)

major steps to solve it:
1.分别把分子分母分解质因数
2.通分

warnings:
分解质因数后有两种处理方案:
①:比较分解后分子分母的质因数,然后消去分子分母中相同的质因数(导致(溢出)WA和TLE)
②:用原来分子的乘积和分母的质因数相消,再用原来分母的乘积和分子的质因数相消
采用方案②

AC code:

#include<cstdio>
#include<cstring>
using namespace std;
int prime[10000005]={0};
int a[100005],b[100005];
int ap[10000005],bp[10000005];
void check(int *x,int *y,int len)//分解因子
{
    for(int i=0;i<len;++i)
        for(int j=x[i];j>1;j/=prime[j])
           y[prime[j]]++;//因子数+1
}
void print(int *x,int *y,int len)
{
    int cnt;
    for(int i=0;i<len;++i)
    {
        cnt=1;
        for(int j=x[i];j>1;j/=prime[j])
            if(y[prime[j]]>0) y[prime[j]]--;
            else              cnt*=prime[j];
        if(i==0) printf("%d",cnt);
        else     printf(" %d",cnt);
    }
    puts("");
}
int main()
{
    int n,m;
    prime[2]=0;
    for(int i=2;i<=10000000;++i)
        if(!prime[i])
        {
            prime[i]=i;
            for(int j=2*i;j<=10000000;j+=i)
                prime[j]=i;
        }
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i) scanf("%d",a+i);
    for(int i=0;i<m;++i) scanf("%d",b+i);
    check(a,ap,n);check(b,bp,m);
    printf("%d %d\n",n,m);
    print(a,bp,n);print(b,ap,m);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cautx/p/11716422.html
今日推荐