spoj LCMSUM sigma(lcm(i,n));
Problem code: LCMSUM |
Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.
Input
The first line contains T the number of test cases. Each of the next T lines contain an integer n.
Output
Output T lines, one for each test case, containing the required sum.
Example
Sample Input :
3
1
2
5
Sample Output :
1
4
55
Constraints
1 <= T <= 300000
1 <= n <= 1000000
题意:sigma(lcm(i,n)) ; 1<=i<=n
思路:题意倒是很简单,有类似的题目sigma(gcd(i,n)) ;1<=i<=n;
一看就是果然是同类型的。
gcd(i,n)的题目 http://www.cnblogs.com/tom987690183/p/3247439.html
这个是求lcm()最小公倍数.
同样的思路,我们枚举gcd() = d 则在[ 1 , n ]中与n最大公约数为d的值有euler(n/d)个.
这些数的和为euler(n/d)*n/2; 我们知道lcm = x*n/(gcd(x,n)) = > x*n/d ;
因为与n gcd() = d的数字为x1,x2,x3....
根据lcm = x*n/(gcd(x,n)) 可以得到 (n/d)*(x1+x2+x3...) => [euler(n/d)*n/2]*(n*d);
这里需要注意的是当gcd(i,n) = n的时候,用euler(n)*n/2算的时候是不对的,就特判吧。
这样的思路,我们就可以试一下打欧拉表opl[ ],需要时间o(N);
然后对于n,要用sqrt(n)的时间。
T*sqrt(n)+o(N) = 3^8+10^6.果断超时。以为能ac,吼吼。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; const int maxn = 1e6+;
int opl[maxn];
void init()
{
for(int i=;i<maxn;i++) opl[i] = i;
for(int i=;i<maxn;i++)
{
if(opl[i]==i) opl[i] = i-;
else continue;
for(int j=i+i;j<maxn;j=j+i)
opl[j] = opl[j]/i*(i-);
}
}
int main()
{
int T;
LL n;
init();
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
LL sum = ;
for(LL i=;i*i<=n;i++)
{
if(n%i==)
{
if(i!=n)
sum = sum + (n/i)*(opl[n/i]*n/);
LL tmp = n/i;
if(tmp!=i && tmp!=n)
sum = sum + i*(opl[i]*n/);
}
}
printf("%lld\n",sum+n);
}
return ;
}
这样的话,是不行了。
原始相当于

也就能转化为(easy)

