[CF797A] k-Factorization(分解因数,构造)

题目链接:http://codeforces.com/contest/797/problem/A

题意:给一个n,一个k。问用k个不是1的数的乘积表示n,能不能表示,能的话输出解。

把n分解质因数,看看一共有多少质因数。输出前k-1个因数,最后一个数用这个数去除以之前的乘积。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 vector<int> a, p;
 6 int n, k;
 7 
 8 int main() {
 9     // freopen("in", "r", stdin);
10     while(~scanf("%d%d",&n,&k)) {
11         int m = n, tot = 0;
12         a.clear(); p.clear();
13         for(int i = 2; i * i <= m; i++) {
14             if(m % i == 0) {
15                 int cnt = 0;
16                 while(m % i == 0) {
17                     m /= i;
18                     cnt++;
19                 }
20                 tot += cnt;
21                 p.push_back(i);
22                 a.push_back(cnt);
23             }
24         }
25         if(m != 1) {
26             tot++;
27             p.push_back(m);
28             a.push_back(1);
29         }
30         if(tot < k) {
31             puts("-1");
32             continue;
33         }
34         int tmp = 1;
35         for(int i = 0; i < p.size(); i++) {
36             while(a[i] && k - 1) {
37                 printf("%d ", p[i]);
38                 tmp *= p[i];
39                 a[i]--; k--;
40             }
41         }
42         printf("%d\n", n / tmp);
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/kirai/p/6845494.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值