【bzoj1101&&luogu3455】[POI2007]Zap

http://www.elijahqi.win/2017/07/02/bzoj1101/
题目描述
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 , and , find the number of integer pairs satisfying the following conditions:
,,, where is the greatest common divisor of and “.
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 n (1<=n<= 50000),denoting the number of queries.
The following lines contain three integers each:a ,b and d(1<=d<=a,b<=50000), separated by single spaces.
Each triplet denotes a single query.

输出格式:

Your programme should write lines to the standard output. The ‘th line should contain a single integer: theanswer to the ‘th query from the standard input.

输入输出样例
输入样例#1:
2
4 5 2
6 4 3
输出样例#1:
3
2

题解:同bzoj2301 并且还简单一些

#include<cstdio>
#include<cstring> 
#define N 55000
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int n,prime[N],not_prime[N],sum[N],mu[N];
inline int swap(int &a,int &b){
    int t=a;a=b;b=t;
}
inline int min(int a,int b){
    return a<b?a:b;
}
inline int calc(int n,int m){
    int last=0,ans=0;
    if (n>m) swap(n,m);
    for (int i=1;i<=n;i=last+1){
        last=min(n/(n/i),m/(m/i));
        ans+=(sum[last]-sum[i-1])*(n/i)*(m/i);
    }
    return ans;
}
int main(){
    freopen("3455.in","r",stdin);
    freopen("3455.out","w",stdout);
    n=read();
    int a,b,k,tot=0;
    mu[1]=1;memset(not_prime,0,sizeof(not_prime));
    for (int i=2;i<=N;++i){
        if (!not_prime[i])prime[++tot]=i,mu[i]=-1;
        for (int j=1;prime[j]*i<=N;++j){
            not_prime[prime[j]*i]=1;
            if (i%prime[j]==0){
                mu[prime[j]*i]=0;break;//保证每个合数只被筛一次 
            }
            mu[prime[j]*i]=-mu[i];
        }
    }
    for (int i=1;i<=N;++i) sum[i]=sum[i-1]+mu[i];
    while (n--){
        a=read();b=read();k=read();
        a/=k;b/=k;
        printf("%d\n",calc(a,b));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值