zoukankan      html  css  js  c++  java
  • 【Luogu P3455】 [POI2007]ZAP-Queries

    题目链接:

    题目

    博客园

    题目大意:

    快速求:

    [sum_{i=1}^{n}sum_{j=1}^{m}left[operatorname{gcd}(i,j)==d ight] ]

    正文:

    将式子化简:

    [egin{aligned}sum_{i=1}^{n}sum_{j=1}^{m}left[operatorname{gcd}(i,j)==d ight] &= sum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{m}{d} floor}left[operatorname{gcd}(i,j)==1 ight]\ &=sum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{m}{d} floor}sum_{k|operatorname{gcd}(i,j)}mu(k)end{aligned} ]

    (mu) 那儿移到前面:

    [egin{aligned}sum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{m}{d} floor}sum_{k|operatorname{gcd}(i,j)}mu(k) &= sum_{k=1}mu(k)sum_{k|i}^{lfloorfrac{n}{d} floor}sum_{k|j}^{lfloorfrac{m}{d} floor}1\ &=sum_{k=1}mu(k)leftlfloorfrac{n}{kd} ight floorleftlfloorfrac{m}{kd} ight floorend{aligned} ]

    接着预处理一下 (mu) 函数再求个关于 (leftlfloorfrac{n}{kd} ight floorleftlfloorfrac{m}{kd} ight floor)整除分块 就行了。

    代码:

    ll n, m, d, t;
    ll ans;
    ll pri[N], miu[N], cnt, sum[N];
    bool vis[N];
    
    void prework()
    {
    	miu[1] = 1;
    	for (int i = 2; i <= N - 10; i++)
    	{
    		if(!vis[i]) {pri[++cnt] = i, miu[i] = -1;}
    		for (int j = 1; j <= cnt && pri[j] * i <= N - 10; j++)
    		{
    			vis[pri[j] * i] = 1;
    			if (i % pri[j] == 0)
    			{
    				miu[i * pri[j]] = 0;
    				break;
    			}
    			else
    				miu[i * pri[j]] = -miu[i];
    		}
    	}
    	for (int i = 1; i <= N - 10; i++)
    		sum[i] = sum[i - 1] + miu[i];
    }
    
    int main()
    {
    	prework();
    	for (scanf ("%lld", &t); t--; )
    	{
    		ans = 0LL;
    		scanf("%lld%lld%lld", &n, &m, &d);
    		if(n > m)
    		{
    			int c = n; n = m; m = c;
    		}
    		n /= d, m /= d;
    		for (int l = 1, r; l <= n; l = r + 1)
    		{
    			r = min (n / (n / l), m / (m / l));
    			ans += (sum[r] - sum[l - 1]) * (n / l) * (m / l);
    		}
    		printf ("%lld
    ", ans);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    iptables的state模块的4种封包链接状态
    添加网络设备eth0
    DOM对象与jquery对象有什么不同
    A Death in the Family May Cause Real Heart Break
    A Soft Body, but the Mind of a Robot
    Oeasy系列教程
    浏览器中的javascript
    递归算法详细分析
    Axure 运行开发工具
    Hello Mono 转载的Mono Project Tutorial
  • 原文地址:https://www.cnblogs.com/GJY-JURUO/p/13598220.html
Copyright © 2011-2022 走看看