CodeForces 797A 以及关于质数筛选和质因数分解的一堆东西

k-Factorization

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.

Example
Input
100000 2
Output
2 50000
Input
100000 20
Output
-1
Input
1024 5
Output
2 64 2 2 2

今次的训练赛简直是一场噩梦,我对质数素数之类的一窍不通……
于是乎恶补关于这方面的知识……
质数是什么
质数和素数是一个意思,表示只能被自己和1整除的数

小型素数判断

bool isPrimer(int n)
{
    if(n==2)return 1;
    if(n%2==0||n<2) return 0;
    for(int i=3;i<=sqrt(n+1);i+=2)
        if(n%i==0) return 0;
    return 1;   
}

唯一分解定理
任何一个大于1的自然数N,若N不为质数,那么N可以唯一分解成有限个质数的乘积

int n;
int c=0;
int ans[100];
for(int i=2;i<=n;i++)
{
        if(n%i==0)
    {  
        n/=i;
        ans[c++]=i;
    }

}

回到本题题解

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n,k,j=0;
    int ans[25];
    scanf("%d%d",&n,&k);
    for(int i=2;i<=n;i++)
    {
        while(n%i==0)
            {
                n/=i;
                ans[j++]=i;
            }
    }
    if(j<k)
     cout<<-1<<endl;
    else
    {
        for(int i=0;i<k-1;i++)
          cout<<ans[i]<<" ";
            int sum=1;
        for(int i=k-1;i<j;i++)
            sum*=ans[i];
            cout<<sum<<endl;

    }




}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值