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

来源:互联网 发布:centos7 nat网络配置 编辑:程序博客网 时间:2024/05/16 12:23

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;    }}
原创粉丝点击