HYSBZ - 2301 莫比乌斯反演

链接

题解:直接用公式算,用容斥来减掉重复计算的部分

但是我犯了一个非常sb的错误,直接把abcd除k了,这样算a-1的时候就错了,然后举的例子刚好还没问题= = ,结果wa了好几发

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=50000+10,maxn=400000+10,inf=0x3f3f3f3f;

int mu[N],prime[N],sum[N];
bool mark[N];
int cnt;
void init()
{
    mu[1]=1;
    cnt=0;
    for(int i=2;i<N;i++)
    {
        if(!mark[i])prime[++cnt]=i,mu[i]=-1;
        for(int j=1;j<=cnt;j++)
        {
            int t=i*prime[j];
            if(t>N)break;
            mark[t]=1;
            if(i%prime[j]==0){mu[t]=0;break;}
            else mu[t]=-mu[i];
        }
    }
    for(int i=1;i<N;i++)sum[i]=sum[i-1]+mu[i];
}
ll cal(int n,int m)
{
    ll ans=0;
    for(int i=1,last;i<=min(n,m);i=last+1)
    {
        last=min(n/(n/i),m/(m/i));
        ans+=(ll)(sum[last]-sum[i-1])*(n/i)*(m/i);
    }
    return ans;
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll a,b,c,d,k;
        scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
        printf("%lld\n",cal(b/k,d/k)-cal((a-1)/k,d/k)-cal((c-1)/k,b/k)+cal((a-1)/k,(c-1)/k));
    }
    return 0;
}
/********************

********************/
View Code

 

posted @ 2018-02-10 21:47  walfy  阅读(125)  评论(0编辑  收藏  举报