Codeforces Round #560 (Div. 3)D题Almost All Divisors

这题觉得挺有意思的,就写篇博客记录一下,主要还是学到了一些东西吧

结论:
1. 如果一个序列 a a a中的数是 一个数 x i x_i xi的除1和本身的所有因子,那么这个 x i x_i xi的值将为 m i n ( a ) ∗ m i n ( b ) min(a)*min(b) min(a)min(b)
2. 如何得到一个数的所有因子(2 <= i < x)

	for(int i = 2; i < x; ++i){
		if(!(x%i)){
			++tot;
			if(i*i!=x) ++tot;
		}
	}

本题思路: 首先把数读进来,然后排序,算出 x = m i n ( a ) ∗ m i n ( b ) x = min(a)*min(b) x=min(a)min(b)
然后判断是不是序列a中的每个数能被x整除,如果不能就出-1
然后再判断x的因子个数(除去n和1)是不是等于n

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MAX = 3e4+5;
ll arr[MAX];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T;  cin >> T;
    while(T--){
        int n;  cin >> n;
        for(int i = 0; i < n; ++i){
            cin >>  arr[i];
        }
        sort(arr,arr+n);
        ll ans = arr[0] * arr[n-1];
        bool istrue = true;
        for(int i = 0; i < n; ++i){
            if(ans%arr[i]){
                istrue = false;
                break;
            }
        }
        if(!istrue){
            cout << "-1" << endl;
            continue;
        }
        int tot = 0;
        for(ll i = 2; i*i <=ans; ++i){
            if(!(ans%i)){
                ++tot;
                if(i*i!=ans) ++tot;
            }
        }
        if(tot!=n){
            cout << -1 << endl;
        }
        else cout << ans <<endl;
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值