hdu1019Least Common Multiple(gcd的性质)

题目大意:给定一个序列,求所有数的最小公倍数,由于gcd(a,b,c)=gcd(gcd(a,b),c)这个性质,可以推出

lcm(a,b,c)=lcm(lcm(a,b),c);我们就可以直接On扫描过去就能求出解了,但是我忘了...
然后就写了一个质因数分解然后去晒后面的数,结果还A了,大聪明说的就是我吧...
依靠性质On:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000;
const int inf = 0x3f3f3f3f;
ll gcd(int m, int n) {
    return n == 0 ? m : gcd(n, m % n);
}
int main() {
    //freopen("test.txt", "r", stdin);
    int t; scanf("%d", &t);
    while (t--) {
        int n; scanf("%d", &n);
        ll res = 1;
        for (int i = 1; i <= n; i++) {
            ll    num; scanf("%lld", &num);
            res = res / gcd(res, num) * num;
        }
        printf("%lld\n", res);
    }
    return 0;
}

质因数分解0n^2(大概):

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000;
const int inf = 0x3f3f3f3f;
ll a[maxn];
int main() {
    //freopen("test.txt", "r", stdin);
    int t; scanf("%d", &t);
    while (t--) {
        int n; scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &a[i]);
        }
        sort(a + 1, a + n + 1);//从小到大,更快(应该吧)
        ll res = 1;
        for (int i = 1; i <= n; i++) {
            ll s = a[i], k = 2;
            res *= a[i];//直接乘上现在的数
            while (s != 1) {//质因数分解,有质因数就晒掉后面的数的该因数
                if (s % k == 0) {
                    int num = 0;
                    while (s % k == 0) {//算一下有多少个
                        num++; s /= k;
                    }
                    for (int j = i + 1; j <= n; j++) {
                        int cnt = 0;
                        while (cnt < num && a[j] % k == 0) {//最多晒num个
                            a[j] /= k; cnt++;
                        }
                    }
                }
                k++;
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}

 

posted @ 2021-03-30 08:22  cono奇犽哒  阅读(58)  评论(0)    收藏  举报