• CF1025B Weakened Common Divisor


    思路:

    首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件。

    时间复杂度:O(sqrt(amax) + n * log(amax))。

    实现:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAXN = 150005;
     4 int a[MAXN], b[MAXN];
     5 set<int> factor(int x)
     6 {
     7     set<int> res;
     8     for (int i = 2; i * i <= x; i++)
     9     {
    10         if (x % i == 0) 
    11         {
    12             res.insert(i);
    13             while (x % i == 0) x /= i;
    14         }
    15     }
    16     if (x != 1) res.insert(x);
    17     return res;
    18 }
    19 int main()
    20 {
    21     int n;
    22     while (scanf("%d", &n) != EOF)
    23     {
    24         for (int i = 0; i < n; i++) scanf("%d %d", &a[i], &b[i]);
    25         set<int>s1 = factor(a[0]);
    26         set<int>s2 = factor(b[0]);
    27         set<int> st;
    28         for (auto it: s1) st.insert(it);
    29         for (auto it: s2) st.insert(it);
    30         for (int i = 1; i < n; i++)
    31         {
    32             set<int> tmp;
    33             for (auto it: st)
    34             {
    35                 if (it > a[i] && it > b[i]) continue;
    36                 else if (a[i] % it && b[i] % it) continue;
    37                 tmp.insert(it);
    38             }
    39             st = tmp;
    40         }
    41         if (st.empty()) puts("-1");
    42         else printf("%d
    ", *st.begin());
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    使用HttpURLConnection
    使用HttpURLConnection
    HTTP基本工作原理
    HTTP基本工作原理
    【杭电】[2022]海选女主角
    【杭电】[2022]海选女主角
    【吐槽】这是第200篇博文
    【吐槽】这是第200篇博文
    【NYOJ】[8]一种排序
    【NYOJ】[8]一种排序
  • 原文地址:https://www.cnblogs.com/wangyiming/p/9512712.html
Copyright © 2020-2023  润新知