Codeforces 1294C - Product of Three Numbers
题目大意:
给定一个n,问是否存在3个互不相同的,大于等于2的整数,满足a*b*c=n
解题思路:
可以把abc其中任意两个看作一个整体,例如a*b=d,那么可以发现d*c=n
所以d和c是n的因子
易得a和b也是n的因子
所以可以往n的因子这方面去考虑
题目就变成,是否存在3个不互相同的且大于等于2的n的因子,使得这三个因子的乘积为n
循环i=2~sqrt(n),找出所有n的因子i和对应的n/i,储存起来
又可以想到,如果abc其中有任意一个数大于sqrt(n),那么剩下两个数的乘积必定小于sqrt(n)
所以只需要枚举2~sqrt(n)中的因子,取出两个当作a和b,判断此时的c是否存在即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> fac;//存2~sqrt(n)内的因子
set<ll> mfac;//存除了1和自身的所有的因子
void solve(){
fac.clear();
mfac.clear();
ll n,i,j,d,d2,cnt;
cin>>n;
d=sqrt(n);
for(i=;i<=d;i++)
if(n%i==){
fac.push_back(i);
mfac.insert(i);
mfac.insert(n/i);
}
cnt=fac.size();
for(i=;i<cnt;i++)
for(j=i+;j<cnt;j++){
d=fac[i]*fac[j];//枚举两个数乘积
d2=n/d;
if(n%d==&&d2!=fac[i]&&d2!=fac[j]&&mfac.find(d2)!=mfac.end()){//如果乘积d是n的因子,且此时三个数互不相同,且n/d也是n的因子,说明找到了答案
cout<<"YES\n"<<fac[i]<<' '<<fac[j]<<' '<<n/d<<'\n';
return;
}
}
cout<<"NO\n";
}
int main(){
ios::sync_with_stdio();
cin.tie();cout.tie();
int T;cin>>T;
while(T--)
solve(); return ;
}
另,还可以根据a,b,c都为n的因子这个定理,在找到第一个因子a后,把b,c看作是n/a的因子
那么就可以只找两个因子就结束循环直接进行判断
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,i,cnt=,d,ar[];
cin>>n;
for(i=;i*i<=n;i++)
if(n%i==){
ar[cnt++]=i;
n/=i;
if(cnt==)
break;
}
if(cnt==){
ar[]=n;
sort(ar,ar+);
if(ar[]!=ar[]&&ar[]!=ar[]){
cout<<"YES\n"<<ar[]<<' '<<ar[]<<' '<<ar[]<<'\n';
return;
}
}
cout<<"NO\n";
}
int main(){
ios::sync_with_stdio();
cin.tie();cout.tie();
int T;cin>>T;
while(T--)
solve(); return ;
}
Codeforces 1294C - Product of Three Numbers的更多相关文章
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- Educational Codeforces Round 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- [leetcode-628-Maximum Product of Three Numbers]
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- Docker Java 例子
版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...
- c++ opencv 动态内存
1.CvMemStorage定义动态内存存储器 内存存储器是一个用来存储诸如序列.轮廓.图形和子划分等动态增长数据结构的底层结构 2.示例 CvMemStorage *mems = cvCreat ...
- BGP的地址聚合
aggregate-address ip make——多用于地址聚合命令. 但,因此很容易产生路由环路. 所以多加一个参数. as-set as-set,可以是BGP聚合路不丢失原来的AS-Path属 ...
- 对DataFrame的再理解
1.构造需要从字典构造 cds={'code':["002372.XSHE","002415.XSHE","002304.XSHE",&qu ...
- 【新年呈献】高性能网络通信框架 HP-Socket v5.7.1
项目主页 : http://www.oschina.net/p/hp-socket 开发文档 : https://www.docin.com/p-2287339564.html 下载地址 : http ...
- jquery判断当前浏览器是否是IE
if (window.ActiveXObject || "ActiveXObject" in window){ layer.msg("This page does not ...
- 判断单链表是否有环,并找出环的入口python
1.如何判断一个链表是否有环? 2.如果链表为存在环,如果找到环的入口点? 1.限制与要求 不允许修改链表结构. 时间复杂度O(n),空间复杂度O(1). 2.思考 2.1判断是否有环 如果链表有环, ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 存储类
存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期.这些说明符放置在它们所修饰的类型之前.下面列出 C++ 程序中可用的存储类: auto register static extern m ...
- Day 5 :ArrayList原理、LinkedList原理和方法和迭代器注意事项
迭代器在变量元素的时候要注意事项: 在迭代器迭代元素 的过程中,不允许使用集合对象改变集合中的元素个数,如果需要添加或者删除只能使用迭代器的方法进行操作. 如果使用过了集合对象改变集合中元素个数那 ...
- Fiddler 自动响应
使用目的:提前设置接口返回规则 便于前端联调 不用每次断点修改数据 操作: