bzoj3309 DZY Loves Math

题目描述:

bz

题解:

线性筛……

瞎jb反演得到$ans=\sum\limits _{T=1}^{a} \lfloor \frac{a}{T} \rfloor \lfloor \frac{b}{T} \rfloor \sum\limits _{d|T} f(d) \mu(\frac{T}{d})$。

后面那个要求$O(n)$筛出来。

剩下的我讲不明白,直接挂PoPoQQQ题解了。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 10000050;
template<typename T>
inline void read(T&x)
{
    T f = 1,c = 0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    x = f*c;
}
bool vis[N];
int pri[N/10],pnt,f[N],fk[N];
ll s[N];
inline void init()
{
    for(register int i=2;i<=10000000;++i)
    {
        if(!vis[i])
        {
            pri[++pnt]=i;
            s[i] = 1;
            f[i] = 1,fk[i] = i;
        }
        for(register int j=1;i*pri[j]<=10000000;++j)
        {
            int now = i*pri[j];
            vis[now]=1;
            if(i%pri[j])
            {
                f[now] = 1,fk[now] = pri[j];
                s[now] = (f[i]==1)?-s[i]:0;
            }else
            {
                f[now] = f[i]+1,fk[now] = fk[i]*pri[j];
                int las = i/fk[i];
                if(las==1)s[now]=1;
                else s[now]=(f[las]==f[now])?-s[las]:0;
                break;
            }
        }
    }
    for(register int i=1;i<=10000000;++i)s[i]+=s[i-1];
}
int T,n,m;
inline void work()
{
    read(n),read(m);
    ll ans = 0;int mn = min(n,m);
    for(register int i=1,j;i<=mn;i=j+1)
    {
        j = min(n/(n/i),m/(m/i));
        ans+=1ll*(n/i)*(m/i)*(s[j]-s[i-1]);
    }
    printf("%lld\n",ans);
}
int main()
{
//    freopen("tt.in","r",stdin);
    read(T);init();
    while(T--)work();
    return 0;
}
View Code

 

posted @ 2019-07-08 10:40  LiGuanlin  阅读(...)  评论(...编辑  收藏