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. ...
随机推荐
- oracle问题:char类型数据查询不到
select distinct id,name from test_table b where b.ID='001' ; id为char字段类型,使用该语句查询不出数据. 解决方法:加trim().改 ...
- CF1141E Superhero Battle
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minu ...
- 第十二篇视图层之视图函数(views)-三件套
视图层之视图函数(views) 阅读目录(Content) 视图层之视图函数(views) 一个简单的视图 HttpRequest HttpResponse redirect 函数 对比render与 ...
- java嵌套循环练习
打印一个等腰三角形 package com.lv.jj; import java.util.Scanner; public class DemoDy { public static void main ...
- 每天一点点之vue框架开发 - vue 动态替换路由(地址栏)参数
import merge from 'webpack-merge': // 修改原有参数 this.$router.push({ query:merge(this.$route.query,{'max ...
- 通过Request获取客户端的真实IP
我们在做项目的时候经常需要获取客户端的真实ip去进行判断,为此搜索了相关文章,以下这个讲解的比较明白,直接拿来 https://blog.csdn.net/yin_jw/article/details ...
- recipe for target 'vmnet.ko' failed
/tmp/modconfig-60OpuH/vmnet-only/bridge.c:639:4: error: invalid preprocessing directive #atomic_inc ...
- Python中的抽象基类
1.说在前头 "抽象基类"这个词可能听着比较"深奥",其实"基类"就是"父类","抽象"就是&quo ...
- DRF框架之序列化器初体验
首先,我们需要明白序列化和反序列化的过程指的是什么. 序列化操作:将模型数据 ---> 字典数据 --->JSON数据(响应JSON数据的操作) 反序列化操作:将JSON数据 ---> ...
- 2018.11.16javascript课上随笔(DOM)
<li> <a href = "“#”>-</a> </li> <li>子节点:文本节点(回车),元素节点,文本节点. 不同节点树 ...