题目大意:

给定一个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. 吴裕雄--天生自然C++语言学习笔记:C++ 基本的输入输出

    C++ 的 I/O 发生在流中,流是字节序列.如果字节流是从设备(如键盘.磁盘驱动器.网络连接等)流向内存,这叫做输入操作.如果字节流是从内存流向设备(如显示屏.打印机.磁盘驱动器.网络连接等),这叫 ...

  2. JavaScript获取当前日期前后7天的日期

    function fun_date(aa){ var date1 = new Date(), time1=date1.getFullYear()+"-"+(date1.getMon ...

  3. jedis哨兵模式的redis组(集群),连接池实现。(客户端分片)

    java 连接redis 我们都使用的 是jedis  ,对于redis这种频繁请求的场景我们一般需要对其池化避免重复创建,即创建一个连接池 ,打开jedis的 jar包我们发现,jedis对池已经有 ...

  4. 通过while循环一步步实现九九乘法表

    # 打印#做出@列的效果height = int(input("height: ")) #用户输入一个高度 num_height = heightwhile num_height ...

  5. 新部署到服务器 报 The requested URL /home/profession was not found on this server. 错误

    The requested URL /home/profession was not found on this server. 通过xxx.com, 首页可以正常访问,xxx.com/xx/xx 就 ...

  6. windows driver 映射大文件

    //如果要做到掉电后仍然可以继续向下操作,可以记录文件的位置重新映射 NTSTATUS status; UNICODE_STRING strFileSrc = RTL_CONSTANT_STRING( ...

  7. tx2系统备份与恢复

    https://blog.csdn.net/ycy_dy/article/details/80493392 https://blog.csdn.net/piaopiaopiaopiaopiao/art ...

  8. jq监听

    $(window).resize(function () {//风电月表格自适应高度 var handHeight =$(".tree-handler").height() $(& ...

  9. WebSocket实现简易聊天室

    前台页面: <html> <head> <meta http-equiv="Content-Type" content="text/html ...

  10. 201771010123汪慧和《面向对象程序设计Java》第十五周实验总结

    一.理论部分 1.JAR文件 (1)Java程序的打包:程序编译完成后,程序员将.class文件压缩打包为.jar文件后,GUI界面 程序就可以直接双击图标运行. (2).jar文件(Java归档)既 ...