题目大意:

给定一个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的更多相关文章

  1. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  2. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  3. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  4. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  5. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  6. [leetcode-628-Maximum Product of Three Numbers]

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  7. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  8. [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  9. 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

随机推荐

  1. ELK 安装Beat

    章节 ELK 介绍 ELK 安装Elasticsearch ELK 安装Kibana ELK 安装Beat ELK 安装Logstash Beat是数据采集工具,安装在服务器上,将采集到的数据发送给E ...

  2. UVA - 11925 Generating Permutations(生成排列)(构造)

    题意:将序列1,2,3,……,n,用不超过2n^2次操作,通过下列操作变成给定序列.(1<=n<=300) 1.交换前两个元素 2.将第一个元素移到最后 分析:因为将序列变成升序更容易操作 ...

  3. 使用FragmentStatePagerAdapter时发现的内存泄露问题

    这篇文章想说的并非是由于使用 FragmentStatePagerAdapter 而导致的内存泄漏,内存泄漏的真正原因和 FragmentStaePagerAdapter 并无直接关联,但是使用 Fr ...

  4. Angular js 复制粘贴

    关于copy到剪切板的实现需要引用Clipboard.min.js https://pan.baidu.com/s/1eStTJlo 页面如下所示,需要实现 点击copy字样 将id为content的 ...

  5. Spring中的注解——@nullable和@notnull

    @nullable和@nutNull 在写程序的时候你可以定义是否可为空指针.通过使用像@NotNull和@Nullable之类的annotation来声明一个方法是否是空指针安全的.现代的编译器.I ...

  6. java基础——深拷贝和浅拷贝的区别

    浅拷贝:对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝,此为浅拷贝. 深拷贝:对基本数据类型进行值传递,对引用数据类型船舰一个新的对象,并复制内容,这是深拷贝.

  7. English Words and Expressions

    Words apprentice 学徒 corny 老套的 obnoxious 作呕的 intermezzo 间奏曲 predate 日期上早于 parenthesis 括号 delimiter 分隔 ...

  8. HDU 5312:Sequence

    Sequence  Accepts: 25  Submissions: 1442  Time Limit: 2000/2000 MS (Java/Others)  Memory Limit: 2621 ...

  9. dbcp连接池的一些方法

    创建连接 // 简写版: Connection conn =null; Statement st =null; conn = DBCP.getConnection(); st=conn.createS ...

  10. vscode-wechat 小程序开发提示工具 vscode 安装

    vscode 安装 vscode-wechat vscode-wechat 小程序开发提示工具 ---- 有了小程序开发提示,开发很方便 https://segmentfault.com/a/1190 ...