zoukankan      html  css  js  c++  java
  • [hdu6434]Problem I. Count

    题目大意:$T(Tleqslant 10^5)$组数据,每组数据给你$n(nleqslant 2 imes 10^7)$,求$sumlimits_{i=1}^nsumlimits_{j=1}^{i-1}[(i+j,i-j)==1]$

    题解:
    $$
    defdsum{displaystylesumlimits}
    egin{align*}
    &dsum_{i=1}^ndsum_{j=1}^{i-1}[(i+j,i-j)=1]\
    &令k=i-j\
    =&dsum_{i=1}^ndsum_{k=1}^{i-1}[(2i-k,k)=1]\
    =&dsum_{i=1}^ndsum_{k=1}^{i-1}[(2i,k)=1]\
    end{align*}
    $$

    $$
    herefore
    (2i,k)=1Rightarrow
    egin{cases}
    (i,k)=1\
    (2,k)=1\
    end{cases}\
    当i为偶数时:\
    (i,k)=1\
    Rightarrow (2,k)=1\
    herefore ans=varphi(i)\
    当i为奇数时:\
    (2,k)=1\
    Rightarrow k为奇数\
    herefore k必为与i互质的数的奇数\
    ecause (i,k)=1Rightarrow(i,i-k)=1\
    herefore 当i为奇数的时候,k奇偶各半\
    herefore ans=dfrac{varphi(i)}2
    $$

    卡点:

    C++ Code:

    #include <cstdio>
    #define maxn 20000010
    int Tim, n;
    long long pre[maxn];
    int plist[maxn << 3], pt, phi[maxn];
    bool isp[maxn];
    void sieve(int n) {
    	phi[1] = 1;
    	pre[1] = 0;
    	isp[1] = true;
    	for (int i = 2; i < n; i++) {
    		if (!isp[i]) {
    			plist[pt++] = i;
    			phi[i] = i - 1;
    		}
    		for (int j = 0; j < pt && i * plist[j] < n; j++) {
    			int tmp = i * plist[j];
    			isp[tmp] = true;
    			if (i % plist[j] == 0) {
    				phi[tmp] = phi[i] * plist[j];
    				break;
    			}
    			phi[tmp] = phi[i] * phi[plist[j]];
    		}
    		pre[i] = pre[i - 1] + ((i & 1) ? phi[i] / 1 : phi[i]);
    	}
    }
    int main() {
    	sieve(maxn);
    	scanf("%d", &Tim);
    	while (Tim --> 0) {
    		scanf("%d", &n);
    		printf("%lld
    ", pre[n]);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Lucene 全文检索
    Redis 集群
    Redis 初步接触
    Mybatis
    FastJson 介绍
    JAVA微信企业付款到零钱(十分钟搞定),附完整DEMO下载
    持续集成与Devops关系
    GIT命令行统计代码提交行数
    一种简单的REST API接口加密实现,只允许自己的产品调用后台,防止接口被刷
    Beyond Compare 4.X 破解方法(亲测有效)
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9671153.html
Copyright © 2011-2022 走看看