欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 1695 GCD

程序员文章站 2022-06-07 21:46:50
...

Here

题意

给你5个数a,b,c,d,ka,b,c,d,k,让你求符合axb,cyd,(x,y)=ka\leq x \leq b,c\leq y \leq d,(x,y)=kx,yx,y对数,这里(1,2)(1,2)(2,1)(2,1)算一种。
你可以认为所有testcasetest casea,c=1a,c=1

又是板子题啦,开始推式子
*除法默认向下取整
假设bdb\leq d

x=1by=1d[gcd(x,y)==k]\sum\limits_{x=1}^{b}\sum\limits_{y=1}^{d}[gcd(x,y)==k]

x=1bky=1dk[gcd(x,y)==1]\sum\limits_{x=1}^{\frac{b}{k}}\sum\limits_{y=1}^{\frac{d}{k}}[gcd(x,y)==1]

x=1bky=1dkggcd(x,y)μ(g)\sum\limits_{x=1}^{\frac{b}{k}}\sum\limits_{y=1}^{\frac{d}{k}}\sum\limits_{g|gcd(x,y)}μ(g)

g=1bkμ(g)bkgdkg\sum\limits_{g=1}^{\frac{b}{k}}μ(g)\lfloor\frac{b}{kg}\rfloor\lfloor\frac{d}{kg}\rfloor

这儿算出来的答案会有重复的,由于我们假设bdb\leq d,所以只要减去g=1bkμ(g)bkgbkg2\frac{\sum\limits_{g=1}^{\frac{b}{k}}μ(g)\lfloor\frac{b}{kg}\rfloor\lfloor\frac{b}{kg}\rfloor}{2}就是答案了

Code 62MS

/****************************
* Author : 水娃             *
* Date : 2020-08-19-14.54.19*
****************************/
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define ll long long
#define debug(a) cout<<#a<<" is "<<a<<"\n"
#define ull unsigned long long
#define fi first
#define se second
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<int,ll>pil;
typedef pair<ll,ll>pll;
const ll mo=(ll)1e9+7;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const int MAXN=2e5+100;
int now;
int mu[MAXN];
int pri[MAXN>>1];
bool vis[MAXN];
void pre()
{
    now=0;
    mu[1]=1;
    for(int i=2;i<MAXN;i++)
    {
        if(!vis[i])
        {
            pri[++now]=i;
            mu[i]=-1;
        }
        for(int j=1;j<=now&&i*pri[j]<MAXN;j++)
        {
            vis[i*pri[j]]=1;
            if(i%pri[j])
            {
                mu[i*pri[j]]=-mu[i];
            }
            else
            {
                mu[i*pri[j]]=0;
                break;
            }
        }
    }
}
int cas;
void work()
{
    ll a,b,c,d,k;
    cin>>a>>b>>c>>d>>k;
    if(k==0)
    {
        cout<<"Case "<<++cas<<": "<<0<<"\n";
        return ;
    }
    if(b>d)swap(b,d);
    ll ans=0,top=b/k;
    for(int g=1;g<=top;g++)
    {
        ans+=mu[g]*(b/(k*g))*(d/(k*g));
    }
    ll ans1=0;
    for(int g=1;g<=top;g++)
    {
        ans1+=mu[g]*(b/(k*g))*(b/(k*g));
    }
    cout<<"Case "<<++cas<<": "<<ans-ans1/2<<"\n";
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    pre();
    int T;cin>>T;while(T--)
        work();
    return 0;
}

HDU 1695 GCD

相关标签: 数学