Problem I. Count

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 443    Accepted Submission(s): 232

Problem Description
Multiple query, for each n, you need to get
n i-1
∑ ∑ [gcd(i + j, i - j) = 1]
i=1 j=1
 
Input
On the first line, there is a positive integer T, which describe the number of queries. Next there are T lines, each line give a positive integer n, as mentioned above.
T<=1e5, n<=2e7
 
Output
Your output should include T lines, for each line, output the answer for the corre- sponding n.
 
Sample Input
4
978
438
233
666
 
Sample Output
194041
38951
11065
89963
 
Source
    ∑i=1ni-1j=1 [gcd(i + j, i − j) = 1]
  =  [gcd(2i-a,a)=1]    (1<=i<=n,1<=a<i)
  =  [gcd(2i,a)=1]    (1<=i<=n,1<=a<i)
即对于每个 i, 求有多少个小于它的 a 满足 gcd(i, a) = 1 且a是奇数。
当i是偶数的时候贡献就是  phi(i) ,因为偶数与偶数一定不互质。
当i是奇数的时候贡献是phi(i)/2 ,因为i是奇数,假如x与i互质那么 i-x与i也互质,且x与i-x一个是奇数一个是偶数,所以数量是均等出现的。
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define LL long long
  4. #define mp make_pair
  5. #define pb push_back
  6. #define inf 0x7fffffffff
  7. #define pii pair<int,int>
  8. const int maxn=;
  9. vector<int>prime;
  10. bool is[maxn];
  11. LL f[maxn];
  12. void init(){
  13. f[]=;
  14. is[]=is[]=;
  15. for(int i=;i<=maxn;++i){
  16. if(!is[i]) prime.push_back(i),f[i]=i-;
  17. for(int j=;j<prime.size()&&1LL*i*prime[j]<=maxn;++j){
  18. is[i*prime[j]]=;
  19. if(i%prime[j]==){
  20. f[i*prime[j]]=f[i]*prime[j];
  21. break;
  22. }
  23. else{
  24. f[i*prime[j]]=f[i]*(prime[j]-);
  25. }
  26. }
  27. }
  28. for(int i=;i<maxn;++i){
  29. if(i&)f[i]/=;
  30. f[i]+=f[i-];
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. init();
  37. int t,n;
  38. scanf("%d",&t);
  39. while(t--){
  40. scanf("%d",&n);
  41. printf("%lld\n",f[n]);
  42. }
  43. return ;
  44. }

hdu-6434-欧拉函数的更多相关文章

  1. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  2. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 2824(欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. hdu 1395(欧拉函数)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. hdu 3307(欧拉函数+好题)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  6. 找新朋友 HDU - 1286 欧拉函数模板题

    题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...

  7. hdu 2824 欧拉函数 O(nlogn) 和O(n)

    裸题 O(nlogn): #include <cstdio> #include <iostream> #include <algorithm> using name ...

  8. hdu 4983 欧拉函数

    http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...

  9. hdu 4002 欧拉函数 2011大连赛区网络赛B

    题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是 ...

  10. hdu 1787(欧拉函数)

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. Jenkins-Publish HTML reports

    创建job:testreport 在job中添加: 在Jenkins服务器上: 创建目录: .jenkins/jobs/{job名称}/workspace/htmlreports    注:此处job ...

  2. Latex: "Missing $ inserted" 解决方法

    参考: Latex报"Missing $ inserted"的解决方法 Latex: "Missing $ inserted" 解决方法 原因一:在文中出现&q ...

  3. DPDK 网卡绑定和解绑

    参考: DPDK网卡绑定和解绑 DPDK的安装与绑定网卡 DPDK 网卡绑定和解绑 注意: 建议不要使用本文的eth0网卡绑定dpdk驱动. 1.进入DPDK目录: $ cd dpdk/tools/ ...

  4. Java 静态方法不能重写但可以被子类静态方法覆盖

    强调 静态方法是属于类的,只存在一份,会被该类的所有对象共享.不可以被重写. 静态方法可以被子类继承,但是不可以被子类重写 class door{ } class wood_Door extends ...

  5. 1、My Scripts

    1.写一个包含命令.变量和流程控制的语句来清除/var/log的messages日志文件的shell脚本.(P26)(11-21) 2.利用$0和(dirname.basename)取出当前路径的目录 ...

  6. 关于PS抠图的各种方法 有这个就可以去面试了!!!加油!!!

    今天和大家说说关于PS抠图的方法 高手也就如此  你值得拥有!!好了 废话不多说 下面进入正题 首先:我们得分析所给的图 然后运用不同的方法,当然也可以相互灵活运用 1:不抠图 2:万能抠图方法:快速 ...

  7. log4net配置使用

    1.配置文件 app.config <?xml version="1.0" encoding="utf-8" ?> <configuratio ...

  8. Python 模块(module)

    模块(module)也是为了同样的目的.在Python中,一个.py文件就构成一个模块.通过模块,你可以调用其它文件中的程序. first.py def laugh(): print "Ha ...

  9. C/C++.全文件名全路径名分割拆分分解

    1._splitpath ZC:windows api的话 可以使用 PathFindFileNameA.PathFindExtensionA.PathFileExistsA等一系列函数 2.测试代码 ...

  10. VC_窗口exe_printf信息

    1. #include <io.h> #include <fcntl.h> #include <stdio.h> 2. void InitConsoleWindow ...