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.
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
3 2
100 5 2
50 10
2 3
2 1
1 1 1
4 3
2 5 10 20
100 1 3
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[]={};
int a[],b[];
int ap[],bp[];
void check(int *x,int *y,int len)//分解因子
{
for(int i=;i<len;++i)
for(int j=x[i];j>;j/=prime[j])
y[prime[j]]++;//因子数+1
}
void print(int *x,int *y,int len)
{
int cnt;
for(int i=;i<len;++i)
{
cnt=;
for(int j=x[i];j>;j/=prime[j])
if(y[prime[j]]>) y[prime[j]]--;
else cnt*=prime[j];
if(i==) printf("%d",cnt);
else printf(" %d",cnt);
}
puts("");
}
int main()
{
int n,m;
prime[]=;
for(int i=;i<=;++i)
if(!prime[i])
{
prime[i]=i;
for(int j=*i;j<=;j+=i)
prime[j]=i;
}
scanf("%d%d",&n,&m);
for(int i=;i<n;++i) scanf("%d",a+i);
for(int i=;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 ;
}
CodeForce 222C Reducing Fractions的更多相关文章
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- CF222C Reducing Fractions
题目大意: 给出两个集合,第一个集合数的乘积是分子,第二个集合的数的乘积是分母,要求够造一个同样的集合,但是得到的分数是最简分数. 分析: 寻找思路并不复杂,对两个集合的每个数进行质因数分解,然后统计 ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- codeforces 练习
codeforces 627 D. Preorder Test 二分 + 树dp 做logn次树dp codeforces 578D.LCS Again 给出一个字符串str,长度n<=10^6 ...
- Codeforces Round #137 (Div. 2)
A. Shooshuns and Sequence 显然\([k,n]\)之间所有数均要相同,为了求最少步数,即最多模拟\(n\)次操作即可. B. Cosmic Tables 映射\(x_i,y_i ...
- CodeForce 359C Prime Number
Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1, ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...
- Codeforces Round #232 (Div. 2) D. On Sum of Fractions
D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...
- 一天一经典Reducing the Dimensionality of Data with Neural Networks [Science2006]
别看本文没有几页纸,本着把经典的文多读几遍的想法,把它彩印出来看,没想到效果很好,比在屏幕上看着舒服.若用蓝色的笔圈出重点,这篇文章中几乎要全蓝.字字珠玑. Reducing the Dimensio ...
随机推荐
- LinqMethod 实现 LeftJoin
LinqMethod 实现 LeftJoin Intro 有时候我们想实现 leftJoin 但是 Linq 提供的 Join 相当于是 INNER JOIN,于是就打算实现一个 LeftJoin 的 ...
- 关于window10更新之后,15.5版本虚拟机不能使用的情况:检测更新版本
1.用了四五年的虚拟机,最近居然老提示检测更新版本,嗯,我将我的虚拟机由10版本,更新到了15.5版本,这也是网友说的,然而并灭有什么乱弄.window10系统自动更新,自动更新以后虚拟机就打不开了, ...
- Web前端——Html常用标签及属性
html 常用的标题等标签就不记录了,只记录一下比较少见的标签以及属性 表格 table td 单元格 tr 表的行 th 表头 td或th可以下面的两个属性达到跨行或跨列 表格跨行 rowspan ...
- HTTP 状态码及含义
来自 Koa.js 官方文档中关于设置请求响应的部分 response.status=,列出了从 1xx ~ 5xx HTTP 状态码及含义,现摘录如下: 100 "continue&quo ...
- LabVIEW工控二进制数据存储
在文件存储的逻辑上,二进制文件基于值编码,而不是字符编码,其占用空间小,读取/写入速度快,但是译码比较复杂,不利用数据共享.根据具体编码方式的不同,二进制的使用方式也有所不同,如对bmp格式,规定了文 ...
- PHP-Curl模拟HTTPS请求
使用PHP-Curl方式模拟HTTPS请求,测试接口传参和返回值状态 上代码!! <?php /** * 模拟post进行url请求 * @param string $url * @par ...
- django3-路由系统进阶
1.django的url到底是什么 就是路径 ,看成django的目录 ,每个目录对应个视图函数 ,当然一个url仅能对应一个函数 2.url的格式 url(正则表达式,views函数名,参数,别名) ...
- ES6数组及对象遍历的新增方法 entries(),keys() 和 values()
ES6 提供三个新的方法——entries(),keys()和values()——用于遍历数组.它们都返回一个遍历器对象(详见<Iterator>一章),可以用for...of循环进行遍历 ...
- 每天一点产品思考(5):Web端链接跳转在当前页面刷新还是新标签页打开?
一.与交互设计师的突然撕逼 今天阿白在验收产品的时候,在博客首页打开一篇博文,是在原先的页面进行刷新,而不是新开一个标签页打开.阿白让开发改成在新标签页中打开,但是开发说这是设计师设计 ...
- python基础-并发编程02
并发编程 子进程回收的两种方式 join()让主进程等待子进程结束,并回收子进程资源,主进程再结束并回收资源 from multiprocessing import Process import ti ...