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

题目链接:https://codeforc.es/contest/1165/problem/D

题意: 给一段序列 问有没有一个数 使得这个序列中的数都是x的因子 (除了1和x本身)

输出这个最小的数 如果不存在就输出-1

思路:写的时候想了lcm做 发现不可行,i的时候没开ll导致又TLE

正解应该是 对该序列排序 假设这个x存在的话那么只可能是第一个和最后一个的乘积为x

然后去o(sqrt(n))找出这个x的因子 比较和原数组是否相等

不相同就说明假设错误 也找不到其他的数了

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =2e5+10;
 6 
 7 int main()
 8 {
 9     ios::sync_with_stdio(false);
10     cin.tie(0);
11     int t;
12     cin>>t;
13     while(t--)
14     {
15         int n;
16         vector<int>a;
17         vector<int>b;
18         cin>>n;
19         for(int i=1;i<=n;i++)
20         {
21             int x;
22             cin>>x;
23             a.pb(x);
24         }
25         sort(a.begin(),a.end());
26         ll x=1ll*a[0]*a[n-1];
27         for(ll i=2;i*i<=x;i++) // 不加ll tle
28         {
29             if(x%i==0)
30             {
31                 b.pb(i);
32                 if(i!=x/i)
33                     b.pb(x/i);
34             }
35         }
36         sort(b.begin(),b.end());
37         if(a==b) // vector 直接比较
38             cout<<x<<'\n';
39         else
40             cout<<-1<<'\n';
41 
42 
43     }
44 
45 
46 }
View Code

 

posted @ 2020-05-24 15:08  canwinfor  阅读(204)  评论(0)    收藏  举报