zoukankan      html  css  js  c++  java
  • Codeforces 797A

    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 
    题目大意:输入两个数n和k,找k个大于1的整数,使他们的乘积为n,输出这k个数(如果有多个,输出任意一组),如果没有,输出-1。
    方法:把n分解成质因数的乘积,并把这些质因数保存在数组里。如果质因数的个数c小于k,输出-1;否则,输出前k-1个质因数和k到c个质因数的乘积(这样就刚好k个)。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int N=1e5;
    int main()
    {
        int n,k;
        cin>>n>>k;
        int f[N];
        int c=0;
        for(int i=2;i<=n;i++)
        {
            while(n%i==0)
            {
                n/=i;
                f[c++]=i;
            }
        }
        if(c<k)cout<<-1<<endl;
        else 
        {
            for(int i=0;i<k-1;i++)
            cout<<f[i]<<' ';
            int sum=1;
            for(int i=k-1;i<c;i++)
            sum*=f[i];
            cout<<sum<<endl;
        }
        return 0;
    }
  • 相关阅读:
    windows“画图”工具用法
    数字信号处理的流程
    怎样去掉桌面图标蓝色阴影
    Linux下Wi-Fi配置工具2
    vs2005下面编译自己的luars232.dll
    [spring]03_装配Bean
    [Java IO]02_字节流
    [Java IO]01_File类和RandomAccessFile类
    [Spring]01_环境配置
    Notepad++ 实用技巧
  • 原文地址:https://www.cnblogs.com/widsom/p/6717790.html
Copyright © 2011-2022 走看看