CodeForces 797A k-Factorization

质因数分解。

分解一下质因数,如果个数小于$k$个,则无解,否则把多出来的合成一个数。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

int b[100010],sz;
int n,k;
int a[100010];
vector<int>ans;

bool prime(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0) return 0;
	}
	return 1;
}

void init()
{
	for(int i=2;i<=100000;i++)
	{
		if(prime(i)) b[sz++] =i;
	}
}

int main()
{
	init();
	scanf("%d%d",&n,&k);

	for(int i=0;i<sz;i++)
	{
		while(n%b[i]==0)
		{
			ans.push_back(b[i]);
			n=n/b[i];
		}
		if(n==1) break;
	}

	if(ans.size()<k)
	{
		printf("-1\n");
	}
	else
	{
		for(int i=0;i<k-1;i++)
		{
			printf("%d ",ans[i]);
		}
		int p=1;
		for(int i=k-1;i<ans.size();i++)
		{
			p=p*ans[i];
		}
		printf("%d\n",p);
	}

	return 0;
}
时间: 2024-10-12 10:10:12

CodeForces 797A k-Factorization的相关文章

codeforces 219a k重复字符串。

背景:周赛d题,当时没读题,怎么知道其实有点水的,也是自己codeforces第一题吧,仰慕torist神牛.! #include<stdio.h> #include<string.h> int a[26],n=0; char b[1009]; bool ok(void); bool ok(void) { for(int j=0;j<26;j++) if(a[j]%n!=0) return false; return true; } int main(void) { whil

codeforces 1133E K Balanced Teams

题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的总和的最大值. 这个题写DP很多的人应该可以很快写出来吧,毕竟不是很难. 思路: 反正最多n^2种状态.用数组存就好了. 先排序. 对于每个点,dp[a][b].表示a到n区间里分b个队伍的答案. 如果a是答案dp[a][b]所需要的,那么我们从a开始到a+i暴力一下(此时,x[a+i]-x[a]>

融合后如何如何后如何如何

http://ypk.39.net/search/all?k=%A6%C6%B6%F5%D6%DD%C4%C4%C0%EF%C2%F2%C3%C0%C9%B3%CD%AAQ%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%C6 http://ypk.39.net/search/all?k=%A8z%BE%A3%C3%C5%C4%C4%C0%EF%C2%F2%C3%C0%C9%B3%CD%AAQ%A3%BA%A3%B8%

哥哥ukulele

http://ypk.39.net/search/all?k=%A1%FB%C6%CE%CC%EF%C4%C4%C0%EF%D3%D0%B0%B2%C0%D6%CB%C0%D2%A9%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%BC http://ypk.39.net/search/all?k=%A1%D3%B8%A3%C7%E5%C4%C4%C0%EF%D3%D0%B0%B2%C0%D6%CB%C0

Codeforces Gym 100187K K. Perpetuum Mobile 构造

K. Perpetuum Mobile Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description The world famous scientist Innokentiy almost finished the creation of perpetuum mobile. Its main part is the energy generator whic

CodeForces 61E Enemy is weak 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组

题目链接:点击打开链接 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 如果只有2元组那就是求逆序数的做法 三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数 然后每次给这个y数组求和,再把x中>a[i]的个数存入y中即可 #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #in

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

三元逆序对 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组Codeforces 61E Enemy is weak

http://codeforces.com/problemset/problem/61/E E. Enemy is weak time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output The Romans have attacked again. This time they are much more than the Persian

Codeforces 463D Gargari and Permutations(求k个序列的LCS)

题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n<=1000),每个序列中的数字分别为1~n,求着k个序列的最长公共子序列是多长?解题思路:由于每个序列的数字分别为1~n即各不相同,所以可以用pos[i][j]记录第i个序列中j的位置.设dp[i]表示以i结尾的最长公共子序列长度,那么我们可以按顺序遍历第一个序列的位置i,再在第一个序列中枚举位置j(j<