【bzoj2818】 Gcd
http://www.lydsy.com/JudgeOnline/problem.php?id=2818 (题目链接)
题意
求给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对。
Solution
对于gcd(x,y)=p的数对个数,就相当于x/p和y/p互质。
细节
前缀和开LL
代码
// poj2478
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define MOD 10000
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=10000010;
int phi[maxn],p[maxn],vis[maxn],n;
LL s[maxn]; void calphi() {
phi[1]=1;
for (int i=2;i<=n;i++) {
if (!vis[i]) {p[++p[0]]=i;phi[i]=i-1;}
for (int j=1;j<=p[0];j++) {
if (i*p[j]>n) break;
vis[p[j]*i]=1;
if (i%p[j]==0) {phi[p[j]*i]=phi[i]*p[j];break;}
else phi[p[j]*i]=phi[p[j]]*phi[i];
}
}
for (int i=2;i<=n;i++) s[i]=s[i-1]+phi[i];
}
int main() {
scanf("%d",&n);
calphi();
LL ans=p[0];
for (int i=1;i<=p[0];i++) {
if (n/p[i]==1) break;
ans+=2*s[n/p[i]];
}
printf("%lld",ans);
return 0;
}
【bzoj2818】 Gcd的更多相关文章
- 【BZOJ2818】Gcd(莫比乌斯反演)
[BZOJ2818]Gcd(莫比乌斯反演) 题面 Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Ou ...
- 【bzoj2818】: Gcd 数论-欧拉函数
[bzoj2818]: Gcd 考虑素数p<=n gcd(xp,yp)=p 当 gcd(x,y)=1 xp,yp<=n满足条件 p对答案的贡献: 预处理前缀和就好了 /* http://w ...
- 【bzoj2818】Gcd
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4344 Solved: 1912[Submit][Status][Discuss ...
- 【BZOJ2818】Gcd 欧拉筛
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
- 【BZOJ-2818】Gcd 线性筛
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3347 Solved: 1479[Submit][Status][Discuss ...
- 【BZOJ2818】Gcd [莫比乌斯反演]
Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...
- 【BZOJ2818】Gcd (欧拉函数)
网址:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 一道数论裸题,欧拉函数前缀和搞一下就行了. 小于n的gcd为p的无序数对,就是phi(1 ...
- 【BZOJ2818】Gcd(莫比乌斯反演,欧拉函数)
题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对 1<=N<=10^7 思路:莫比乌斯反演,同BZOJ2820…… ; ..max]of ...
- 【反演复习计划】【bzoj2818】gcd
就是之前的2820的升级版. 把暴力枚举素数改成预处理就随便A了. #include<bits/stdc++.h> #define N 10000005 #define ll long l ...
随机推荐
- 01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
1.什么是Spring : v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:u ...
- 解决jquery.validate.js的验证bug
版本提示:jq为1.4.4, jquery.validate 为jQuery validation plug-in 1.7 问题: a.选填选项,如邮箱设置格式验证,那么情况输入框,验证label变成 ...
- jQuery的prop和attr方法之间区别
JQuery.attr(): Get the value of an attribute for the first element in the set of matched elements. J ...
- JS使构造函数与new操作符无关
function User(name, passwordHash) { this.name = name; this.passwordHash = passwordHash; } 当使用User函数创 ...
- Linux下使用automake、autoconf生成configure文件
一.生成configure过程中各文件之间的关系图 二.详细介绍 autoscan: 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是con ...
- ViewModelBase && ObservableObject
ViewModelBase && ObservableObject 在Mvvm中,ViewModel和Model都需要具有通知界面更新数据的能力,这都要借助于WPF中的 INotify ...
- opencv6.3-imgproc图像处理模块之边缘检测
接opencv6.2-improc图像处理模块之图像尺寸上的操作 本文大部分都是来自于转http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutori ...
- Android一键多渠道分发打包实战和解析
当项目需要有更多的客户的时候,你就会考虑将apk上架到应用商店了,无奈天朝Android应用商店真的是百家争鸣,据某地不完全统计已经有900+.若将Apk上架到所有的应用商店是个好主意,但是据统计也就 ...
- Windows Server+AMD GPU+HDMI时_黑边_不铺满问题的解决办法
HDMI接显示器或电视,有黑边或者被放大了是个很常见的问题,显卡设置界面里改下Scale或者Overscan/Underscan就行,可问题是WindowsServer版的CCC没有控制颜色对比度和缩 ...
- Addressing Complex and Subjective Product-Related Queries with Customer Reviews-www2016-20160505
1.Information publication:www2016 author:Julian McAuley 2.What 学习商品评论中的信息,对商品的提问,自动给出回答:按照相关程度排序 3.D ...