【数论】洛谷_2429 制杖题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SSL_hzb/article/details/82502164

题目

求不大于 M 的、质因数集与给定质数集有交集的自然数之和。

思路

翻译一下题目意思:就是求出不超过 M 的数中,质因子含有这些质数的数的总和(不重复)。
那么这些数就是这些质数的倍数,我们用 s e t 装一装我们求出的数,然后累加就好了。

代码

#include<set>
#include<cstdio>
using namespace std;

int N, M, A;
set<int> S; 

void count(int x) {
    for (int i = x; i <= M; i += x) S.insert(i);
}

int main() {
    scanf("%d %d", &N, &M);
    for (int i = 1; i <= N; i++) {
        scanf("%d", &A);
        count(A);
    }
    long long ans = 0;
    for (set<int>::iterator i = S.begin(); i != S.end(); i++)
        ans += *i;
    printf("%lld", ans % 376544743);
}

猜你喜欢

转载自blog.csdn.net/SSL_hzb/article/details/82502164