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的更多相关文章

  1. 「LuoguP4752」牧 Divided Prime(判质数

    Description 给定一个数字 A,这个 A 由 a1,a2,⋯,aN相乘得到. 给定一个数字 B,这个 B 由 b1,b2,⋯,bM相乘得到. 如果 A/B 是一个质数,请输出YES,否则输出 ...

  2. 【LGR-049】洛谷7月月赛

    Preface Luogu八月月赛都结束了我才来补七月月赛 这次月赛还是很狗的,在绍一的晚上恰逢刮台风,然后直接打到一半断网了 结果都没有交上去GG 感觉这次难度适中,解法也比较清新自然吧,十分给个九 ...

  3. 【堆栈应用一】一个数divided=几个最小质因数的乘积

    /******************************************堆栈:一个数divided几个质因数(质因数的乘积为N)***************************** ...

  4. 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 ...

  5. Summary: Prime

    最近遇到很多问题都跟Prime有关,于是总结一下: Prime definition:A prime number (or a prime) is a natural number greater t ...

  6. ZOJ - 3483 - Gaussian Prime

    先上题目: Gaussian Prime Time Limit: 3 Seconds      Memory Limit: 65536 KB In number theory, a Gaussian ...

  7. [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 ...

  8. 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 ...

  9. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

随机推荐

  1. 算法笔记(c++)--完全背包问题

    算法笔记(c++)--完全背包和多重背包问题 完全背包 完全背包不同于01背包-完全背包里面的东西数量无限 假设现在有5种物品重量为5,4,3,2,1  价值为1,2,3,4,5  背包容量为10 # ...

  2. rest_framework之版本控制

    简介 API版本控制可以用来在不同的客户端使用不同的行为.REST框架提供了大量不同的版本设计. 版本控制是由传入的客户端请求决定的,并且可能基于请求URL,或者基于请求头. 有许多有效的方法达到版本 ...

  3. Live Love(思维)

    DreamGrid is playing the music game Live Love. He has just finished a song consisting of n notes and ...

  4. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  5. 细节--服务器mysql空密码

    在部署致服务器的时候 发现mysql密码为空的情况 如果采用 root账户的话 试过很多 比如不写下面这行 <property name="password" value=& ...

  6. [2017BUAA软工]第三次博客作业:案例分析

    第三次博客作业:案例分析 1. 调研和评测 1.1 BUG及设计缺陷描述 主要测试博客园在手机端上的使用情况. [BUG 01] 不能后退到上一界面(IOS) 重现步骤:打开博客首页中任意博文,点击博 ...

  7. 【转】mysql force Index 强制索引

    其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...

  8. 修改表中的enum字段

    alter table 表名 modify 字段名 enum('system','audit','account','secadmin') DEFAULT NULL;

  9. apache 部署web.py

    一.安装Mod_wsgi 1.先yum -y install httpd-devel,否则会提示没有apxs 2.如果在make时 wsgi报错apxs:Error: Command failed w ...

  10. windows查看端口占用指令

    1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\>netstat -ano 协议    本地地址                     外部地址  ...