CodeForces 222C Reducing Fractions

C. Reducing Fractions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
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), we obtain fraction 20/3.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <map>
#include <set>
#include <list>

#if ( _win32 || __win32__ )
#define lld "%i64d"
#else
#define lld "%lld"
#endif

#define MP make_pair
#define PB push_back
#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.14159265358979323846
#define N 700010
#define E 100010

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int Uint;
typedef double DB;

int min_prime[10000000];
vector<int> prime;
void get_prime()
{
    prime.clear();
    memset(min_prime,-1,sizeof(min_prime));
    for(int i=2; i<10000000; i++)
    {
        if(min_prime[i]==-1)
        {
            prime.PB(i);
            min_prime[i]=(int)prime.size()-1;
        }
        for(int j=0; j<(int)prime.size() && i*prime[j]<10000000; j++)
        {
            min_prime[i*prime[j]]=j;
            if(i%prime[j]==0) break;
        }
    }
}

int a[2][N];

void get_in(int u)
{
    for(int i=1; i<=a[u][0]; i++)
        scanf("%d",&a[u][i]);
}

struct data
{
    int id,num;
};
vector<data> temp[2][N];
int g[2][N];

void factor(int u,int id)
{
    int val=a[u][id];
    while(val>0)
    {
        int pos=min_prime[val];
        data now;
        now.id=id;
        now.num=0;
        while(val>0 && val%prime[pos]==0)
        {
            now.num++;
            val/=prime[pos];
        }
        temp[u][pos].PB(now);
        g[u][pos]+=now.num;
        if(val==1) break;
    }
}

void deal(int u)
{
    for(int i=0; i<N; i++)
        temp[u][i].clear();
    memset(g[u],0,sizeof(g[u]));
    for(int i=1; i<=a[u][0]; i++)
        factor(u,i);
}

int pow(int n,int p)
{
    if(p==0) return 1;
    int odd=1;
    while(p>1)
    {
        if(p&1) odd*=n;
        n*=n;
        p/=2;
    }
    return n*odd;
}

void calculate(int u)
{
    int tot=(int)prime.size();
    for(int i=0; i<tot; i++)
    {
        if(g[u][i]==0) continue;
        for(int j=0; g[u][i]>0 && j<(int)temp[u][i].size(); j++)
        {
            data now=temp[u][i][j];
            if(now.num<g[u][i])
            {
                g[u][i]-=now.num;
                a[u][now.id]/=pow(prime[i],now.num);
            }
            else
            {
                a[u][now.id]/=pow(prime[i],g[u][i]);
                g[u][i]=0;
            }
            if(g[u][i]==0) break;
        }
    }
}

void Print(int u)
{
    for(int i=1; i<=a[u][0]; i++)
    {
        printf("%d",a[u][i]);
        if(i==a[u][0]) printf("\n");
        else printf(" ");
    }
}

int main()
{
    get_prime();
    while(~scanf("%d%d",&a[0][0],&a[1][0]))
    {
        get_in(0);
        get_in(1);
        deal(0);
        deal(1);
        for(int i=0; i<N; i++)
        {
            if(g[0][i]>g[1][i]) g[0][i]=g[1][i];
            else g[1][i]=g[0][i];
        }
        calculate(0);
        calculate(1);
        printf("%d %d\n",a[0][0],a[1][0]);
        Print(0);
        Print(1);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值