这样的话,我们就能单独的求出euler(a)*a/2;然后用它来筛选g[n]的值。
(x|n的意思代表 n是x的倍数)
我们想到在第一种方法里,我们用sqrt(n)来求它的因子的方法。
同理,逆向一下就好。
这样我们只需要o(N) 的时间对g[]进行筛选。总的预处理时间
3*o(N) = > 3*10^6;
完毕。
需要注意的是,我们没有把gcd() = n的情况包含进去,所以最后要+n。
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; const int maxn = 1e6+;
LL opl[maxn];
LL g[maxn];
void init()
{
for(int i=;i<maxn;i++) opl[i] = i;
//这种方法筛选素数,不用和以前一样要先刷素数,还开num[];
for(int i=;i<maxn;i++)
{
if(opl[i]==i)
opl[i] = i-;
else continue;
for(int j=i+i;j<maxn;j=j+i)
opl[j] = opl[j]/i*(i-);
}
for(int i=;i<maxn;i++){
opl[i] = opl[i]*i/;
g[i] = opl[i];
}
for(long long i=;i<=;i++) //这里的i 不能用int,肯定会超int的
{
for(long long j=i*i,k=i;j<maxn;j=j+i,k++)
if(i!=k) //不重复
g[j] = g[j] + opl[i]+opl[k];
else g[j] = g[j] + opl[i];
}
}
int main()
{
init();
int T;
LL n;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
printf("%lld\n",g[n]*n+n);
}
return ;
}
spoj LCMSUM sigma(lcm(i,n));的更多相关文章
- SPOJ LCMSUM - LCM Sum
题意是求: $\sum_{i = 1}^{n}lcm(i, n)$ $= \sum_{i = 1}^{n}\frac{ni}{gcd(i, n)}$ $= n\sum_{i = 1}^{n}\frac ...
- SPOJ LGLOVE 7488 LCM GCD Love (区间更新,预处理出LCM(1,2,...,n))
题目连接:http://www.spoj.com/problems/LGLOVE/ 题意:给出n个初始序列a[1],a[2],...,a[n],b[i]表示LCM(1,2,3,...,a[i]),即1 ...
- (转载)有关反演和gcd
tips : 积性函数 F (n) = Π F (piai ) 若F (n), G (n)是积性函数则 F (n) * G (n) Σd | n F (n) 是积性函数 n = Σd | n φ ( ...
- 51Nod 最大公约数之和V1,V2,V3;最小公倍数之和V1,V2,V3
1040 最大公约数之和 给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6 1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 输入 1个数N ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- bzoj 2693: jzptab 线性筛积性函数
2693: jzptab Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 444 Solved: 174[Submit][Status][Discus ...
- 【莫比乌斯反演】BZOJ2154 Crash的数字表格
Description 求sigma lcm(x,y),x<=n,y<=m.n,m<=1e7. Solution lcm没有什么直接做的好方法,用lcm=x*y/gcd转成gcd来做 ...
- 2011 Multi-University Training Contest 4 - Host by SDU
A.Color the Simple Cycle(polya计数+字符串匹配) 此题的难点在于确定置换的个数,由a[i+k]=a[i], e[i+k]=e[i]联想到KMP. 于是把原串和原串扩大两倍 ...
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...
随机推荐
- iOS 检查版本号的代码
- (void)checkNewVersion{ if ([@"appStore" isEqualToString:CHANNEL]) { AFHTTPRequestOperati ...
- Python高频技巧总结[基础篇]
0. 概要说明 python应用最多的场景还是web快速开发.爬虫.自动化运维:简单网站.自动Fuzz脚本.收发邮件脚本.简单验证码识别脚本. 爬虫在开发过程中也有很多复用的过程,这里总结一下,以后也 ...
- UML: 序列图
摘自http://www.umlonline.org/school/thread-37-1-1.html 大家都进过餐馆吃饭吧?你是如何和餐厅服务员“眉来眼去”的呢?回忆一下从你进餐馆开始到你离开餐馆 ...
- UVA 10498 Happiness(线性规划-单纯形)
Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...
- sql 表连接 join
inner join 和 join 的 区别 inner join 是内连接 ,查询出两边 都有的数据 join 是交叉 连接, 假设集合A={a, b},集合B={0, 1, 2},则两个集 ...
- struts拦截器
struts中的拦截器相当于过滤器的作用 一在struts.xml中配置拦截器或拦截器栈 <interceptors>!--全部的拦截器 <interceptor name=&quo ...
- web工程常见部署方式总结
作为一个web测试工程师,对测试所属的平台架构,项目部署情况应该是有所了解的,下面在此基础上总结下web项目在各种场景下常用的部署方式: 第一种方法: 开发常用部署方法,直接在myeclipse里部署 ...
- html5,表格
<table border="1"><caption>表格的实例</caption><tr><td>单元格</td ...
- 典型的检查对float精度理解的代码
-rand()%); vy = ); vz = ); pList_particle[i].m_velocity = Vector3(vx,vy,vz); ... 1,3行代码的vx和vz的值域可以通过 ...
- OpenStack 密码注入
现状 实例可以创建,可以使用vnc,可以ssh,但是就是密码要使用默认tima123,要修改密码必须进入虚拟机.实际场景中如果用户将密码修改后忘记,需要重置密码则我们作为管理员也没有办法.这在实际需求 ...