2226: [Spoj 5971] LCMSum

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 1123  Solved: 492
[Submit][Status][Discuss]

Description

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.

Sample Input

3
1
2
5

Sample Output

1
4
55

HINT

Constraints

1 <= T <= 300000
1 <= n <= 1000000

思路:时间复杂度T*sqrt(n);

   小于n并且与n互质的数为phi(k)*k/2;

   1的情况是特例;全部long long会超时。。。卡死我了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define esp 0.00000000001
const int MAXN=;
int prime[MAXN],cnt;
bool vis[MAXN];
int phi[MAXN];
void Prime(int n)
{
phi[]=;
for(int i=;i<n;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
phi[i]=i-;
}
for(int j=;j<cnt&&i*prime[j]<n;j++)
{
int k=i*prime[j];
vis[k]=;
if(i%prime[j]==)
{
phi[k]=phi[i]*prime[j];
break;
}
else
phi[k]=phi[i]*(prime[j]-);
}
}
}
ll phii(ll n)
{
return (ll)phi[n]*n/;
}
int main()
{
int x,y,z,i,t;
Prime();
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
ll ans=;
ll gg=sqrt(x);
for(i=;i<=gg;i++)
{
if(x%i==)
ans+=phii(x/i),ans+=phii(i);
}
if(gg*gg==x)
ans-=phii(gg);
printf("%lld\n",(ans+)*x);
}
return ;
}

bzoj 2226 LCMSum 欧拉函数的更多相关文章

  1. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  2. 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数

    题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...

  3. BZOJ 2818 Gcd(欧拉函数+质数筛选)

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 9108  Solved: 4066 [Submit][Status][Discu ...

  4. BZOJ2226:LCMSum(欧拉函数)

    Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...

  5. bzoj 2705 数学 欧拉函数

    首先因为N很大,我们几乎不能筛任何东西 那么考虑设s(p)为 gcd(i,n)=p 的个数,显然p|n的时候才有意义 因为i与n的gcd肯定是n的因数,所以那么可得ans=Σ(p*s(p)) 那么对于 ...

  6. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  7. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

  8. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  9. bzoj 2190 [SDOI2008]仪仗队(欧拉函数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2190 [题意] n*n的正方形,在(0,0)格点可以看到的格子数目. [思路] 预处理 ...

随机推荐

  1. 【BZOJ4429】[Nwerc2015] Elementary Math小学数学 最大流

    [BZOJ4429][Nwerc2015] Elementary Math小学数学 Description Ellen给她的学生教小学数学.期末考试已经来临了.考试有n个题目,每一个题目学生们都要对一 ...

  2. 批量远程执行linux服务器程序--基于pxpect(多进程、记日志版)

    #!/usr/bin/python '''Created on 2015-06-09@author: Administrator''' import pexpect import os,sys fro ...

  3. thinkCMF的使用!

    第一次使用 thinkCMF:在此记录下使用的过程! 后台登录: http://thinkcmf.fyz.com/public/admin 前台:控制器 前台控制器的模板:

  4. JAVAWEB Filter使用

    Filter学习 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator ...

  5. nginx:服务器集群

    一.Nginx的事件处理机制 对于一个基本的web服务器来说,事件通常有三种类型,网络事件.信号.定时器. 首先看一个请求的基本过程:建立连接---接收数据---发送数据 . 再次看系统底层的操作 : ...

  6. mysql备份的4种方式

    mysql备份的4种方式 转载自:https://www.cnblogs.com/SQL888/p/5751631.html 总结: 备份方法 备份速度 恢复速度 便捷性 功能 一般用于 cp 快 快 ...

  7. Windows mysql默认字符集修改

    一.通过MySQL命令行修改: set character_set_client=utf8; set character_set_connection=utf8; set character_set_ ...

  8. spark2.1.1创建Pipeline

    Pipeline 为流程,是Spark创建机器学习的一个流程控制的类 下面直接贴出创建的代码,以及整个流程 第一种: import org.apache.spark.ml.{Pipeline, Pip ...

  9. Windows7系统运行hadoop报Failed to locate the winutils binary in the hadoop binary path错误

    程序运行的过程中,报Failed to locate the winutils binary in the hadoop binary path  Java.io.IOException: Could ...

  10. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...