题目大意

  \(t\)组询问, 每组询问给定\(n\),求\(\sum_{k=1}^n[n,k]\),其中\([a,b]\)表示\(a\)和\(b\)的最小公倍数 .

  \(t\leq 300000,n\leq 1000000\)

题解

\[\begin{align}
\sum_{k=1}^n[k,n]&=n\sum_{k=1}^n\frac{k}{(k,n)}\\
&=n\sum_{p|n}\frac{1}{p}\sum_{k=1}^nk[(k,n)=p]\\
&=n\sum_{p|n}\sum_{k=1}^{\lfloor\frac{n}{p}\rfloor}k[(k,\frac{n}{p})=1]\\
&=n\sum_{p|n}\frac{\frac{n}{p}\phi(\frac{n}{p})+[\frac{n}{p}=1]}{2}\\
&=n\sum_{p|n}\frac{p\phi(p)+[p=1]}{2}
\end{align}
\]

  用线性筛或者其他方法处理出\(\phi\)函数,调和级数的复杂度预处理,每次查表。或者枚举因子。

  时间复杂度:\(O(n\log n)\)或\(O(n+t\sqrt{n})\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int miu[1000010];
int phi[1000010];
int p[1000010];
int b[1000010];
ll e[1000010];
ll f[1000010];
int cnt;
int maxn=1000000;
map<int,ll> d;
void init()
{
memset(b,0,sizeof b);
int i,j;
cnt=0;
for(i=2;i<=maxn;i++)
{
if(!b[i])
{
p[++cnt]=i;
miu[i]=-1;
phi[i]=i-1;
}
for(j=1;j<=cnt&&i*p[j]<=maxn;j++)
{
b[i*p[j]]=1;
if(i%p[j]==0)
{
miu[i*p[j]]=0;
phi[i*p[j]]=phi[i]*p[j];
break;
}
miu[i*p[j]]=-miu[i];
phi[i*p[j]]=phi[i]*phi[p[j]];
}
}
e[1]=1;
for(i=2;i<=maxn;i++)
e[i]=(ll(i)*phi[i])/2;
for(i=1;i<=maxn;i++)
for(j=i;j<=maxn;j+=i)
f[j]+=e[i];
}
void solve()
{
int n;
scanf("%d",&n);
printf("%lld\n",f[n]*n);
}
int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
init();
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}

【XSY2470】lcm 数学的更多相关文章

  1. hdu 4497 GCD and LCM 数学

    GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...

  2. Codeforces Round #613 (Div. 2) C. Fadi and LCM (数学)

    题意:给你一个正整数\(x\),找两个正整数\(a\),\(b\),使得\(lcm(a,b)=x\),并且\(max(a,b)\)最小. 题解:我们知道,\(lcm(a,b)=a*b/gcd(a,b) ...

  3. Codeforces #6241 div2 C. Orac and LCM (数学)

    题意:给你一个数列,求所有子序列对的\(lcm\),然后求这些所有\(lcm\)的\(gcd\). 题解:我们对所有数分解质因数,这里我们首先要知道一个定理: ​ 对于\(n\)个数,假如某个质数\( ...

  4. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  5. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  6. UVA 10892 LCM Cardinality 数学

    A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...

  7. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  8. Codeforces Round #328 (Div. 2) C. The Big Race 数学.lcm

    C. The Big Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/probl ...

  9. hdu 5584 LCM Walk(数学推导公式,规律)

    Problem Description A frog has just learned some number theory, and can't wait to show his ability t ...

随机推荐

  1. OSGI基础环境搭建

    1.安装jdk,步骤自行搜索 2.下载eclipse,用luna版本,下载地址: https://pan.baidu.com/s/1gdfmW5znU4fltsLCAo8tkg 提取码: nrg7 3 ...

  2. Django 中间件版登录验证

    中间件版的登录验证需要依靠session,所以数据库中要有django_session表. urls.py # urls.py from django.conf.urls import url fro ...

  3. Python全栈开发之路 【第五篇】:Python基础之函数进阶(装饰器、生成器&迭代器)

    本节内容 一.名称空间 又名name space,就是存放名字的地方.举例说明,若变量x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的地方. 名称空间共3种,分别如下 ...

  4. 使用 Markdown编辑

    作用: 学习笔记,整理日志, 发布日记,杂文,所见所想 撰写发布技术文稿(代码支持) 撰写发布学术论文(LaTeX 公式支持) sublime text3插件 输入 Shift + Ctrl + P, ...

  5. vue入门(一)

    通过JS引用vue就不说了,重点说一下使用npm搭建vue脚手架. (以下是windows系统下的操作,win7+) npm是个命令行工具,在搭建vue脚手架之前首先要安装nodeJS,下面是node ...

  6. Linux sed使用方法

    目录 sed处理流程 测试数据 sed命令格式 sed命令行格式 行定位 定位1行 定位区间行(多行) 定位某一行之外的行 定位有跨度的行 操作命令 -a (新增行) -i(插入行) -c(替代行) ...

  7. Linux中profile

    http://www.cnblogs.com/mmfzmd517528/archive/2012/07/05/2577988.html

  8. mysql 5.7:show_compatibility_56

    show_compatibility_56 - rudy gao - CSDN博客 https://blog.csdn.net/rudygao/article/details/50403107 [SO ...

  9. MySQL设计SQL语句优化规范

    原文:http://bbs.landingbj.com/t-0-240751-1.html 1. 使用mysql explain 对sql执行效率进行检测 ,explain显示了mysql如何使用索引 ...

  10. babel (三) babel polly-fill

    Babel includes a polyfill that includes a custom regenerator runtime and core-js. This will emulate ...