codeforces #686 div3 D.Number into Sequence 2021.1.4

codeforces #686 div3 D.Number into Sequence

You are given an integer n (n>1).

Your task is to find a sequence of integers a1,a2,…,ak such that:

·each ai is strictly greater than 1;
·a1⋅a2⋅…⋅ak=n (i. e. the product of this sequence is n);
·ai+1 is divisible by ai for each i from 1 to k−1;
·k is the maximum possible (i. e. the length of this sequence is the maximum possible).
If there are several such sequences, any of them is acceptable. It can be proven that at least one valid sequence always exists for any integer n>1.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤5000) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (2≤n≤1010).

It is guaranteed that the sum of n does not exceed 1010 (∑n≤1010).

Output

For each test case, print the answer: in the first line, print one positive integer k — the maximum possible length of a. In the second line, print k integers a1,a2,…,ak — the sequence of length k satisfying the conditions from the problem statement.

If there are several answers, you can print any. It can be proven that at least one valid sequence always exists for any integer n>1.

翻译:

给定一个整数 n (n>1)。

你的任务是查找一系列整数 a1,a2,…,,满足以下条件:

·每个 ai 严格大于 1;
·a1·[a2]·…·ak = n(即此序列的生成为 n);
·ai + 1能被 ai 从1到 k-1的每个 i 整除;
·k 是可能的最大值(即此序列的长度是可能的最大值)。

如果有几个这样的序列,其中任何一个都是可以接受的。可以证明,对于任何整数 n > 1,至少存在一个有效序列。

题目拥有t个独立的测试用例。

输入

输入的第一行包含一个整数 t (1≤ t ≤5000)ー测试用例数。
然后接下来是测试用例。测试用例中唯一的一行包含一个整数 n
(2≤ n ≤10^10) 。保证所有的n之和不超过10 ^10(∑ n ≤10 ^10)。

输出

对于每个测试用例,打印答案:
在第一行,打印一个正整数 k ー最大可能长度为 a。
在第二行中,打印 k 整数 a1,a2,… ,ak ー满足问题陈述中条件的长度 k 序列。如果有几个答案,你可以打印任何一个。对于任意整数 n > 1,可以证明至少有一个有效序列始终存在。

题意大概

分析可知,我们先将10^5内的质数打表。我们要满足有最大长度k且ai + 1能被 ai 从1到 k-1的每个 i 整除,则我们需要前面的数尽可能的小且为相同值,最后一个数为质数乘以前面的相同值,这样才能满足条件。

代码
#include <bits/stdc++.h>
using namespace std;

vector<int>prime;

//判断质数
bool isprime(long long n)
{
    if(n == 2)return true;
    for(int i = 2;i <= sqrt(n);i++){
        if(n % i == 0)return false;
    }
    return true;
}
int main()
{
    vector<int>prime;
    for(int i = 2;i <= 1e5;i++){//质数打表
        if(isprime(i))prime.push_back(i);
    }
    int t;
    long long n;
    cin >> t;
    while(t--){
        cin >> n;
        int maxn = 0;
        int num = -1;
        long long ans = n;//存储n
        for(int i = 0;i < prime.size();i++){//遍历所有的质数
            int cnt = 0;
            while(n % prime[i] == 0){//如果存在质数
                cnt++;
                n /= prime[i];
                //cout << cnt << endl;
            }
            if(maxn < cnt){//存储最大的数组长度
                maxn = cnt;
                num = prime[i];
            }
        }
        if(maxn >= 2){
            cout << maxn << endl;
            for(int i = 1;i < maxn;++i){
                cout << num << " ";
                ans /= num;
            }
            cout << ans << endl;
        }
        else{//本身为质数的情况
            cout << 1 << endl;
            cout << ans << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值