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. ...
随机推荐
- Spring开发环境搭建(Eclipse)
开发环境搭建,主要包含2部分: Java安装 Eclipse安装 为易于学习,我们只安装这2个部分,对于一般开发学习也足够了.如果你有其他要安装的,酌情添加. Java安装 我们使用Java8: 下载 ...
- 启用sql日志
SHOW VARIABLES LIKE "general_log%"; -- 查询是否启用日志 SET GLOBAL general_log = 'ON'; -- 设置启用 SE ...
- c++程序—输入
#include<iostream> using namespace std; #include<string> int main() { string str = " ...
- JS - 使 input 失去焦点
$(document).ready(function(){ $("body").click(function(){ if(!event.srcElement.type) { ...
- 19 ~ express ~ 文章的增加 , 查看 ,修改 ,删除
一,前台 1,添加文章 /views/admin/content_add.html {% extends 'layout.html' %} {% block main %} <ol class= ...
- cf 758D - Ability To Convert
从后往前贪心就好了.各种各样0的情况太BT了.. (各种爆long long,f**k) #include<bits/stdc++.h> #define LL long long #def ...
- TP中统计指定字段的总数
如统计已激活设备数量和未激活设备数量 $condition = [ ['member_id', '=', $member_id] ]; $field = [ 'COUNT(IF(active_memb ...
- SQL中的LEFT/RIGHT/SUBSTRING函数
语法: LEFT(field,length) RIGHT(field,length)SUBSTRING(field,start,length) LEFT/RIGHT函数返回field最左边/最右边的l ...
- 关于torch.norm函数的笔记
先看一下它的参数: norm(p='fro', dim=None, keepdim=False, dtype=None) p: the order of norm. 一般来说指定 $p = 1, 2$ ...
- UML-使用多态性和“Do It Myself”模式处理支付
1.概念 "Do It Myself"使用多态性(和信息专家),这是常见的方法. Do It Myself:我(一个软件对象)是对实际对象的抽象,由我来完成这些通常由实际对象所完成 ...