黑妹的游戏三

PS:枚举质因数很容易想到,比赛的时候想到枚举1e8范围内,果断写不出来。其实只需要枚举10000内的质因数就行了,因为对于a来说大于10000的质因数最多一个。枚举质因数后怎么确定能消去多少个呢?最容易想到的方法就是模拟了(我是这样YY的,最坏的情况:10000个 134217728。需要计算 (10000 * 1000 + 27 * 5000)次,10000内有1000多个素数),挺考验实现能力的。

 

#include<bits/stdc++.h>
#include<algorithm>
#define ll long long
#define P pair<int, int>
#define PP pair<int,pair<int, int>>
#define pb push_back
#define pp pop_back
#define lson root << 1
#define INF (int)2e9 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

const int maxn = 20000;
const int mod = 1000000007;

int T, n;
int a[maxn], prime[maxn];

vector<int> p;
priority_queue<int> q;

void Inite() {
    for(int i = 2; i <= 10000; i++) {
        if(prime[i]) continue;
        p.pb(i);
        for(int j = 2 * i; j <= 10000; j += i) prime[j] = 1;
    }
}

int main()
{
    Inite();
    cin >> T;
    while(T--) {
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i];
        ll ans = 1;
        for(auto i : p) {
            for(int j = 1; j <= n; j++) {
                int t = 0;
                while(a[j] % i == 0) {
                    a[j] /= i;
                    t++;
                }
                if(t) q.push(t);
            }
            while(q.size() > 1) {
                int x = q.top(); q.pop();
                int y = q.top(); q.pop();
                --x; --y;
                if(x > 0) q.push(x);
                if(y > 0) q.push(y);
            }
            if(q.empty()) continue;
            int x = q.top(); q.pop();
            for(int j = 0; j < x; j++) ans = (ans * i) % mod;
        }
        sort(a + 1, a + n + 1);
        for(int i = 1; i < n; i++) if(a[i] == a[i + 1]) a[i] = a[i + 1] = 1;
        for(int i = 1; i <= n; i++) ans = (ans * a[i]) % mod;
        cout << ans << endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zgglj-com/p/9245791.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值