zoukankan      html  css  js  c++  java
  • 【刷题】洛谷 P3455 [POI2007]ZAP-Queries

    题目描述

    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 aa , bb and dd , find the number of integer pairs ((x,y)) satisfying the following conditions:

    $1le xle a $,(1le yle b) ,(gcd(x,y)=d), where (gcd(x,y)) is the greatest common divisor of xx and yy ".

    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) ((1le nle 50 000) ),denoting the number of queries.

    The following (n) lines contain three integers each: (a) , (b) and (d) ((1le dle a,ble 50 000) ), separated by single spaces.

    Each triplet denotes a single query.

    输出格式:

    Your programme should write nn lines to the standard output. The (i) 'th line should contain a single integer: theanswer to the (i) 'th query from the standard input.

    输入输出样例

    输入样例#1:

    2

    4 5 2

    6 4 3

    输出样例#1:

    3

    2

    题解

    这题其实还是上一篇的弱化版,f(x)和F(x)的定义一样,最后的式子是:

    [ans=f(d)=sum_{T=1}^{min(a,b)}mu(frac{T}{n})lfloor frac{a}{T} floor lfloor frac{b}{T} floor ]

    一样的整除分块,一样的前缀和

    #include<bits/stdc++.h>
    #define ll long long
    const int MAXN=50000+10;
    ll T,mu[MAXN],s[MAXN],prime[MAXN],cnt;
    bool vis[MAXN];
    template<typename T> inline void read(T &x)
    {
    	T data=0,w=1;
    	char ch=0;
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-')w=-1,ch=getchar();
    	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    	x=data*w;
    }
    template<typename T> inline void write(T x,char c='')
    {
    	if(x<0)putchar('-'),x=-x;
    	if(x>9)write(x/10);
    	putchar(x%10+'0');
    	if(c!='')putchar(c);
    }
    template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
    template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
    template<typename T> inline T min(T x,T y){return x<y?x:y;}
    template<typename T> inline T max(T x,T y){return x>y?x:y;}
    inline void init()
    {
    	memset(vis,1,sizeof(vis));
    	vis[0]=vis[1]=0;
    	mu[1]=1;
    	for(register int i=2;i<MAXN;++i)
    	{
    		if(vis[i])
    		{
    			prime[++cnt]=i;
    			mu[i]=-1;
    		}
    		for(register int j=1;j<=cnt&&i*prime[j]<MAXN;++j)
    		{
    			vis[i*prime[j]]=0;
    			if(i%prime[j])mu[i*prime[j]]=-mu[i];
    			else break;
    		}
    	}
    	for(register int i=1;i<MAXN;++i)s[i]=s[i-1]+mu[i];
    }
    inline ll solve(ll a,ll b,ll d)
    {
    	ll res=0;
    	for(register ll i=1;;)
    	{
    		if(i>min(a,b))break;
    		ll j=min(a/(a/i),b/(b/i));
    		res+=(a/i)*(b/i)*(s[j/d]-s[(i-1)/d]);
    		i=j+1;
    	}
    	return res;
    }
    int main()
    {
    	init();
    	read(T);
    	while(T--)
    	{
    		ll a,b,d;
    		read(a);read(b);read(d);
    		write(solve(a,b,d),'
    ');
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    Spring初次整体了解及一篇SpringBoot的稿子。。。
    JAVA日志框架log4j和slf4j
    初学SpringBoot之三
    初学SpringBoot之二
    初学SpringBoot之一
    (转)SWITCH_ROOT
    (转)initrd和initramfs的区别
    (转)三星HDMI驱动框架解析
    (转)linux内存分析
    (转)linux中挂载IMG文件
  • 原文地址:https://www.cnblogs.com/hongyj/p/8535261.html
Copyright © 2011-2022 走看看