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. ...
随机推荐
- 干货分享|留学Essay怎么写?
留学生活其实就是分割成一个个deadline,留学就是赶完一个又一个deadline.朋友同学的革命情感源自赶一个个deadline时候的不离不弃,相知相守,无数个夜里大家群里打卡,你今天Essay写 ...
- 打开文件管理器Device File Explorer
版本Android Studio3.2
- Maven的安装和创建项目的过程
一.下载Maven包和配置环境变量 1.将下载好的maven包放到一个目录中:目录中不能有汉字和空格 2.配置环境变量 3.配置path路径 二.配置阿里云私服 1.找到setting目录,配置下载j ...
- AI 人工智能产业园路口-----dp
北京市商汤科技开发有限公司建立了新的 AI 人工智能产业园,这个产业园区里有 nn 个路口,由 n - 1n−1 条道路连通.第 ii 条道路连接路口 u_iui 和 v_ivi. 每个路口都布有 ...
- VM安装CentOS7步骤
VM15下载,在360软件管家就可以下载 CentOS7下载地址:http://mirror.bit.edu.cn/centos/7.6.1810/isos/x86_64/CentOS-7-x86_6 ...
- SQL中的LEFT/RIGHT/SUBSTRING函数
语法: LEFT(field,length) RIGHT(field,length)SUBSTRING(field,start,length) LEFT/RIGHT函数返回field最左边/最右边的l ...
- hihocoder:#1082 : 然而沼跃鱼早就看穿了一切(用string)
题目是这样的: 描述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼("marshtomp",不区分大小写).为了使句子不缺少成分, ...
- js获取浏览器窗口大小
摘抄:https://blog.csdn.net/qq_27628085/article/details/81947478 常用: JS 获取浏览器窗口大小 // 获取窗口宽度 if ...
- POJ 3258:River Hopscotch 二分的好想法
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9326 Accepted: 4016 D ...
- BZOJ [HAOI2006]旅行comf
题解:枚举最大边,然后对<=最大边的边做最大生成树,使最小边最大 #include<iostream> #include<cstdio> #include<cstr ...