【洛谷P2568】GCD
题目大意:给定整数 \(N\),求\(1\le x,y\le N\) 且 \(gcd(x,y)\) 为素数的数对 \((x,y)\) 有多少对。
题解:
\]
\]
\]
\]
因此,利用线性筛求出欧拉函数的前缀和,直接枚举素数计算答案贡献即可。
代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
int n,prime[maxn],tot;
ll phi[maxn],sum[maxn];
bool vis[maxn];
void sieve(){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i])prime[++tot]=i,phi[i]=i-1;
for(int j=1;i*prime[j]<=n;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}else{
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+phi[i];
}
int main(){
scanf("%d",&n);
sieve();
ll ans=0;
for(int i=1;i<=tot;i++)ans+=(2*sum[n/prime[i]]-1);
printf("%lld\n",ans);
return 0;
}
【洛谷P2568】GCD的更多相关文章
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- 洛谷P2568 GCD(线性筛法)
题目链接:传送门 题目: 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 ...
- 洛谷 P2568 GCD
https://www.luogu.org/problemnew/show/P2568#sub 最喜欢题面简洁的题目了. 本题为求两个数的gcd是素数,那么我们将x和y拆一下, 假设p为$gcd(x, ...
- 洛谷 - P2568 - GCD - 欧拉函数
https://www.luogu.org/problemnew/show/P2568 统计n以内gcd为质数的数的个数. 求 \(\sum\limits_p \sum\limits_{i=1}^{n ...
- [洛谷P2568]GCD
题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...
- 洛谷 P2568 GCD(莫比乌斯反演)
题意:$\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)\epsilon prime]$. 对于这类题一般就是枚举gcd,可得: =$\sum_{d\epsilon prim ...
- 洛谷 P2568 GCD 题解
原题链接 庆祝一下:数论紫题达成成就! 第一道数论紫题.写个题解庆祝一下吧. 简要题意:求 \[\sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \] 其中 \(p\) ...
- 洛谷P2568 GCD(莫比乌斯反演)
传送门 这题和p2257一样……不过是n和m相同而已…… 所以虽然正解是欧拉函数然而直接改改就行了所以懒得再码一遍了2333 不过这题卡空间,记得mu开short,vis开bool //minamot ...
- 洛谷P2398 GCD SUM (数学)
洛谷P2398 GCD SUM 题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入 ...
- 洛谷 P1890 gcd区间
P1890 gcd区间 题目提供者 洛谷OnlineJudge 标签 数论(数学相关) 难度 普及/提高- 题目描述 给定一行n个正整数a[1]..a[n]. m次询问,每次询问给定一个区间[L,R] ...
随机推荐
- C# Note15:设置Window图标的正确方式
Windows Presentation Foundation(WPF)独立应用程序有两种类型的图标: 一个程序集(assembly) 图标,通过在应用程序的项目构建文件中使用<Applicat ...
- SSM+shiro及相关插件的整合maven所有依赖,详细注释版,自用,持续更新
整合了SSM+shiro框架,slf4j+logback日志,及一些好用的插件PageHelper,mybatis-generator,Lombok,fastjson等等 <?xml versi ...
- Google css & Google fonts
最近用某开源模板做提案的时候, 抓包工具老是有外部Request. 问题出在某css中有这么一句: @import url(https://fonts.googleapis.com/css?famil ...
- vue之v-for使用说明
demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- web跨域请求
第一种情况: 1. sina.com=====>baidu.com/xxx.jsp 也就是前面的域名不相同,(url第三根斜杠之前的内容,也就是主机) 2:localhost =====> ...
- 2.ansible-playbook基本参数
ansible-playbook的参数--force-handlers run handlers even if a task fails 强制执行handler--list-tags list al ...
- Jquery实现检测用户输入用户名和密码不能为空
要求 1.用户名和密码为空点击登录时提示相应的提示 2.获取用户名输入框时,错误提示清除 思路 1.创建1个input-text标签和1个input-password标签,1个input-botton ...
- 双系统windows+linux如何正确删除linux
双系统windows+linux如何正确删除linux 2017年11月16日 10:42:49 dovepym 阅读数:26363 之前在windows的基础上又安装了ubuntu系统16.04 ...
- Luogu5205 【模板】多项式开根(NTT+多项式求逆)
https://www.cnblogs.com/HocRiser/p/8207295.html 安利! 写NTT把i<<=1写成了i<<=2,又调了一年.发现我的日常就是数组开 ...
- POJ 3667 Hotel(算竞进阶习题)
线段树区间染色 题目要求最大的连续段的左端点,我们在查询的时候返回最左端即可,注意查找顺序,应该从左到右!! 另外这类染色的push_down其实比较简单,直接染成上一层的标记即可 push_up和连 ...