codeforces-797A-k-Factorization

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 ≤ 1000001 ≤ 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
Copy
100000 2
output
Copy
2 50000 
input
Copy
100000 20
output
Copy
-1
input
Copy
1024 5
output
Copy
2 64 2 2 2 

题目大意:给你个数n让你找到k个大于1的数并使它们的乘积等于n;

思路:素数唯一分解定理

代码:

#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
int mark[11100],a[11100],aa[11111];
void prime()
{
    int i,j,q=0;
    memset(mark,0,sizeof(mark));
    mark[1]=1;
    for(i=2; i<=31000; i++)
    {
        if(mark[i]==0)
        {
            for(j=i*i; j<=31000; j+=i)
            {
                mark[j]=1;
            }
        }
    }
    for(i=2; i<=31000; i++)
    {
        if(mark[i]==0)
        {
            a[q++]=i;
        }
    }
}
int main()
{
    int m,n;
    prime();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(aa,0,sizeof(aa));
        int i=0,k=0;
        if(m==1)
        {
            printf("%d\n",n);
            continue;
        }
        i=0;
        while(1)
        {
            if(n==1)
                break;
            if(k==m-1)
            {
                aa[k++]=n;
                break;
            }
            if(n%a[i]==0)
            {
                aa[k++]=a[i];
                n/=a[i];
            }
            else
                i++;
        }
        if(k==m)
        {
            printf("%d",aa[0]);
            for(i=1; i<k; i++)
            {
                printf(" %d",aa[i]);
            }
            printf("\n");
        }
        else
            printf("-1\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值