题意

给一个\(n\),计算

\[\sum_{i=1}^{n}\sum_{j=1}^{i-1}[gcd(i + j, i - j) = 1]
\]

题解

令\(a = i - j\)

要求

\[\sum_{i=1}^{n}\sum_{j=1}^{i-1}[gcd(i + j, i - j) = 1]
\]

即求

\[\sum_{i=1}^{n}\sum_{a=1}^{i-1}[gcd(2*i - a, a) = 1]
\]

根据\(gcd\)的性质,即

\[\sum_{i=1}^{n}\sum_{a=1}^{i-1}[gcd(2*i, a) = 1]
\]

所以要求的就是\(1\)到\(i-1\)中,与\(2*i\)互质的数的个数。

令\(sum[i]\)为\(i\)的欧拉函数\(\phi\)的前缀和。结论是,对于奇数,答案就是\(sum[i]/2\),对于偶数,答案是\(sum[i]\)。

与\(2*i\)互质的数的个数,和\(\phi(i)\)(与\(i\)互质的数的个数)有什么关系呢?

如果\(i\)是奇数,那么\(1\)到\(i-1\)中与\(i\)互质的所有数中的奇数,都与\(2*i\)互质。而且这些数中,奇数占一半(为什么?因为对于任何一个奇数,小于它的和它互质的数,是以\(k\)和\(n-k\)的形式成对出现的。这两个数必然一奇一偶)。

如果\(i\)是偶数,那么\(1\)到\(i-1\)中与\(i\)互质的所有数,都与\(2*i\)互质。

代码

#include <cstdio>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream> #define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout) using namespace std;
typedef long long LL;
const int maxn = 2e7 + 5; int phi[maxn], prime[maxn];
LL sum[maxn];
int tot = 0; void getPhi(int n)
{
for (int i = 2; i <= n; i++) phi[i] = 0;
phi[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!prime[i])
{
prime[++tot] = i;
phi[i] = i-1;
}
for (int j = 1; j <= tot; j++)
{
if (i*prime[j] > n) break;
prime[i*prime[j]] = 1;
if (i % prime[j] == 0)
{
phi[i*prime[j]] = prime[j] * phi[i];
break;
}
else phi[i*prime[j]] = (prime[j]-1)*phi[i];
}
}
} void init(int n)
{
getPhi(n); for (int i = 1; i <= n; i++)
if (i % 2 == 1)
sum[i] = sum[i-1] + phi[i] / 2;
else
sum[i] = sum[i-1] + phi[i];
} int t, n;
int main()
{
init(2e7);
scanf("%d", &t);
for (int ca = 1; ca <= t; ca++)
{
scanf("%d", &n);
printf("%lld\n", sum[n]);
}
}

Problem I. Count - 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 5528 Count a * b 欧拉函数

    题意: 定义函数\(f(n)\)为\(i \cdot j \not\equiv 0 \; (mod \; n)\)的数对\((i,j)\)的个数\((0 \leq i,j \leq n)\) \(g( ...

  9. hdu 1787(欧拉函数)

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

随机推荐

  1. svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupte...

    今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...

  2. 使用Quartz任务调用的时候报错Based on configured schedule, the given trigger will never fire.

    org.quartz.SchedulerException: Based on configured schedule, the given trigger will never fire. 大概意思 ...

  3. c#写word文档基础操作(自己控制样式)

    下面一个函数,建立一个Word 文档,添加页眉.页脚,在内容中两个不同字体的Hello!!!   来自 <http://bbs.csdn.net/topics/340041961> pub ...

  4. vue使用element-ui实现按需引入

    基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 MintUi 基于vue 移动端的ui框架 http://element.ele ...

  5. 【干货】JavaScript DOM编程艺术学习笔记1-3

    从7月29号到8月8号,断断续续地看完了这本书,做了部分实践联系.总体感觉本书真的只能算是个入门,学完之后看到库的那一章才感觉是个大坑,实践中大部分应该都是用的现成的库吧,所以还要重新学习一个库,但是 ...

  6. Miner3D Professional专业版

    ——高级的可视化数据分析为专业人士量身打造 Miner3D Professional 专业版可以帮助工程师,研究人员,分析师,管理人员,知识工作者,以分析师和信息专家,在较短的时间内作出更好的判断.探 ...

  7. DataGridView控件使用大全

    转自:http://www.cnblogs.com/xiaofengfeng/archive/2011/04/16/2018504.html DataGridView控件 DataGridView是用 ...

  8. Linux最常用命令实战

    1.改变机器的名称: vim /etc/hostname Master 在文件中修改机器名称为我们想要的名称(相当于域名) 可以通过shutdown -h now 关闭 2.查看当前机器IP: ifc ...

  9. IOS autosizing(设置控件的固定位置大小)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  10. IOS UIImageView的帧动画

    ● UIImageView可以让一系列的图片在特定的时间内按顺序显示 ● 相关属性解析: ● animationImages:要显示的图片(一个装着UIImage的NSArray) ● animati ...