/**
题目:GCD
链接:https://vjudge.net/contest/178455#problem/E
题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对. 1<=N<=10^7 思路:
定义:
f(n)表示gcd(x,y)==n的对数。
F(n)表示n|gcd(x,y)的对数。 枚举n以内的素数p; f(p) = sigma(mu[d/p]*F(d)) (p|d) F(d) = (n/d)*(n/d); N/10*sqrt(N)复杂度。 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const LL INF = 1e10;
const int mod = 1e9 + ;
const int maxn = 1e7 + ;
int prime[maxn], tot, not_prime[maxn];
int mu[maxn], sum[maxn];
void init()
{
mu[] = ;
tot = ;
for(int i = ; i < maxn; i++){
if(!not_prime[i]){
prime[++tot] = i;
mu[i] = -;
}
for(int j = ; prime[j]*i<maxn; j++){
not_prime[prime[j]*i] = ;
if(i%prime[j]==){
mu[prime[j]*i] = ;
break;
}
mu[prime[j]*i] = -mu[i];
}
}
for(int i = ; i < maxn; i++) sum[i] = sum[i-]+mu[i];
}
LL solve(int n)///x在[1,n], y在[1,n],z在[1,n] gcd(x,y,z)=1的对数。
{
LL ans = ;
int last;
for(int i = ; i <= n; i=last+){
last = n/(n/i);
ans += (LL)(sum[last]-sum[i-])*(n/i)*(n/i);
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
int n;
init();
while(scanf("%d",&n)==)
{
LL ans = ;
for(int i = ; i <= tot&&prime[i]<=n; i++){
ans += solve(n/prime[i]);
//cout<<"prime = "<<prime[i]<<endl;
//cout<<"ans = "<<ans<<endl;
}
printf("%lld\n",ans);
}
return ;
}

GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.的更多相关文章

  1. BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)

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

  2. Bzoj 2818: Gcd(莫比乌斯反演)

    2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对 ...

  3. luogu2658 GCD(莫比乌斯反演/欧拉函数)

    link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD ...

  4. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  5. 【BZOJ2818】Gcd [莫比乌斯反演]

    Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...

  6. BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  7. bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  8. Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)

    2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  9. 【BZOJ2820】YY的GCD(莫比乌斯反演 数论分块)

    题目链接 大意 给定多组\(N\),\(M\),求\(1\le x\le N,1\le y\le M\)并且\(Gcd(x, y)\)为质数的\((x, y)\)有多少对. 思路 我们设\(f(i)\ ...

随机推荐

  1. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)如何在TwinCAT Scope中做变量监控

    为了更好的监控变量,可以打开ScopeView即变量监控器   添加一个Scope View,然后右击添加一个Channel   我们在之前登录的时候可以选择Run-Time的端口(默认是801)   ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何修改某个轴的数值单位

    在某个轴上双击,切换到Settings,然后可以再Unit中修改为角度,弧度,mm     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/aceta ...

  3. WIN7如何查找网络打印机

    1 在开始菜单中输入"打印机"并点击"添加打印机" 2 点击下面一个,并搜索家庭组的打印机,一般可以搜到(注意这台电脑不能关机或睡眠). 3 查找并添加会需要安 ...

  4. nginx+lua+redis 处理APK包替换

    nginx + lua +redis 安装与使用入门: http://huoding.com/2012/08/31/156 nginx httpEchoModule : http://wiki.ngi ...

  5. 有些类库(node.js版)

    做项目经常会用到好些类库,大的还好说,用的多了自然记住了名字.如express. 但也有些小而精湛的类库,不仅提供了良好的功能,本身的实现也值得研究.暂记于此. 1.web类 request    简 ...

  6. react-redux 中 connect 的常用写法

    1.方式一 export default connect(({ monitorRedux })=>{ return { data: monitorRedux.data } })(Monitor) ...

  7. [Spring MVC - 2A] - java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

    严重: Servlet.service() for servlet [springMVC] in context with path [/ExceptionManageSystem] threw ex ...

  8. IOS炫酷的引导界面

    代码地址如下:http://www.demodashi.com/demo/11246.html 一.准备工作 1.先用时ps工具制作好图片 2.然后计算好每张图片通过滑动视图的偏移量来改变图片的位置 ...

  9. spring jdbc查询 依赖JdbcTemplate这个类模版封装JDBC的操作

    package cn.itcast.spring.jdbc; import java.util.List; import org.springframework.jdbc.core.support.J ...

  10. 阻塞IO、非阻塞IO的区别

    1.类与类之间的关系:依赖,实现,泛化(继承),关联,组合,聚合. 1)依赖(虚线):一个类是 另一个类的函数参数 或者 函数返回值. 2)实现(实线加小圆):对纯虚函数类(抽象类)的实现. 3)继承 ...