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 ...
随机推荐
- Newtonsoft.Json初探
1.序列化 VehicleModelSearchingModel model = new VehicleModelSearchingModel() { brandId = , modelIds=&qu ...
- bug汇总
bug 2018年8月23日 bug 1:散点图画不出来. plt.scatter(validation_examples["longitude"], validation_exa ...
- 在Linux系统中重现黑客帝国经典画面
我们需要一个叫cmatrix的小程序,下面写出步骤 1 :依赖环境 yum -y install gcc ncurses-devel 2 :下载程序 wget https://files.cnbl ...
- JQ之$.ajax()方法以及ajax跨域请求
AJAX(Asynchronous javascript AND xml :异步javascript和xml):是一种创建交互式网页应用的网页开发技术.AJAX可以在不重新加载整个页面的情况下与服务器 ...
- vue 配置多页面应用
前言: 本文基于vue 2.5.2, webpack 3.6.0(配置多页面原理类似,实现方法各有千秋,可根据需要进行定制化) vue 是单页面应用.但是在做大型项目时,单页面往往无法满足我们的需求, ...
- 【windows】【md5】查看文件的md5值
certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile filename SHA256 ...
- thinkphp5开发restful-api接口学习 笔记二
目录 第4节 为api项目搭建数据库 第5节 使用markdown书写接口文档 第6节(判断数据库中是否有此用户) 第7节 为项目配置URL 需求分析 配置主域名和二级域名 使用tp5路由进行URL解 ...
- Python中类的声明,使用,属性,实例属性,计算属性及继承,重写
Python中的类的定义以及使用: 类的定义: 定义类 在Python中,类的定义使用class关键字来实现 语法如下: class className: "类的注释" 类的实体 ...
- selenium+phantomjs爬取bilibili
selenium+phantomjs爬取bilibili 首先我们要下载phantomjs 你可以到 http://phantomjs.org/download.html 这里去下载 下载完之后解压到 ...
- QT入门学习笔记2:QT例程
转至:http://blog.51cto.com/9291927/2138876 Qt开发学习教程 一.Qt开发基础学习教程 本部分博客主要根据狄泰学院唐老师的<QT实验分析教程>创作,同 ...