码迷,mamicode.com
首页 > 其他好文 > 详细

[Luogu3455][POI2007]ZAP-Queries

时间:2018-01-04 11:00:55      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:contains   cpp   正整数   greatest   html   orm   single   ace   gen   

BZOJ(权限题)
Luogu
题目描述
Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He has alreadyfound out that whilst deciphering a message he will have to answer multiple queries of the form"for givenintegers aaa, bbb and ddd, find the number of integer pairs (x,y) satisfying the following conditions:
1≤x≤a,1≤y≤b,gcd(x,y)=d, where gcd(x,y)is the greatest common divisor of x and y".
Byteasar would like to automate his work, so he has asked for your help.
TaskWrite a programme which:
reads from the standard input a list of queries, which the Byteasar has to give answer to, calculates answers to the queries, writes the outcome to the standard output.
FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。
输入输出格式
输入格式:
The first line of the standard input contains one integer nnn (1≤n≤50 000),denoting the number of queries.
The following nnn lines contain three integers each: aaa, bbb and ddd(1≤d≤a,b≤50 000), separated by single spaces.
Each triplet denotes a single query.
输出格式:
Your programme should write nnn lines to the standard output. The iii‘th line should contain a single integer: theanswer to the iii‘th query from the standard input.
输入输出样例
输入样例#1:
2
4 5 2
6 4 3
输出样例#1:
3
2

sol

参见莫比乌斯反演总结中的“举个栗子”、“骚操作”以及“奇技淫巧”部分
这道题就做完了。

code

#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 50005;
const int n = 50000;
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
int mu[N],pri[N],tot,s[N];
bool zhi[N];
void Mobius()
{
    zhi[1]=true;mu[1]=1;
    for (int i=2;i<=n;i++)
    {
        if (!zhi[i]) pri[++tot]=i,mu[i]=-1;
        for (int j=1;j<=tot&&i*pri[j]<=n;j++)
        {
            zhi[i*pri[j]]=true;
            if (i%pri[j]) mu[i*pri[j]]=-mu[i];
            else {mu[i*pri[j]]=0;break;}
        }
    }
    for (int i=1;i<=n;i++) s[i]=s[i-1]+mu[i];
}
ll calc(int a,int b,int k)
{
    a/=k;b/=k;
    if (a>b) swap(a,b);
    int i=1,j;ll ans=0;
    while (i<=a)
    {
        j=min(a/(a/i),b/(b/i));
        ans+=1ll*(s[j]-s[i-1])*(a/i)*(b/i);
        i=j+1;
    }
    return ans;
}
int main()
{
    int T=gi();
    Mobius();
    while (T--)
    {
        int a=gi(),b=gi(),k=gi();
        printf("%lld\n",calc(a,b,k));
    }
    return 0;
}

[Luogu3455][POI2007]ZAP-Queries

标签:contains   cpp   正整数   greatest   html   orm   single   ace   gen   

原文地址:https://www.cnblogs.com/zhoushuyu/p/8191339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!