《洛谷P3455 [POI2007]ZAP-Queries》

不是很难,这里筛法写错了,一度以为自己推错了。。

题意:求解$ans = \sum_{x = 1}^{a}\sum_{y = 1}^{b}|gcd(x,y) = d|$

Solution:

$ans = = \sum_{x = 1}^{[\frac{a}{d}]}\sum_{y = 1}^{[\frac{b}{d}]}|gcd(x,y) = 1| = \sum_{t = 1}^{min(\frac{a}{d},\frac{b}{d})} \mu (t) * [\frac{a}{dt}] * [\frac{b}{dt}]$

 其实化到了这里已经可以了,但我一开始继续化了,令T = dt.

那么$ans = \sum_{T = 1}^{min(a,b)}\mu (\frac{T}{d}) * [\frac{a}{T}] * [\frac{b}{T}]$

然后就可以整除分块,然后处理一个莫比乌斯的前缀和,注意这里的莫比乌斯函数下标是T/d,分块时注意位置即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<double,int> pii;
const int N = 5e4+5;
const int M = 1e6+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline int read()
    {
        int x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}
        return x * f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;

int mu[N],prime[N],tot = 0;
LL sum[N];
bool vis[N];
void init()
{
    mu[1] = 1;
    for(rg int i = 2;i < N;++i)
    {
        if(!vis[i])
        {
            prime[++tot] = i;
            mu[i] = -1;
        }
        for(rg int j = 1;j <= tot && prime[j] * i < N;++j)
        {
            vis[prime[j] * i] = 1;
            if(i % prime[j] == 0) break;
            else mu[prime[j] * i] = -mu[i];
        }
    }
    for(rg int i = 1;i < N;++i) sum[i] = sum[i - 1] + mu[i];
}
int main()
{
    init();
    int ca;ca = read();
    while(ca--)
    {
        int a,b,d;a = read(),b = read(),d = read();
        LL ans = 0;
        for(int L = 1,r = 0;L <= min(a,b);L = r + 1)
        {
            r = min(a / (a / L),b / (b / L));
            ans += (sum[r / d] - sum[(L - 1) / d]) * (a / L) * (b / L);
        }
        printf("%lld\n",ans);
    }
    //system("pause");
    return 0;
}
View Code

现在我们直接看第一种,其实只要把后面的看成$\frac{\frac{a}{d}}{t} $和$\frac{\frac{b}{d}}{t} $来分块即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<double,int> pii;
const int N = 5e4+5;
const int M = 1e6+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline int read()
    {
        int x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}
        return x * f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;

int mu[N],prime[N],tot = 0;
LL sum[N];
bool vis[N];
void init()
{
    mu[1] = 1;
    for(rg int i = 2;i < N;++i)
    {
        if(!vis[i])
        {
            prime[++tot] = i;
            mu[i] = -1;
        }
        for(rg int j = 1;j <= tot && prime[j] * i < N;++j)
        {
            vis[prime[j] * i] = 1;
            if(i % prime[j] == 0) break;
            else mu[prime[j] * i] = -mu[i];
        }
    }
    for(rg int i = 1;i < N;++i) sum[i] = sum[i - 1] + mu[i];
}
int main()
{
    init();
    int ca;ca = read();
    while(ca--)
    {
        int a,b,d;a = read(),b = read(),d = read();
        LL ans = 0;
        a = a / d,b = b / d;
        for(int L = 1,r = 0;L <= min(a,b);L = r + 1)
        {
            r = min(a / (a / L),b / (b / L));
            ans += (sum[r] - sum[L - 1]) * (a / L) * (b / L);
        }
        printf("%lld\n",ans);
    }
    //system("pause");
    return 0;
}
View Code

 

posted @ 2020-10-17 08:36  levill  阅读(126)  评论(0编辑  收藏  举报