题意

给一个\(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. [Environment Build] 工作中遇到的关于Git的问题

    修改已经提交的commit中的author, commit git commit --amend --author 'Your Name <Your Email>' git commit ...

  2. JavaScript中函数声明和函数表达式的区别

    声明一个函数: var x=1; foo(); function foo() { console.log(x);//1 } myfun();//报错 定义一个函数表达式: var myfun=myfo ...

  3. Struts2笔记2

    一.请求参数封装 1.属性驱动:     (1).无实体类情况:属性和动作类在一起         a.编写jsp页面,提交数据,例如name和age            <form acti ...

  4. jQuerychicun

    jQuery 尺寸 方法 jQuery 提供多个处理尺寸的重要方法: width() height() innerWidth() innerHeight() outerWidth() outerHei ...

  5. mysql decimal类型与decimal长度用法详解

    三者的区别介绍 float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位) double:双精度实型,含字节数为8,64bit数值范围-1.7E308~1.7E ...

  6. 【Android开发笔记】杂项

    Android studio shift+enter : start new line Theme 将     <style name="AppBaseTheme" pare ...

  7. COGS 1453. [USACO NOV]空牛栏

    ★★   输入文件:empty.in   输出文件:empty.out   简单对比时间限制:1 s   内存限制:64 MB [题目描述] FJ建的新牛棚里有N(2<=N<=3,000, ...

  8. linux 命令——13 less(转)

    less 工 具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性. 在 more 的时候,我们并没有办法向前 ...

  9. linux 命令——9 touch (转)

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a    ...

  10. js基础笔录

    1.页面中获取对象 document.getElementById("demo") 2.在页面加载时向 HTML 的 <body> 写文本 document.write ...