http://www.lydsy.com/JudgeOnline/problem.php?id=2818

我很sb的丢了原来做的一题上去。。

其实这题可以更简单。。

$$f[i]=1+2 \times \phi (i) $$

那么答案就是

$$\sum_{p是质数} f[n/p]$$

就丢原来的题了。。。不写了。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next) const int N=10000005;
int p[N], cnt, np[N], mu[N], g[N], sum[N];
void init() {
mu[1]=1;
for2(i, 2, N) {
if(!np[i]) p[++cnt]=i, mu[i]=-1, g[i]=1;
for1(j, 1, cnt) {
int t=p[j]*i; if(t>=N) break;
np[t]=1;
if(i%p[j]==0) { mu[t]=0; g[t]=mu[i]; break; }
mu[t]=-mu[i]; g[t]=mu[i]-g[i];
}
}
for2(i, 1, N) sum[i]=sum[i-1]+g[i];
} int main() {
init();
int n=getint();
ll ans=0;
int pos;
for(int i=1; i<=n; i=pos+1) {
pos=min(n/(n/i), n/(n/i));
ans+=(ll)(sum[pos]-sum[i-1])*(n/i)*(n/i);
}
printf("%lld\n", ans);
return 0;
}

  


Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

hint

对于样例(2,2),(2,4),(3,3),(4,2)

1<=N<=10^7

Source

【BZOJ】2818: Gcd(欧拉函数/莫比乌斯)的更多相关文章

  1. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  2. ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...

  3. BZOJ 2818 Gcd(欧拉函数+质数筛选)

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 9108  Solved: 4066 [Submit][Status][Discu ...

  4. 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)

    P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...

  5. BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec  Memory Limit ...

  6. HYSBZ 2818 Gcd【欧拉函数/莫比乌斯】

    I - Gcd HYSBZ - 2818 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample In ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)

    UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...

  9. Bzoj-2818 Gcd 欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...

  10. 51nod 1237 最大公约数之和 V3【欧拉函数||莫比乌斯反演+杜教筛】

    用mu写lcm那道卡常卡成狗(然而最后也没卡过去,于是写一下gcd冷静一下 首先推一下式子 \[ \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) \] \[ \sum_{i= ...

随机推荐

  1. DICOM医学图形处理:storescp.exe与storescu.exe源码剖析,学习C-STORE请求(续)

    转载:http://blog.csdn.net/zssureqh/article/details/39237649 背景: 上一篇博文中,在对storescp工具源文件storescp.cc和DcmS ...

  2. 暑假热身 B. 下载测速

    最近,nono终于结束了每年一次的为期12个月的冬眠,醒来的第一件事就是——看电影!!nono发现最近一年出现了各种很好很强大的电影,例如这个.这个.还有这个. 于是nono直接把这些电影全部扔进了下 ...

  3. HTML前端——CSS样式

    使用CSS样式的方式: HTML<!DOCTYPE> 声明标签 内链样式表<body style="background: green; margin: 0; paddin ...

  4. VirtualBox共享文件夹等高级特性

    转自: http://blog.csdn.net/longerzone/article/details/32119457 http://www.oschina.net/translate/10-vir ...

  5. dubbo作为消费者注册过程分析

    请支持原创: http://www.cnblogs.com/donlianli/p/3847676.html   作者当前分析的版本为2.5.x.作者在分析的时候,都是带着疑问去查看代码,debug进 ...

  6. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. WebStorm快捷键操作

    1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + j: 输出模板 3. ctrl + b: 跳到变量申明处 4. ctrl + al ...

  8. 解决 g++ error:/usr/lib/rpm/redhat/redhat-hardened-cc1 No that file and directory

    You need to install redhat-rpm-config which is required by some of the qt switches, probably: sudo d ...

  9. mysql的日期存储字段比较int,datetime,timestamp区别

    1.首先是我们分析datetime长度是8个字节,INT的长度是4个字节,存储空间上比datatime少. 2.int存储索引的空间也比datetime少,排序效率高,查询速度比较快. 3.方便计算, ...

  10. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...