题目描述

for i=1 to n

for j=1 to n

 sum+=gcd(i,j)

给出n求sum. gcd(x,y)表示x,y的最大公约数.

输入输出格式

输入格式:

n

输出格式:

sum

输入输出样例

输入样例#1:

2
输出样例#1:

5

说明

数据范围

30% n<=3000

60% 7000<=n<=7100

100% n<=100000


题目的意思大概是这样的

O(n2)枚举当然是不行的啦。

考虑枚举k,求gcd为k的“数对”的个数。

而可以证明gcd为k的“数对”的个数为

利用容斥把gcd为2k,3k,4k的“数对”的个数减去就好啦?

注意k要从大到小枚举。

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long ll; const int maxn=; int n;
ll dp[maxn],ans=; int main(){
scanf("%d",&n);
for(int i=n;i>;i--){
dp[i]=1ll*(n/i)*(n/i);
for(int j=(i<<);j<=n;j+=i)
dp[i]-=dp[j];
ans+=dp[i]*i;
}
printf("%lld\n",ans);
return ;
}

luoguP2398 GCD SUM [gcd]的更多相关文章

  1. LuoguP2398 GCD SUM

    题目地址 题目链接 题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入格式: n ...

  2. acdream 1148 GCD SUM 莫比乌斯反演 ansx,ansy

    GCD SUM Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatis ...

  3. GCD SUM 强大的数论,容斥定理

    GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...

  4. Luogu2398 GCD SUM

    Luogu2398 GCD SUM 求 \(\displaystyle\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j)\) \(n\leq10^5\) 数论 先常规化式子(大雾 \[ ...

  5. bnu——GCD SUM (莫比乌斯反演)

    题目:GCD SUM 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=39872 算法:莫比乌斯反演.优化 #include<stdio.h& ...

  6. 洛谷P2398 GCD SUM [数论,欧拉筛]

    题目传送门 GCD SUM 题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入格式 ...

  7. P2398 GCD SUM

    P2398 GCD SUM一开始是憨打表,后来发现打多了,超过代码长度了.缩小之后是30分,和暴力一样.正解是,用f[k]表示gcd为k的一共有多少对.ans=sigma k(1->n) k*f ...

  8. 洛谷P2398 GCD SUM

    题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入格式: n 输出格式: sum ...

  9. 洛谷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的最大公约数. 输入输出格式 输入 ...

随机推荐

  1. 2018-2-13-win10-uwp-让焦点在点击在页面空白处时回到textbox中

    title author date CreateTime categories win10 uwp 让焦点在点击在页面空白处时回到textbox中 lindexi 2018-2-13 17:23:3 ...

  2. 笔记32 SpringMVC中使用静态资源、处理中文乱码

    一.静态资源的使用 在WebConfig.java中有如下代码段 @Override // 配置静态资源处理 public void configureDefaultServletHandling(D ...

  3. sqlmap 使用方法及实例

    注:标黄处为输入内容     批注为得到的信息 1.-u url --dbs 爆数据库 [root@Hacker~]# Sqlmap -u http://www.lbgold.com/article_ ...

  4. Dockfile中的命令如何在.sh中执行

    有类似如下内容的Dokefile文件.1 RUN cd /tmp/patch \ && /lib/python3./site-packages/moduleA/a.* \ && ...

  5. select 可输入的下拉框

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="U ...

  6. 安装percona-toolkit.rpm时候报错:perl(Time::HiRes) is needed by percona-toolkit-2.2.16-1.noarch

    1.安装percona-toolkit.rpm时候报错: warning: percona-toolkit.rpm: Header V4 DSA/SHA1 Signature, key ID cd2e ...

  7. Servlet - HTTP协议相关

    1. 术语 : 请求 : 客户端根据用户所给的地址信息将数据发送给服务器的过程 响应 : 服务器将请求的处理结果发送给浏览器的过程 2. HTTP协议 : 超文本传输协议 ( Hyper Text T ...

  8. UML关系类图

    在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregation),组合(Composit ...

  9. Ubuntu安装msf

    环境 root运行 ubuntu18.04 腾讯云服务器 控制面板上面所有的端口全部放行 本机自带防火墙已拆 拆墙是为了能msf接受到会话 安装 curl https://raw.githubuser ...

  10. Python中将dict转换为kwargs

    Python中将dict转换为kwargs 我们都知道kwargs是变长kv参数,能否将dict转换成kwargs. 在python调用函数的时候func(**{'type'='event'}),可以 ...