题解 P2429 【制杖题】

· · 题解

这不就是线筛吗(map版)

#include <bits/stdc++.h>
using namespace std;
int n,m,s;
long long sum=0;
map<int,bool>a;//注意,第二个变量要用bool(因为只有0和1),不然会MLE(我也不知道会不会,但能省就省)
int main()
{
    scanf("%d%d",&n,&m);
    while(n--)
    {scanf("%d",&s);
    for(int j=s;j<=m;j+=s)//把它的倍数标记
    if(!a[j])
    {sum+=j;//累计和
    a[j]=1;}}
    printf("%lld",sum%376544743);//取模很重要
}