A. k-Factorization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
5 1
output
5
input
5 2
output
-1
input
1024 5
output
2 64 2 2 2
第一次忘记限制最后一个数要满足>2了,所以WA一次
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cmath>
//#include <cctype>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
//#include <sstream>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
#define MP make_pair
#define PB push_back
#define mst(a,b) memset((a),(b),sizeof(a))
#define TEST cout<<"*************************"<<endl
#define rep(s,n,up) for(int i = (s); i < (n); i+=(up))
#define per(n,e,down) for(int i = (n); i >= (e); i-=(down))
#define rep1(s,n,up) for(int j = (s); j < (n); j+=(up))
#define per1(n,e,down) for(int j = (n); j >= (e); j-=(down))
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<Pii> Vii;
const int inf = 0x3f3f3f3f;
const LL INF = (1uLL << 63) - 1;
const double Pi = acos(-1.0);
const int maxn = (1 << 16) + 7;
const uLL Hashmod = 29050993;
const double esp=1e-6;
//#define local
int flag[100001],knb[20],kn=0,fail=0;
int main() {
#ifdef local
freopen("input.txt", "r", stdin);
//freopen("output.txt","w",stdout);
#endif
//ios::sync_with_stdio(0);
//cin.tie();
int n,k;
scanf("%d%d",&n,&k);
mst(flag,0);flag[1]=1,flag[0]=1;
for(int i=2;i<=n;++i)
{
for(int j=2*i;j<=n;j+=i)
flag[j]=1;
}
if(!flag[n])
{
if(k>1)
printf("-1\n");
else
printf("%d\n",n);
}
else
{
int nn=n,kk=1;
while(kk<k){
for(int i=2;i<=n&&kk<k;)
{
if(!flag[nn]||nn<2) {fail=1;goto endd;}
if(!flag[i]){
while(!(nn%i)&&kk<k){
++kk;
knb[kn++]=i;
//printf("%d ",i);
nn/=i;
}
}
++i;
while(flag[i]&&kk<k)
++i;
}
}
endd:
if(fail)
printf("-1\n");
else
{
for(int i=0;i<k-1;++i)
printf("%d ",knb[i]);
printf("%d\n",nn);
}
}
return 0;
}
然后稍稍修改才AC= =。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <cmath> //#include <cctype> #include <queue> #include <vector> #include <string> #include <stack> #include <set> #include <map> //#include <sstream> #include <iostream> #include <bitset> #include <algorithm> using namespace std; #define MP make_pair #define PB push_back #define mst(a,b) memset((a),(b),sizeof(a)) #define TEST cout<<"*************************"<<endl #define rep(s,n,up) for(int i = (s); i < (n); i+=(up)) #define per(n,e,down) for(int i = (n); i >= (e); i-=(down)) #define rep1(s,n,up) for(int j = (s); j < (n); j+=(up)) #define per1(n,e,down) for(int j = (n); j >= (e); j-=(down)) typedef long long LL; typedef unsigned long long uLL; typedef pair<int, int> Pii; typedef vector<int> Vi; typedef vector<Pii> Vii; const int inf = 0x3f3f3f3f; const LL INF = (1uLL << 63) - 1; const double Pi = acos(-1.0); const int maxn = (1 << 16) + 7; const uLL Hashmod = 29050993; const double esp=1e-6; //#define local int flag[100001],knb[20],kn=0,fail=0; int main() { #ifdef local freopen("input.txt", "r", stdin); //freopen("output.txt","w",stdout); #endif //ios::sync_with_stdio(0); //cin.tie(); int n,k; scanf("%d%d",&n,&k); mst(flag,0);flag[1]=1,flag[0]=1; for(int i=2;i<=n;++i) { for(int j=2*i;j<=n;j+=i) flag[j]=1; } if(!flag[n]) { if(k>1) printf("-1\n"); else printf("%d\n",n); } else { int nn=n,kk=1; while(kk<k){ for(int i=2;i<=n&&kk<k;) { if(!flag[nn]||nn<2) {fail=1;goto endd;} if(!flag[i]){ while(!(nn%i)&&kk<k){ ++kk; knb[kn++]=i; //printf("%d ",i); nn/=i; } } ++i; while(flag[i]&&kk<k) ++i; } } endd: if(fail||nn<2) printf("-1\n"); else { for(int i=0;i<k-1;++i) printf("%d ",knb[i]); printf("%d\n",nn); } } return 0; }
不过大概也只有我看这篇blog了吧(或者我都不看)(雾
洗洗睡!~