BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
2024-08-31 02:42:55
BZOJ 2226 [Spoj 5971] LCMSum
这道题和上一道题十分类似。
\[\begin{align*}
\sum_{i = 1}^{n}\operatorname{LCM}(i, n) &= \sum_{i = 1}^{n}\frac{i \times n}{\operatorname{gcd}(i, n)}\\
&= n \times \sum_{i = 1}^{n}\frac{i}{\operatorname{gcd}(i, n)}
\end{align*}\]
\sum_{i = 1}^{n}\operatorname{LCM}(i, n) &= \sum_{i = 1}^{n}\frac{i \times n}{\operatorname{gcd}(i, n)}\\
&= n \times \sum_{i = 1}^{n}\frac{i}{\operatorname{gcd}(i, n)}
\end{align*}\]
设\(d = \operatorname{gcd}(i, n)\),则\(d | n\)且\(\operatorname{gcd}(\frac{i}{d}, \frac{n}{d}) = 1\)。
则每个\(n\)的因数\(d\)的贡献是小于等于\(d\)的所有数(\(\frac{i}{d}\))之和。而这个值等于\(\frac{\phi(d) * d}{2}\)。
所以答案就是:
\[\sum_{d | n}\frac{\phi(d) * d}{2}
\]
\]
注意这道题卡常卡得非常难受,所以能预处理的都预处理吧。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c > '9' || c < '0')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
const int N = 1000000;
int T, n, lst[N + 5], cnt;
bool notprime[N + 5];
ll ans, phi[N + 5];
void init(){
phi[1] = 1;
for(int i = 2; i <= N; i++){
if(!notprime[i]) lst[++cnt] = i, phi[i] = i - 1;
for(int j = 1; j <= cnt && lst[j] * i <= N; j++){
notprime[lst[j] * i] = 1;
if(i % lst[j] == 0){
phi[lst[j] * i] = lst[j] * phi[i];
break;
}
phi[i * lst[j]] = phi[i] * (lst[j] - 1);
}
}
for(int i = 2; i <= N; i++)
phi[i] = phi[i] * i / 2;
}
int main(){
init();
read(T);
while(T--){
read(n);
ans = 0;
for(int i = 1; i * i <= n; i++)
if(n % i == 0){
ans += phi[i];
if(i * i < n) ans += phi[n / i];
}
write(ans * n), enter;
}
return 0;
}
最新文章
- ajax 多级联动 下拉框 Demo
- 2016第七季极客大挑战Writeup
- Python_Day12_python mysql and ORM and redis
- 向架构师进军--->;如何编写软件架构文档
- 判断图片的类型(图片是data类型 )
- python 迭代器和生成器
- 【BZOJ】2152: 聪聪可可(点分治)
- Java Garbage Collection/垃圾收集 策略查看
- DataTime.Now.Ticks的应用
- maven依赖传递关系
- AlarmReceiver 与IntentService的使用
- 使用gson(一)
- SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
- laravel5 事务回滚
- C#调用Oracle的存储过程时,连接字符串需要配置PLSQLRSet=1
- vue中动态样式不起作用? scoped了解一下
- ubuntu代理设置
- Unable to cast object of type &#39;System.Int32&#39; to type &#39;System.Array&#39;.
- 把自己的代码发布到npm(npm publish)
- .Net Core MVC实现自己的AllowAnonymous