题目描述

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. pytest--fixure前置执行一个函数

    import pytest@pytest.fixture()def login_r(): print('登陆') @pytest.fixture()def open_browser(): print( ...

  2. Centos下的 docker安装

    安装一些必要的系统工具:sudo yum install -y yum-utils device-mapper-persistent-data lvm2 添加软件源信息:sudo yum-config ...

  3. 记录一次工作中jvm被linux杀死的调查

    首先,以后碰到任何jvm的错误,先看日志!!!!!!!! web项目在tomcat目录下的log里,或者自己设定的errorfile目录下.总之,找到一切可以运用的日志,比如crash日志,cored ...

  4. ubuntu查看时间同步服务器的匹配源

    当服务器时间与设定好的同步时间源的时间有差异的时候,一般都需要先查看本机的时间同步服务功能是否在正常的运转,以及同步的时间源是哪里,在这里为大家提供一个检查时间用的命令. ubuntu版本 servi ...

  5. centos WPS 字体安装

    首先下载字体,解压后将整个wps_symbol_fonts目录拷贝到/usr/share/fonts目录下,然后赋予可读可执行权限. 权限设置操作如下: cd /usr/share/fonts/ ch ...

  6. Shell [[]]详解:检测某个条件是否成立

    [[ ]]是 Shell 内置关键字,它和 test 命令类似,也用来检测某个条件是否成立. test 能做到的,[[ ]] 也能做到,而且 [[ ]] 做的更好:test 做不到的,[[ ]] 还能 ...

  7. python代码{v: k for k, v in myArray.items()}是什么意思?

    最近在扒vnpy的源码总能看到{v: k for k, v in ORDERTYPE_VT2HUOBI.items()}这样的源码,就是不知道什么意思 然后万能的google找到了Quora的一个类似 ...

  8. vue中数据绑定遇到的问题

    <!-- 使用element中的表格组件,在编辑的时候传递每行的数据 --> <el-button size="small" type="success ...

  9. 使用Process子类创建进程

    #_author:来童星#date:2019/12/17# 使用Process子类创建进程from multiprocessing import Processimport timeimport os ...

  10. 配置类一@Configuration

    import org.springframework.context.annotation.Configuration; @Configuration用于定义配置类,可替换xml配置文件,被注解的类内 ...