题目大意

  \(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. 最值反演 min-max容斥

    说实话这些博客早晚都要整理后上m***999. 最值反演是针对一个集合中最大/最小值的反演. \[ \max\{S\}=\sum_{T\subset S}(-1)^{|T|+1}\min\{T\} \ ...

  2. Educational Codeforces Round 52 (Rated for Div. 2) -C

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  3. case when then的用法-leetcode交换工资

    case具有两种格式:简单case函数和case搜索函数. --简单case函数 case sex when ' then '男' when ' then '女’ else '其他' end --ca ...

  4. Python_迭代器、生成器、列表推导式,生成器表达式

    1.迭代器 (1)可迭代对象 s1 = ' for i in s1: print(i) 可迭代对象 示例结果: D:\Python36\python.exe "E:/Python/课堂视频/ ...

  5. html总结:表格中的文字居中

    <style> table { text-align:center; } </style>

  6. semantic-ui 标题

    在semantic-ui中定义了5中标题样式,注意HTML中有h1-h6,而semantic-ui中只有h1-h5. 不过需要注意的是,semantic-ui的标题仍旧使用h1-h5来表示,但是在cl ...

  7. Ubuntu18.04更新源

    一.备份/etc/apt/sources.list文件 cd /etc/apt sudo cp sources.list sources.list.old 二.选择国内常用的源 #阿里源 deb ht ...

  8. ShowDoc上手

    ShowDoc是什么 每当接手一个他人开发好的模块或者项目,看着那些没有写注释的代码,我们都无比抓狂.文档呢?!文档呢?!Show me the doc !! 程序员都很希望别人能写技术文档,而自己却 ...

  9. Yii2几个要注意的小地方

    本人新手, 刚接触Yii, 记录下遇到的坑, 大神请绕道/ 1. //插入数据到数据库, 需要 new 一下,设置属性: $info = new BasicInfo(); $info -> se ...

  10. [转帖]web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全。

    web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全. https://www.cnblogs.com/1996V/p/7458377.html 感谢原作者写的内容 安全 ...