深夜切题——Educational Codeforces Round 19

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 ≤ 1000001 ≤ 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 

今天由于校OJ崩了,不知道干嘛就咸鱼了一天。。。(其实前后没关系- -)所以深夜切切难度合适我这种新手的题目(CF中部分A题>.<)。。。但是这样我也照样错呀!!!
第一次忘记限制最后一个数要满足>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了吧(或者我都不看)(雾
洗洗睡!~




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值