P4752 Divided Prime
P4752 Divided Prime
题目描述
给定一个数字 AA ,这个 AA 由 a_1,a_2,\cdots,a_Na
1
,a
2
,⋯,a
N
相乘得到。
给定一个数字 BB ,这个 BB 由 b_1,b_2,\cdots,b_Mb
1
,b
2
,⋯,b
M
相乘得到。
如果 \frac{A}{B}
B
A
是一个质数,请输出YES,否则输出NO。
输入输出格式
输入格式:
每个测试点包含多组数据,第一行读入一个整数 TT 表示数据组数,对于每组数据:
第一行输入两个整数 N,MN,M ,分别表示 AA 由 NN 个数字相乘得到, BB 由 MM 个数字相乘得到。
第二行输入 NN 个整数,分别表示组成 AA 的 NN 个数字。
第三行输入 MM 个整数,分别表示组成 BB 的 MM 个数字。
保证对于一个数字,其在 {b_i}b
i
中出现的次数不多于在 {a_i}a
i
中出现的次数。
输出格式:
对于每组数据:
如果 \frac{A}{B}
B
A
是一个质数,请输出YES,否则输出NO。
在输出YES或NO后输出一个换行符。
模拟 + 数学
首先读题可知,分母\(B\)的因子分子\(A\)都有,所以我们把因子排序,双指针模拟约分的过程。
一个约分后的分数为质数,当且约分后分子剩余一个质数
所以我们约分,当分子有合数不能约分是,则不是质数,或者当分子有两个以上质数无法约分是则不是质数
注意\(1\)不是质数,还有分子分母约数的\(1\)要跳过
Code
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cmath>
typedef long long LL;
using namespace std;
LL RD(){
LL out = 0,flag = 1;char c = getchar();
while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return flag * out;
}
const LL maxn = 1000019;
LL T, na, nb;
LL a[maxn], b[maxn];
bool isprime(LL x){
LL R = sqrt(x * 1.0);
for(LL i = 2;i <= R;i++){
if(x % i == 0)return 0;
}
return 1;
}
int main(){
T = RD();
while(T--){
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
na = RD();nb = RD();
for(LL i = 1;i <= na;i++)a[i] = RD();
for(LL i = 1;i <= nb;i++)b[i] = RD();
sort(a + 1, a + 1 + na);sort(b + 1, b + 1 + nb);
LL p1 = 1, p2 = 1, tot = 0, mem = -1;
bool flag = 1;
while(a[p1] == 1)p1++;while(b[p2] == 1)p2++;
while(p1 <= na){
if(a[p1] == b[p2])p1++, p2++;
else{
mem = a[p1];
if(!isprime(mem)){
printf("NO\n");
flag = 0;
break;
}
tot++;
if(tot == 2){
printf("NO\n");
flag = 0;
break;
}
p1++;
}
}
if(mem == -1){
printf("NO\n");
continue;
}
if(flag)
printf("YES\n");
}
return 0;
}
P4752 Divided Prime的更多相关文章
- 「LuoguP4752」牧 Divided Prime(判质数
Description 给定一个数字 A,这个 A 由 a1,a2,⋯,aN相乘得到. 给定一个数字 B,这个 B 由 b1,b2,⋯,bM相乘得到. 如果 A/B 是一个质数,请输出YES,否则输出 ...
- 【LGR-049】洛谷7月月赛
Preface Luogu八月月赛都结束了我才来补七月月赛 这次月赛还是很狗的,在绍一的晚上恰逢刮台风,然后直接打到一半断网了 结果都没有交上去GG 感觉这次难度适中,解法也比较清新自然吧,十分给个九 ...
- 【堆栈应用一】一个数divided=几个最小质因数的乘积
/******************************************堆栈:一个数divided几个质因数(质因数的乘积为N)***************************** ...
- hdu 5594 ZYB's Prime 最大流
ZYB's Prime Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...
- Summary: Prime
最近遇到很多问题都跟Prime有关,于是总结一下: Prime definition:A prime number (or a prime) is a natural number greater t ...
- ZOJ - 3483 - Gaussian Prime
先上题目: Gaussian Prime Time Limit: 3 Seconds Memory Limit: 65536 KB In number theory, a Gaussian ...
- [Algorithm] Finding Prime numbers - Sieve of Eratosthenes
Given a number N, the output should be the all the prime numbers which is less than N. The solution ...
- about how to determine a prime number
(1) if divided by 2 or 3, then no; (2) we only have to go through prime factors; because a composite ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- ab命令做压测测试
1. 背景:互联网发达的今天,大大小小的网站如雨后春笋,不断出现,但是想要做出一个网站很简单,但是想要做好一个网站,非常非常难,首先:网站做好之后的功能怎么样这都是次要的,主要的是你的网站能承受怎么样 ...
- Sorting a Three-Valued Sequence(三值排序)
Description 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌序的时候. 在这个任务中可能的值只有三种1,2和3.我们用交换的 ...
- Scrum立会报告+燃尽图(十一月十五日总第二十三次):代码规范与技术交流
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 项目地址:https://git.coding.net/zhang ...
- Scrum立会报告+燃尽图(十月二十四日总第十五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- c# Application.run和form.show区别
Application.run(form):在当前线程上开始运行标准应用程序消息循环,并使指定窗体可见. form.show() :使指定窗体可见: 参照:https://blog.csdn.net/ ...
- 解决Ubuntu16.04下git clone太慢问题
记录一些博客,省着自己再去找了... ss-qt5安装 生成.pac genpac --pac-proxy "SOCKS5 127.0.0.1:1080" --gfwlist-pr ...
- Eclipse的黑色主题背景(github)
MoonRise UI Theme An early version of a dark UI theme for Eclipse 4+. Requirements Eclipse 4.2+ In ...
- (三)MySQL终极篇
1.索引 详细介绍:http://www.cnblogs.com/57rongjielong/p/8039452.html 索引是对数据库表中一个或多个列的值进行排序的结构.索引是经过某种算法优化过的 ...
- SpringCloud——服务网关
1.背景 上篇博客<SpringCloud--Eureka服务注册和发现>中介绍了注册中心Eureka.服务提供者和服务消费者.这篇博客我们将介绍服务网关. 图(1) 未使用服务网关的做法 ...
- c 读取文本
#include <stdio.h> #include <stdlib.h> #include <string.h> #define max 10 #define ...