P2568 GCD
\(\color{#0066ff}{ 题目描述 }\)
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对.
\(\color{#0066ff}{输入格式}\)
一个整数N
\(\color{#0066ff}{输出格式}\)
答案
\(\color{#0066ff}{输入样例}\)
4
\(\color{#0066ff}{输出样例}\)
4
\(\color{#0066ff}{数据范围与提示}\)
对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
\(\color{#0066ff}{ 题解 }\)
\]
\]
拿\(\varphi\) xjb统计一下就行了
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e7 + 10;
int pri[maxn], tot, n;
LL phi[maxn];
bool vis[maxn];
signed main() {
n = in();
phi[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]) pri[++tot] = i, phi[i] = i - 1;
for(int j = 1; j <= tot && (LL)i * pri[j] <= n; j++) {
vis[i * pri[j]] = true;
if(i % pri[j] == 0) {
phi[i * pri[j]] = phi[i] * pri[j];
break;
}
else phi[i * pri[j]] = phi[i] * (pri[j] - 1);
}
}
for(int i = 2; i <= n; i++) phi[i] += phi[i - 1];
LL ans = 0;
for(int i = tot; i >= 1; i--) {
ans += (phi[n / pri[i]] << 1LL) - 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
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 ...
- Luogu P2568 GCD
我们首先发现这样肯定是做不了的,所以我们枚举为\(gcd(x,y)=d\)的\(d\) 然后考虑以下的性质: \(gcd(x,y)=1 \Leftrightarrow gcd(px,py)=p(p为素 ...
- 洛谷P2568 GCD(线性筛法)
题目链接:传送门 题目: 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 ...
- [洛谷P2568]GCD
题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...
- 【洛谷】P2568 GCD
前言 耻辱,我这个OI界的耻辱! 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对.输入格式 一个整数N输出格式答案输入输出样例 输入 4 ...
- 洛谷 P2568 GCD(莫比乌斯反演)
题意:$\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)\epsilon prime]$. 对于这类题一般就是枚举gcd,可得: =$\sum_{d\epsilon prim ...
- 「Luogu P2568 GCD」
看到这是一道紫题还是和gcd有关的才点进来(毕竟数论只会gcd). 前置芝士 质数**(又称素数):因数只有1和本身,但是很特殊的1不是一个质数. gcd**:欧几里得算法,又称辗转相除法,可以在约为 ...
随机推荐
- cron job error : c queue max run limit reached
在cron job的日志中发现以下报错: ! c queue max run limit reached Wed Aug 28 12:56:00 2013 ! rescheduling a cron ...
- leetcode628
这道题十分不容易啊,做到半夜. class Solution { public: static int cmp628(int a, int b) { return a > b; } static ...
- ios AppStore 帐号申请
App Store最新审核指南 https://developer.apple.com/support/app-review/cn/ http://www.woshipm.com/ucd/144218 ...
- C语言学习笔记--函数
1. C 语言中的函数 (1)函数的由来: 程序 = 数据 + 算法→C 程序 = 数据 + 函数 (2)模块化程序设计 (3)C 语言中的模块 2. 面向过程的程序设计 (1)面向过程是一种以过程为 ...
- DAY17-Django之logging
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': ...
- jQuery选择器大全整理
一.选择网页元素 $(document) //选择整个文档对象 $('#myId') //选择ID为myId的网页元素 $('div.myClass') // 选择class为myClass的div元 ...
- 使用git将代码传到github
廖雪峰git教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 注:add加入 ...
- Ros学习topic——小海龟
ROS Topics 1.rqt_graph:创建一个显示当前系统运行情况的动态图形 安装 $ sudo apt-get install ros-<distro>-rqt $ sudo a ...
- 【摘自张宴的"实战:Nginx"】使用nginx的fastcgi_cache缓存php输出的内容
亲自测试发现,fastcgi_cache虽然可以缓存生成的php输出的文件,但是有个弊端,在缓存的失效时间之内,你继续访问这个地址,输出的内容没有发生变化,即使数据库新增了数据或者删除了数据,所以不适 ...
- 如何使用EditPlus将json格式字符串默认为UTF-8格式
1.首先用EditPlus打开json格式的文件 2.选择Tools工具栏下的Configure User Tools 3.点击左侧File 4.在右侧Default encoding中选择UTF- ...