题解:枚举gcd,算每个gcd对答案的贡献,贡献用到欧拉函数的一个结论

最后用nlogn预处理一下,O(1)出答案

把long long 打成int 竟然没看出来QWQ

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=1000009;
const int u=1000000;
typedef long long Lint; int T;
int n; int cntprime;
int prime[maxn];
int vis[maxn];
Lint phi[maxn];
Lint f[maxn];
void Lineshake(){
vis[1]=1;phi[1]=1;
for(int i=2;i<=u;++i){
if(!vis[i]){
prime[++cntprime]=i;
phi[i]=i-1;
}
for(int j=1;(j<=cntprime)&&(i*prime[j]<=u);++j){
vis[i*prime[j]]=1;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
phi[1]=1;
for(int i=2;i<=u;++i)phi[i]=phi[i]*1LL*i/2;
} void minit(){
cntprime=0;
memset(vis,0,sizeof(vis));
} int main(){
scanf("%d",&T);
minit();
Lineshake();
for(int d=1;d<=u;++d){
for(int k=1;k*d<=u;++k){
f[d*k]+=phi[d]*d*k;
}
}
while(T--){
scanf("%d",&n);
printf("%lld\n",f[n]);
}
return 0;
}

  

BZOJ 2226 [Spoj 5971] LCMSum的更多相关文章

  1. BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论

    BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...

  2. bzoj 2226: [Spoj 5971] LCMSum 数论

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 578  Solved: 259[Submit][St ...

  3. BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...

  4. BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常

    Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...

  5. BZOJ2226: [Spoj 5971] LCMSum

    题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...

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

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

  7. 【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 ...

  8. bzoj 2226 LCMSum 欧拉函数

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1123  Solved: 492[Submit][S ...

  9. 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster

    目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...

随机推荐

  1. P1091合唱队形(LIS问题)

    题目描述(题目链接:https://www.luogu.org/problem/P1091) NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱 ...

  2. Day2-G-Sticks-POJ1011

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  3. Docker 安装(centos7下)

    下面链接为官方的安装方法(官方的是最好的): https://docs.docker.com/install/linux/docker-ce/centos/#upgrade-docker-after- ...

  4. IOS 导航栏颜色 标题

    修改导航栏颜 #define COLOR_TOMATO    [UIColor colorWithRed:255/255.0f green:99/255.0f blue:71/255.0f alpha ...

  5. sklearn学习笔记(1)--make_blobs函数及相应参数简介

    make_blobs方法: sklearn.datasets.make_blobs(n_samples=100,n_features=2,centers=3, cluster_std=1.0,cent ...

  6. delphi窗体按钮灰化禁用

    1.使最小化按钮变灰:setwindowlong(handle,gwl_style,getwindowlong(handle,gwl_style)   and   not   ws_minimizeb ...

  7. javac导出参考文档报错 错误: 编码GBK的不可映射字符”

  8. 【转】WdatePicker日历控件使用方法

    转                自:   https://www.cnblogs.com/yuhanzhong/archive/2011/08/10/2133276.html WdatePicke官 ...

  9. Vue 中使用Ajax请求

    Vue 项目中常用的 2 个 ajax 库 (一)vue-resource vue 插件, 非官方库,vue1.x 使用广泛 vue-resource 的使用 在线文档   https://githu ...

  10. python2学习------基础语法2(函数)

    1.函数 # 无参数函数 def loopTest2(): a=1; while a<40: print a; a=a+1; if a==35: continue; else: print 'o ...