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. ...
随机推荐
- 第二篇MTV模型、基本命令、简单配置
MTV模型.基本命令.简单配置 阅读目录(Content) MTV模型 基本命令 简单配置 MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Tem ...
- linux crash工具安装配置
crash简介 crash是redhat的工程师开发的,主要用来离线分析linux内核转存文件,它整合了gdb工具,功能非常强大.可以查看堆栈,dmesg日志,内核数据结构,反汇编等等.crash支持 ...
- [BJDCTF2020]The mystery of ip
0x00 知识点 SSTI模板注入: 之前也写过: https://www.cnblogs.com/wangtanzhi/p/12238779.html SSTI模板注入: 模板注入涉及的是服务端We ...
- Windows环境安装与搭建node.js环境
参考文章:https://www.cnblogs.com/zhouyu2017/p/6485265.html 一.下载node.js,直接下一步至安装完成.https://nodejs.org/en/ ...
- BZOJ:2186: [Sdoi2008]沙拉公主的困惑
问题:可能逆元不存在吗? 题解: Gcd(a,b)==Gcd(b,a-b); 从数据范围可以看出应该求M!的欧拉函数: 然后通过Gcd转化过去 一开始没想到 #include<iostream& ...
- 剑指offer_1.18_Day_2
怠惰怠惰,好好练练了要 二维数组中查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个 ...
- JAVAEE 和项目开发(第二课:HTTP协议的特点和交互流程)
HTTP 的概念和介绍 概念:超文本传输协议(Hyper Text Transfer Protocol) 作用:规范了浏览器和服务器的数据交互 特点: 简单快速:客户向服务器请求服务时,只需传送请求方 ...
- WindowsForm ComboBoxList 下拉框带复选框 可以动态添加
先来张效果图: 1.这里需要对控件进行重写,详细内容如下,对此不感兴趣的可以直接跳过这步,下载本人生成的dll,直接看第二小结,下载链接https://pan.baidu.com/s/1gfzrK5t ...
- Window Nginx安装
1.下载Nginx 下载地址:http://nginx.org/en/download.html 我这里下载的版本是: nginx/Windows-1.12.2 2.解压Nginx 把下载下来的zip ...
- 使用那各VUE的打印功能(print.js)出现多打印一个空白页的问题
最近这段时间,用VUE写东西,有个打印功能. 百度了一下,铺天盖地的VUE打印的两种实现方法. 很感激这些千篇一律的帖子,虽然不知道他们是否真的用过,还是只是复制粘贴. 至少这些帖子告诉我,是有两个可 ...