Codeforces 797A k-Factorization
2017-11-22 20:02
375 查看
A. k-Factorization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Given a positive integer n, find k integers
(not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.
Input
The first line contains two integers n and k (2 ≤ n ≤ 100000, 1 ≤ k ≤ 20).
Output
If it's impossible to find the representation of n as a product of k numbers,
print -1.
Otherwise, print k integers in any order. Their product must be equal to n.
If there are multiple answers, print any of them.
Examples
input
output
input
output
input
output
虽然这道题很水,但是我不会写.....
题目大意:给你一个n,分解为k个数,这k个数可以相同,相乘要等于n
根据唯一分解定理,将n分解为质因数的形式
#include<iostream>
#include<vector>
using namespace std;
vector<int> vec;
void fun(int n){
for(int i=2;i*i<=n;i++){
while(n%i==0){
vec.push_back(i);
n/=i;
}
}
if(n!=1) vec.push_back(n);
}
int main(){
int n,k;
while(cin>>n>>k){
vec.clear();
fun(n);
if(vec.size()<k) cout<<-1<<endl;
else{
for(int i=0;i<k-1;i++){
cout<<vec[i]<<' ';
}
int ans=1;
for(int i=k-1;i<vec.size();i++){
ans*=vec[i];
}
if(ans>1){
cout<<ans<<endl;
}
}
}
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Given a positive integer n, find k integers
(not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.
Input
The first line contains two integers n and k (2 ≤ n ≤ 100000, 1 ≤ k ≤ 20).
Output
If it's impossible to find the representation of n as a product of k numbers,
print -1.
Otherwise, print k integers in any order. Their product must be equal to n.
If there are multiple answers, print any of them.
Examples
input
100000 2
output
2 50000
input
100000 20
output
-1
input
1024 5
output
2 64 2 2 2
虽然这道题很水,但是我不会写.....
题目大意:给你一个n,分解为k个数,这k个数可以相同,相乘要等于n
根据唯一分解定理,将n分解为质因数的形式
#include<iostream>
#include<vector>
using namespace std;
vector<int> vec;
void fun(int n){
for(int i=2;i*i<=n;i++){
while(n%i==0){
vec.push_back(i);
n/=i;
}
}
if(n!=1) vec.push_back(n);
}
int main(){
int n,k;
while(cin>>n>>k){
vec.clear();
fun(n);
if(vec.size()<k) cout<<-1<<endl;
else{
for(int i=0;i<k-1;i++){
cout<<vec[i]<<' ';
}
int ans=1;
for(int i=k-1;i<vec.size();i++){
ans*=vec[i];
}
if(ans>1){
cout<<ans<<endl;
}
}
}
}
相关文章推荐
- Codeforces 797A k-Factorization
- CodeForces 797A k-Factorization
- CodeForces 797A k-Factorization
- codeforces] ECR19 A. k-Factorization [数论]
- 【CodeForces 797A】k-Factorization(分解质因数)
- Codeforces 16D logging 水题
- CodeForces 415B Mashmokh and Tokens
- Codeforces 672D Robin Hood
- CodeForces 37d
- Codeforces 742 C Arpa's loud Owf and Mehrdad's evil plan
- CodeForces 893A Chess For Three
- Codeforces 443 B. Kolya and Tandem Repeat
- codeforces 180C C. Letter(dp)
- codeforces 676B Pyramid of Glasses(模拟题)
- Codeforces 525E Anya and Cubes【折半枚举法+剪枝Dfs】好题~
- CodeForces 828B Black Square
- codeforces 893F Subtree Minimum Query 线段树合并
- Codeforces 441 B. Valera and Fruits
- Codeforces 583c GCD Table
- codeforces 115A