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; }}
- CodeForces 797A 以及关于质数筛选和质因数分解的一堆东西
- 质因数分解和N以内的质数
- 质数构造以及质因数分解模板
- 对 质数筛法 和 质因数分解 的模版实验
- 质因数分解 和 判断是否为质数
- 【CodeForces 797A】k-Factorization(分解质因数)
- codeforces-26A-Almost Prime【分解质因数】
- Java 与质数判断相结合的质因数分解
- 笔试面试3 将一个数分解成质因数的形式以及如何判断一个数是否是质数
- 关于分解质因数的一个算法
- 关于一个分解质因数的问题
- 关于分解质因数问题
- 找质数,分解质因数,找完全数
- POJ1811 大质数判断+质因数分解
- 数的分解、分解质因数
- 质数/素数 和筛选
- 分解质因数的问题
- 分解正整数的质因数
- 贪心 sort
- 从尾到头打印单链表
- kali linux2.0 nessus离线更新插件
- mybatis plus 学习
- Hadoop是一个开源框架
- CodeForces 797A 以及关于质数筛选和质因数分解的一堆东西
- 17.12.5日报
- java mongoDB 获取并使用_id(ObjectId)的值
- php实现周和月起止时间计算方法
- 1. Two Sum
- ADC7608芯片使用
- Python:找到列表中满足某些条件的元素
- 步进电机进阶——控制
- c#--XAML语言介绍