BNU 12846 LCM Extreme 最小公倍数之和(线性欧拉筛选+递推)
LCM Extreme
64-bit integer IO format: %lld Java class name: Main
Find the result of the following code:
unsigned long long allPairLcm(int n){
unsigned long long res = 0;
for( int i = 1; i<=n;i++)
for(int j=i+1;j<=n;j++)
res += lcm(i, j);// lcm means least common multiple
return res;
}
A straight forward implementation of the code may time out.
Input
Input starts with an integer T (≤ 25000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 5*106
).
Output
For each case, print the case number and the value returned by the function 'allPairLcm(n)'. As the
result can be large, we want the result modulo 2
64
.
Sample Input Output for Sample Input
4
2
10
13
100000
Case 1: 2
Case 2: 1036
Case 3: 3111
Case 4: 9134672774499923824
/*
题目大意:求lcm(1,2)+lcm(1,3)+lcm(2,3)+....+lcm(1,n)+....+lcm(n-2,n)+lcm(n-1,n)
设sum(n)为sum(lcm(i,j))(1<=i<j<=n)之间最小公倍数的和,f(n)为sum(i*n/gcd(i,n))(1<=i<n)
那么sum(n)=sum(n-1)+f(n)。可以用线性欧拉筛选+递推来做。
*/
#include <iostream>
#include <cstdio>
#include <cstring> typedef unsigned long long LL;
const int maxn=;
LL phi[maxn],sum[maxn],f[maxn]; void Euler()
{
memset(phi,,sizeof(phi));
int i,j;phi[]=;
for(i=;i<maxn;i++)
{
if(phi[i]) continue;
for(j=i;j<maxn;j+=i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
for(i=;i<maxn;i++) phi[i]=phi[i]*i/;//与i互质的数之和
} void init()
{
Euler();
memset(sum,,sizeof(sum));
memset(f,,sizeof(f));
int i,j;sum[]=f[]=;
for(i=;i<maxn;i++)
{
f[i]+=phi[i]*i;//与i互质的数之间的lcm之和
for(j=*i;j<maxn;j+=i)
f[j]+=phi[i]*j;//gcd(x,j)=i的sum(lcm(x,j))
sum[i]=sum[i-]+f[i];
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
int t,icase=,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("Case %d: %llu\n",++icase,sum[n]);
}
return ;
}
BNU 12846 LCM Extreme 最小公倍数之和(线性欧拉筛选+递推)的更多相关文章
- LightOJ 1375 - LCM Extreme 莫比乌斯反演或欧拉扩展
题意:给出n [1,3*1e6] 求 并模2^64. 思路:先手写出算式 观察发现可以化成 那么关键在于如何求得i为1~n的lcm(i,n)之和.可以知道lcm(a,b)为ab/gcd(a,b) 变换 ...
- 【51Nod 1363】最小公倍数之和(欧拉函数)
题面 传送门 题解 拿到式子的第一步就是推倒 \[ \begin{align} \sum_{i=1}^nlcm(n,i) &=\sum_{i=1}^n\frac{in}{\gcd(i,n)}\ ...
- UVA 11426 (欧拉函数&&递推)
题意:给你一个数N,求N以内和N的最大公约数的和 解题思路: 一开始直接想暴力做,4000000的数据量肯定超时.之后学习了一些新的操作. 题目中所要我们求的是N内gcd之和,设s[n]=s[n-1] ...
- POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】
一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), ...
- 51nod 1040 最大公约数之和(欧拉函数)
1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出一个n,求1-n这n个数,同n的最大公约数的和.比如: ...
- BZOJ 2818 Gcd 线性欧拉
题意:链接 方法:线性欧拉 解析: 首先列一下表达式 gcd(x,y)=z(z是素数而且x,y<=n). 然后我们能够得到什么呢? gcd(x/z,y/z)=1; 最好还是令y>=x 则能 ...
- uva 11426 线性欧拉函数筛选+递推
Problem J GCD Extreme (II) Input: Standard Input Output: Standard Output Given the value of N, you w ...
- 51nod1040 最大公约数之和,欧拉函数或积性函数
1040 最大公约数之和 给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6时,1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 看起来很简单 ...
- POJ2909_Goldbach's Conjecture(线性欧拉筛)
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one p ...
随机推荐
- C++链表简单的应用
学生管理系统,输入学生的姓名和学号,然后再输出: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <stdlib ...
- 课外作业1:将一个double类型的小数,按照四舍五入保留两位小数
package come.one01; public class One02 { public static void main(String[] args) { double numa = 3.14 ...
- javaweb基础(12)_session详解
一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...
- 实现HTTP文件下载
[原文:http://www.jb51.net/article/89958.htm] HTTP实现文件下载时,只要在服务器设置好相关响应头,并使用二进制传输文件数据即可,而客户端(浏览器)会根据响应头 ...
- SizeClass介绍
随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个新特性将颠覆包括iOS7及之前版本的UI布局方式,这个新特性就是Size Class.Size Class配合Auto Layout可以解决 ...
- javascript (六)DOM
学习后的总结: DOM:document object model 关于DOM的简介:http://www.w3school.com.cn/htmldom/dom_intro.asp 本文说的是HTM ...
- SwaggerUI日常使用
最近公司项目集成springFox,记录一些swaggerUI日常使用,包括数组,文件,默认值,允许值,参数/结果类注解,响应码..等等. 一.参数注解: 单参数:@ApiImplicitParam ...
- python3 简单服务器监控,自动发送邮件
import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimpo ...
- IDEA入门学习笔记1:资料收集
IDEA2018软件下载 :https://mp.weixin.qq.com/s?__biz=MzIwMjE1MjMyMw==&mid=2650200056&idx=1&sn= ...
- 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装
参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...