A. k-Factorization
来源:互联网 发布:农村淘宝服务站利润 编辑:程序博客网 时间:2024/05/22 11:53
http://codeforces.com/problemset/problem/797/A
Given a positive integer n, find k integers (not necessary distinct) such that all these integers are strictly greater than1, and their product is equal to n.
The first line contains two integers n andk (2 ≤ n ≤ 100000,1 ≤ k ≤ 20).
If it's impossible to find the representation of n as a product ofk numbers, print -1.
Otherwise, print k integers in any order. Their product must be equal ton. If there are multiple answers, print any of them.
100000 2
2 50000
100000 20
-1
1024 5
2 64 2 2 2
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int ans[100005];
int a[100005];
int main()
{
a[1]=0;
a[2]=1;
a[3]=1;
for(int i=2;i<=100005;i++)
a[i]=1;
for(int i=2;i<=100005;i++)
{
if(a[i]==1)
{
for(int j=2*i;j<=100005;j+=i)
a[j]=0;
}
}
int n,k;
while(~scanf("%d%d",&n,&k))
{
if(k==1)printf("%d\n",n);
else
{
int nn=n;
int flog=0; int r=1;
for(int len=2;len<=nn;len++)
{
if(a[len]==1&&n%len==0)
{
ans[r]=len;
n=n/len;
len--; r++;
}
if(n==1)
{
printf("-1\n");
flog=1;
break;
}
if(r==k)
{
ans[r]=n;
break;
}
}
if(flog==0)
for(int i=1;i<=r;i++)
{
if(i==r)
printf("%d\n",ans[i]);
else
printf("%d ",ans[i]);
}
}
}
}
- A. k-Factorization
- codeforces A. k-Factorization
- A. k-Factorization
- CodeForces 797A k-Factorization
- Codeforces 797A k-Factorization
- Codeforces 797A k-Factorization
- Educational Codeforces Round 19 A. k-Factorization
- codeforces] ECR19 A. k-Factorization [数论]
- 【CodeForces 797A】k-Factorization(分解质因数)
- k-Factorization CF
- pat a 1103 integer factorization
- Factorization
- PAT(A) - 1103. Integer Factorization (30)
- PAT(A) 1103 Integer Factorization (30)
- Pat(A) 1103. Integer Factorization (30)
- Matrix Factorization: A Simple Tutorial and Implementation in
- Matrix Factorization: A Simple Tutorial and Implementation in Python
- Matrix Factorization: A Simple Tutorial and Implementation in Python
- CCF NOI1065 最小公倍数
- C# 判断文件是否被占用的两种方法
- 基于proteus的51单片机仿真实例四十一、C语言的一维数组
- Linux bash shell之declare
- PYTHON中的语法元素--Python(7)
- A. k-Factorization
- win10 UEFI 启动安装 Ubuntu 双系统
- Storm实战常见问题及解决方案
- Echarts 数据可视化图表库 Overview
- 再探java基础——throw与throws
- 【最小生成树】(模板-prim算法)poj 1258
- AndroidStudio 获取SHA1值
- 设计模式之职责链模式
- 不使用if、?:、switch及其他判断语句如何找出两个int型变量中的最大值和最小值