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> # ...
随机推荐
- 解决 linux [Fedora] 升级 导致VMware启动出现"before you can run vmware workstation, serveral modules must be complied and loaded into the runing kernel" 而无法卸载
解决: 开机启动 进入 升级之前的内核系统 然后 执行卸载 VMware 命令 # vmware-uninstall You have gotten this message because you ...
- poj 1176 Party Lamps
http://poj.org/problem?id=1176 Party Lamps Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- acm算法模板(4)
杂乱小模板 状态压缩dp小技巧 x&-x是取x的最后一个1的位置. x-=x&-x是去掉x的最后一个1. 读入外挂 int nxt_int(){// neg or pos cha ...
- [转]Web程序员必须知道的 Console 对象里的九个方法
一.显示信息的命令 01 1: <!DOCTYPE html> 02 2: <html> 03 3: <head> 04 4: <title&g ...
- Ruby与Python开发的环境IDE配置(附软件的百度云链接)
Ruby开发环境配置 1.Aptana_RadRails(提示功能不好,开发Ruby不推荐) 链接:http://pan.baidu.com/s/1i5q96K1 密码:yt04 2.Aptana S ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- MVC架构剖析--ASP.NET MVC图解(二)
- 161017、SQL必备知识点
经典SQL语句大全 基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql ser ...
- makefile 中 $@ $^ %< 使用【转】
转自:http://blog.csdn.net/kesaihao862/article/details/7332528 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将 ...
- M公司面试
1.技术面 跟日历相关的,根据你联系人的时间,确定可以安排活动的时间 2.final面 你的项目经历,挑战,解决办法: 判断两个长方形,是否有重叠部分: 你的人生规划[这个很多公司都会问]