BZOJ 2818-Gcd(莫比乌斯反演)
来源:互联网 发布:跳舞直播软件 编辑:程序博客网 时间:2024/05/21 17:38
2818: Gcd
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 5969 Solved: 2648
[Submit][Status][Discuss]
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
题意:求有多少对(x,y)满足gcd(x,y)为质数
题解:我们设f(n)为gcd(x,y)=n的对数,F(n)为gcd(x,y)为n的倍数的对数
然后我们可以利用莫比乌斯反演定理,将mu函数带入进来。
#include<map> #include<stack> #include<queue> #include<vector> #include<string> #include<math.h> #include<stdio.h> #include<iostream> #include<string.h> #include<stdlib.h> #include<algorithm> #include<functional> using namespace std;typedef long long ll;#define inf 1000000000 #define mod 1000000007 #define maxn 10000005 #define lowbit(x) (x&-x) #define eps 1e-9 int a[maxn] = { 1,1 }, b[maxn/10], mu[maxn], cnt;void init(){ll i, j;mu[1] = 1;for (i = 2;i < maxn;i++){if (a[i] == 0)b[++cnt] = i, mu[i] = -1;for (j = 1;j <= cnt && b[j] * i < maxn;j++){a[b[j] * i] = 1;if (i % b[j] == 0){mu[b[j] * i] = 0;break;}elsemu[b[j] * i] = -mu[i];}}}int main(void){init();ll i, j, n, ans = 0;scanf("%lld", &n);for (i = 1;i <= cnt && b[i] <= n;i++){ll sum = 0;for (j = b[i];j <= n;j += b[i])sum += (ll)mu[j / b[i]] * (n / j)*(n / j);ans += sum;}printf("%lld\n", ans);return 0;}
阅读全文
0 0
- BZOJ 2818 gcd(莫比乌斯反演)
- BZOJ 2818 Gcd (欧拉筛 \ 莫比乌斯反演)
- bzoj 2818 Gcd(莫比乌斯反演)
- BZOJ 2818-Gcd(莫比乌斯反演)
- bzoj 2818 Gcd 莫比乌斯反演
- BZOJ 2818: Gcd (莫比乌斯反演)
- Bzoj 2818: Gcd(莫比乌斯反演)
- [BZOJ 2818]Gcd:莫比乌斯反演
- BZOJ 2818 Gcd + spoj 4491(莫比乌斯反演 分块)
- BZOJ 2820 YY的GCD 莫比乌斯反演
- 【莫比乌斯反演】[BZOJ 2820 YY的GCD]
- BZOJ 2820 YY的GCD 莫比乌斯反演
- [莫比乌斯反演] BZOJ 2820 YY的GCD
- BZOJ 2820 YY的GCD(莫比乌斯反演)
- bzoj 2820: YY的GCD 莫比乌斯反演
- BZOJ 2820 YY的GCD 莫比乌斯反演
- 【BZOJ 2820】YY的GCD 莫比乌斯反演
- BZOJ 2820 YY的GCD 莫比乌斯反演
- *args,**kwargs 可变参数 关键字参数 命名关键字参数 区别 用法
- hdu 1180
- 多尺度图片滑动窗口输出
- hdu1698 Just a Hook 线段树更新延迟标记
- 全排列和全组合实现
- BZOJ 2818-Gcd(莫比乌斯反演)
- 回环网卡上配置ip和外部网络通信
- 2017/8/19
- iOS尝试用测试驱动的方法开发一个列表模块【二】
- python图像预处理
- 在Visual Studio中配置命令行环境(Command Prompt)
- 2017CCPC 网络选拔赛 1005 CaoHaha's staff
- poj3468 A Simple Problem with Integers 线段树 更新延迟
- Unity5--小球走直线、弧形脚本