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. Android中集成QQ登陆和QQ好友分享及QQ空间分享

    extends : http://blog.csdn.net/arjinmc/article/details/38439957 相关官方文档及下载地址: 如果只用分享和登陆,用lite包就可以,体积小 ...

  2. Swift - 多线程GCD详解

    //  GCD详解 //  目录: //  1. 创建GCD队列(最常用) //  2. 自定义创建队列 //  3. 使用多线程实现延迟加载 //  4. 使用多线程实现重复(循环) //  5. ...

  3. javascript飞机大战-----008积分

    /* 创建敌机: */ function Enemy(blood,speed,imgs,scroe){ //敌机left this.left = 0; //敌机top this.top = 0; // ...

  4. PHP 的“魔术常量”

    名称 说明 __LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路 ...

  5. linux memory dump--http://www.forensicswiki.org/wiki/Tools:Memory_Imaging

    Linux provides two virtual devices for this purpose, '/dev/mem' and '/dev/kmem', though many distrib ...

  6. Jury Compromise---poj1015(动态规划,dp,)

    题目链接:http://poj.org/problem?id=1015 大致题意: 在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n 个人作为陪审团的候 ...

  7. pandas 从txt读取DataFrame&DataFrame格式化保存到txt

    前提 首先保证你txt里的文本内容是有规律可循的(例如,列与列之间通过“\t”.“,”等指定的可识别分隔符分隔): 例如我需要读取的数据,(\t)分隔: (此文件内容是直接以DataFrame格式化写 ...

  8. Linux eventfd分析

    2017-07-20 eventfd在linux中是一个较新的进程通信方式,和信号量等不同的是event不仅可以用于进程间的通信,还可以用户内核发信号给用户层的进程.eventfd在virtIO后端驱 ...

  9. boost.sha1

    #include <boost/uuid/sha1.hpp> #include <iostream> /* @brief SHA1摘要算法:一种很重要的密码学算法,可将任意长度 ...

  10. day08:软件系统的体系结构&Tomcat详解&Web应用&http协议

        day08 软件系统体系结构     常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端 ...