A. k-Factorization

本文探讨了k-Factorization问题,即给定一个正整数n,寻找k个严格大于1的整数,使它们的乘积等于n。文章提供了具体的解决方法,并通过示例演示了解决方案的有效性。
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
100000 2
output
2 50000 
input
100000 20
output
-1
input
1024 5
output
2 64 2 2 2 

水题:可以想到只要从最小数开始判断因数,一旦是该数的因数就记入结果数组,同时更新该数,当已经统计到结果数组的因数个数达到k-1个时,直接将最后的数记入结果数组即可,这里需要判断一下,因为最后一个结果因数不可为1.
代码如下:
//
//  main.cpp
//  A. k-Factorization
//
//  Created by 徐智豪 on 2017/4/15.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include <iostream>
using namespace std;
int k,n;int tcount=1;
int flag=1;
int ans[1000];
void print()
{
    tcount=1;
    int temp=k;
    if(n==1)
    {
        ans[tcount]=k;
        return ;
    }
    int i;
    for( i=2;i<=k;i++)
    {
        while(temp%i==0)
        {
            ans[tcount++]=i;
            temp/=i;
            if(tcount==n&&temp>1)
            {
                ans[tcount]=temp;
                return ;
            }
            else if(tcount==n&&temp==1)
            {
                flag=0;
            }
        }
    }
    if(i==k+1)
    {
        flag=0;
        return ;
    }
    
}
int main(int argc, const char * argv[]) {
    cin>>k>>n;
    print();
    if(flag)
    {
        for(int i=1;i<tcount;i++)
    {
        cout<<ans[i]<<" ";
    }
        cout<<ans[tcount]<<endl;
    }
    
    else cout<<-1<<endl;
    return 0;
}







C. XOR-factorization time limit per test2 seconds memory limit per test256 megabytes    Ostad thinks that the usual way of factoring numbers is too mathematical, so he invented a new notion called XOR-factorization, which is more computer-science-like. For a given integer n , a sequence of integers a1,a2,…,ak with 0≤ai≤n for all i is called a XOR-factorization of n if and only if a1⊕a2⊕⋯⊕ak=n, where ⊕ denotes the bitwise XOR operation. You are given integers n and k . Find a XOR-factorization a1,a2,…,ak of n that maximizes the sum a1+a2+⋯+ak . It can be proven that under the problem conditions, a XOR-factorization always exists. DeepL 翻译    奥斯塔德认为通常的因式分解方法过于数学化,因此他发明了一种新的概念,称为 XOR 因式分解,这种方法更类似于计算机科学。对于给定的整数 n ,当且仅当符合以下条件时,所有 i 的整数 a1,a2,…,ak 与 0≤ai≤n 的序列称为 n 的 XOR 因式分解 a1⊕a2⊕⋯⊕ak=n, 其中 ⊕ 表示位向 XOR 运算。 给定整数 n 和 k 。请找出使总和 a1+a2+⋯+ak 最大的 n 的 XOR 因式分解 a1,a2,…,ak 。 可以证明,在问题条件下,XOR 因式分解总是存在的。    Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. Each of the next t lines contains two integers n and k (1≤n≤109 , 1≤k≤105 ). It is guaranteed that the sum of k over all test cases does not exceed 105 . DeepL 翻译    输入 每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。测试用例说明如下。 接下来的每行 t 包含两个整数 n 和 k ( 1≤n≤109 , 1≤k≤105 )。 保证所有测试用例中 k 的总和不超过 105 。    Output For each test case, output k integers a1,a2,…,ak such that 0≤ai≤n . We can show that an answer always exists. If there are multiple valid answers, you may print any of them in any order. DeepL 翻译    输出 对于每个测试用例,输出 k 个整数 a1,a2,…,ak ,使得 0≤ai≤n . 我们可以证明答案总是存在的。如果存在多个有效答案,可以按任意顺序打印其中任何一个。 Example InputCopy 4 5 4 4 3 8 2 1 1 OutputCopy 1 4 5 5 4 4 4 0 8 1    Note In the first test case, we can factor 5 as 1⊕4⊕5⊕5 with a sum of 15 , and it can be shown that no other XOR-factorization has a higher sum. In the second test case, we can factor 4 as 4⊕4⊕4 with a sum of 12 , which is trivially the maximum possible.用c++
最新发布
12-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值