CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合
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), we obtain fraction 20/3.
日常WA一天
不看跑的数据,我都不知道自己怎么错的,老天爷。我的输出超出了限制100001不能超过100000,我觉得那时候,那些没有过的,一定是这个原因,出题人真是丧心病狂。
第一个代码是错的,第二个是修改了的,换了方式。
#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
int f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *f;
}
bitset<100000010> v;
int prime[6000001];
int m = 0;
void primes(int n)
{
for (int i = 2; i * i <= n; i++)
{
if (!v[i])
{
for (int j = i * i; j <= n; j += i)
v[j] = 1;
}
}
for (int i = 2; i <= n; i++)
if (!v[i])
prime[m++] = i;
}
vector<int> a[4];
unordered_map<int, int> c, d;
int main()
{
int n, m, maxi = 0;
read(n), read(m);
primes(10000005);
for (int i = 0; i < n; i++)
{
int tem;
read(tem);
maxi = max(maxi, tem);
c[tem]++;
}
for (int i = 0; i < m; i++)
{
int tem;
read(tem);
maxi = max(maxi, tem);
d[tem]++;
}
//cout << 1 << endl;
for (int i = 0; prime[i] <= maxi; i++)
{
// cout<<i<<endl;
int cnt = 0, ans = 0, cnt2 = 0;
int flag = 1;
for (auto po = c.begin(); po != c.end();)
{
// cout<<1<<endl;
pair<int, int> tem = *po;
cnt = 0;
if (tem.first < prime[i])
{
po++;
continue;
}
else
{
flag = 0;
while (tem.first % prime[i] == 0)
{
tem.first /= prime[i];
cnt++;
//cout<<i<<endl;
}
cnt *= tem.second;
auto pi = po;
po++;
c.erase(pi);
if (tem.first != 1)
c[tem.first] += tem.second;
}
ans += cnt;
}
cnt2 = ans;
ans = 0;
for (auto po = d.begin(); po != d.end();)
{
pair<int, int> tem = *po;
cnt = 0;
if (tem.first < prime[i])
{
po++;
continue;
}
else
{
flag = 0;
while (tem.first % prime[i] == 0)
{
tem.first /= prime[i];
cnt++;
//cout<<i<<endl;
}
cnt *= tem.second;
auto pi = po;
po++;
d.erase(pi);
if (tem.first != 1)
d[tem.first] += tem.second;
}
ans += cnt;
}
cnt = cnt2 - ans;
if (cnt == 0)
continue;
else if (cnt < 0)
{
cnt = -cnt;
int temp = 1;
int j = 0;
for (; j < cnt; j++)
{
temp *= prime[i];
if (temp * prime[i] > 10000000)
{
a[3].push_back(temp);
// cout << 1 << endl;
temp = 1;
}
}
a[3].push_back(temp);
}
else
{
int temp = 1;
int j = 0;
for (; j < cnt; j++)
{
temp *= prime[i];
if (temp * prime[i] > 10000000)
{
a[2].push_back(temp);
// cout << 1 << endl;
temp = 1;
}
}
a[2].push_back(temp);
}
if (flag)
break;
}
if (a[2].size() == 0)
a[2].push_back(1);
if (a[3].size() == 0)
a[3].push_back(1);
cout << a[2].size() << " " << a[3].size() << endl;
for (int i = 0; i < a[2].size(); ++i)
printf("%d ", a[2][i]);
puts("");
for (int i = 0; i < a[3].size(); ++i)
printf("%d ", a[3][i]);
puts("");
}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m, tot, a[100005], b[100005], z[10000005], pos[10000005], q[1000005], t1[1000005], t2[1000005];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i)
scanf("%d", &b[i]);
for (int i = 2; i <= 10000000; ++i)
if (!z[i])
{
for (int j = i; j <= 10000000; j += i)
z[j] = i;
q[++tot] = i;
pos[i] = tot;
}
for (int i = 1; i <= n; ++i)
{
int k = a[i];
while (k != 1)
{
t1[pos[z[k]]]++;
k /= z[k];
}
}
for (int i = 1; i <= m; ++i)
{
int k = b[i];
while (k != 1)
{
t2[pos[z[k]]]++;
k /= z[k];
}
}
for (int i = 1; i <= tot; ++i)
{
t1[i] = min(t1[i], t2[i]);
t2[i] = t1[i];
}
printf("%d %d\n", n, m);
for (int i = 1; i <= n; ++i)
{
int k = a[i], p = a[i];
while (k != 1)
{
if (t1[pos[z[k]]])
{
p /= z[k];
t1[pos[z[k]]]--;
}
k /= z[k];
}
printf("%d ", p);
}
printf("\n");
for (int i = 1; i <= m; ++i)
{
int k = b[i], p = b[i];
while (k != 1)
{
if (t2[pos[z[k]]])
{
p /= z[k];
t2[pos[z[k]]]--;
}
k /= z[k];
}
printf("%d ", p);
}
printf("\n");
return 0;
}
CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)的更多相关文章
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- CF思维联系–CodeForces - 225C. Barcode(二路动态规划)
ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)
ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...
- P1458 顺序的分数 Ordered Fractions(有技巧的枚举)+C++类封装=精简代码
题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数. 这有一个例子,当N=5时,所有解 ...
- CodeForce 222C Reducing Fractions
To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...
随机推荐
- win10无法使用VMwareWorkstation的解决办法
最近(2019-10)国庆期间,微软更新了一次win10. 此次更新导致很多同学的Workstation pro不能用了. 主要的解决办法有一下几种: 卸载最新的系统更新包 在控制面板>> ...
- Jdk 和 jre 的 关系和区别
Jdk 和 jre 的 关系和区别 区别: JDK:是Java Development Kit 的简称–>翻译过来就是:Java 开发工具包.是程序员使用java语言编写java程序所需的开发工 ...
- Lua 5.3 -- SOL2.0 用户指南 【2】
系列教程指南[1] 注意 在你学习了sol的基础知识之后,建议你如果认为某些东西可以运行,你应该尝试一下.它可能会运行! 以下所有代码均可在sol2教程示例中找到. 断言/先决条件 The imple ...
- https的秘钥公钥以及之间的会话流程
一 共享秘钥 1.1 概念 共享秘钥和我们生活中同一把锁的钥匙概念类似,对同一把锁来说,加锁时使用什么钥匙,解锁也必须使用同样的钥匙. 1.2 共享秘钥在HTTP传输中的缺点 以共享密钥方式加密时 ...
- 数据结构和算法(Golang实现)(10)基础知识-算法复杂度主方法
算法复杂度主方法 有时候,我们要评估一个算法的复杂度,但是算法被分散为几个递归的子问题,这样评估起来很难,有一个数学公式可以很快地评估出来. 一.复杂度主方法 主方法,也可以叫主定理.对于那些用分治法 ...
- 视频图文教学 - 用最快的速度把 DotNet Core Blazor 程序安装到 树莓派中 并且用网页控制 GPIO 闪灯
前言 dotnet core 在3.0时代已经发展得很好. 尤其是在跨平台方面更已经是达到了很实用的阶段. 作为 dotnet 程序员, 应该对 Linux 有充分的了解, 也可以在业余时间玩玩硬件, ...
- IOCP完成端口
转:https://blog.csdn.net/piggyxp/article/details/6922277 本系列里完成端口的代码在两年前就已经写好了,但是由于许久没有写东西了,不知该如何提笔,所 ...
- Django中HttpRequest常用参数介绍
HttpRequest对象常用参数介绍,以及前端不同请求方式(http方法/Content-Type类型)对应的参数获取方式. 一.HttpRequest对象 django请求对象的详细参数以及实现方 ...
- Java 多线程实现方式三:实现 Callable 接口
完整套路 java 通过实现Callable 接口来实现多线程相比较于继承Thread 接口和 实现Runnable 接口比较麻烦,但好处是可以有返回值. 基本套路: 1. 创建目标对象 2. 创建执 ...
- C#多线程(12):线程池
目录 线程池 ThreadPool 常用属性和方法 线程池说明和示例 线程池线程数 线程池线程数说明 不支持的线程池异步委托 任务取消功能 计时器 线程池 线程池全称为托管线程池,线程池受 .NET ...