51nod1363 最小公倍数之和
题目描述
给出一个n,求1-n这n个数,同n的最小公倍数的和。
例如:n = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30,6,加在一起 = 66。
由于结果很大,输出Mod 1000000007的结果。
输入
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 50000)
第2 - T + 1行:T个数A[i](A[i] <= 10^9)
输出
共T行,输出对应的最小公倍数之和
输入样例
3
5
6
9
输出样例
55
66
279
题解
&n\sum{\frac{i}{(i,n)}}\\
&=n\sum_{d|n}{\sum_{i=1}^n{\frac{i}{d}}}[(i,n)=d]\\
&=n\sum_{d|n}\sum_{i=1}^{\frac{n}{d}}i[(i,\frac{n}{d})=1]\\
&=n\sum_{d|n}φ(\frac{n}{d})\frac{n}{2d}\\
&=\frac{n}{2}\left(\sum_{d|n}φ(d)d+1\right)\\
\end{aligned}
\]
考虑\(\sum_{d|n}φ(d)d\)是个积性函数。
证明:
\[\begin{aligned}
&设x,y互质\\
&\sum_{d|n}φ(d)d=1*(φ·id)\\
&1*(φ(x)·x\timesφ(y)·y)=1*(φ(xy)·xy)
\end{aligned}
\]
则对于每个p,都有
\]
p的x次方也类似。根据唯一分解定理,我们可以得到下面的推论:
&设f(n)=\sum_{d|n}φ(d)d\\
&f(n)=f(p_1^{c_1})*f(p_2^{c_2})*...*f(p_k^{c_k})
\end{aligned}
\]
我们知道\(φ\)函数的一个性质:对于\(n=p^k\),有\(φ(p^k)=p^k-p^{k-1}\)。所以可以预处理质数,然后来分解质因数,对每个质因子算出\(f(p_i^{c_i})\),然后乘起来就好了。(这样复杂度是\(O(\frac{\sqrt{n}}{\log n})\)的,如果不先预处理质数就是\(O(\sqrt{n})\)的,会TLE)
这样子的话常数也是很小的,在51nod跑到了第一页(rk17)。
个人感觉这个做法好写好想啊...为什么网上都没这个做法的题解...都是好麻烦的考虑贡献
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline
namespace io {
#define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n')
#define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int
}
using namespace io;
using namespace std;
const ll mod = 1e9 + 7;
#define N 1000010
int T = read();
int p[N], cnt;
bool vis[N];
void init(int n) {
cnt = 0;
for(int i = 2; i <= n; ++i) {
if(!vis[i]) p[++cnt] = i;
for(int j = 1; j <= cnt && i * p[j] <= n; ++j) {
vis[i * p[j]] = 1;
if(i % p[j] == 0) break;
}
}
}
ll power(ll a, ll b) {
ll ans = 1;
while(b) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod; b >>= 1;
} return ans;
}
ll inv2 = power(2, mod-2);
ll solve(ll n) {
ll ans = 1;
for(int i = 1; p[i] * p[i] <= n && i <= cnt; ++i) {
if(n % p[i] == 0) {
ll now = 1, sum = 1;
while(n % p[i] == 0) {
n /= p[i];
now *= (ll)p[i];
sum += (ll)(now - now / p[i]) * now;
}
ans = ans * sum % mod;
}
}
if(n > 1) {ans = ans * (1 + n * (n - 1) % mod) % mod;}
return ans + 1ll;
}
int main() {
init(35000);
while(T--) {
ll n = read();
outn(solve(n)*(n*inv2%mod)%mod);
}
}
51nod1363 最小公倍数之和的更多相关文章
- 51nod-1363: 最小公倍数之和
[传送门:51nod-1363] 简要题意: 给出一个数n,求出1到n的数与n的最小公倍数的和 多组数据 题解: 理所当然推柿子 原题相当于求$\sum_{i=1}^{n}\frac{i*n}{gcd ...
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- 51nod 1238 最小公倍数之和 V3
51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...
- 51nod 1190 最小公倍数之和 V2
给出2个数a, b,求LCM(a,b) + LCM(a+1,b) + .. + LCM(b,b). 例如:a = 1, b = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30 ...
- 51nod 1363 最小公倍数之和 ——欧拉函数
给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30,6,加在一起 = 66. 由于结果很大,输出Mod 1000 ...
- 51Nod 最大公约数之和V1,V2,V3;最小公倍数之和V1,V2,V3
1040 最大公约数之和 给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6 1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 输入 1个数N ...
- BNU 12846 LCM Extreme 最小公倍数之和(线性欧拉筛选+递推)
LCM Extreme Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Orig ...
- 51 NOD 1238 最小公倍数之和 V3
原题链接 最近被51NOD的数论题各种刷……(NOI快到了我在干什么啊! 然后发现这题在网上找不到题解……那么既然A了就来骗一波访问量吧…… (然而并不怎么会用什么公式编辑器,写得丑也凑合着看吧…… ...
- 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛
题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...
随机推荐
- Delphi 的内存操作函数(2): 给数组指针分配内存
静态数组, 在声明时就分配好内存了, 譬如: var arr1: ..] of Char; arr2: ..] of Integer; begin ShowMessageFmt('数组大小 ...
- asp.net 使用rabbitmq事例
本例asp.net 使用rabbitmq需求背景:为了提升用户体验,用户点击下单按钮,后台先做一些简单必要的操作,返回给用户一个友好提示(比如提示处理中,或者订单状态为处理中),然后发通过发消息给队列 ...
- C#进度条简单应用
进度条表示文件复制的进度: 1.将进度条最大值设置为需要复制的文件总数 2.遍历文件时每复制一个文件之后,进度条+1 ;//文件总数 progressBar1.Value = progressBar1 ...
- PHP生成当前月份包括最近12个月内的月份
直接上代码: $time=array(); $currentTime = time(); $cyear = floor(date("Y",$currentTime)); $cMon ...
- python range的用法小题
题目(1)for i in range(10): print(i) 结果:123456789 题目(2) for lst in range(100): if lst % 7 == 0 and str( ...
- 好用的一些 git 命令
git stash 将已修改未提交的 改动保存起来 恢复用git stash pop gir revert 反转commit git rebase 更换基础分支 git grep -n 显示 ...
- 学习animate.css包含了一组炫酷、有趣、跨浏览器的动画
1.animate.css包含了一组炫酷.有趣.跨浏览器的动画,可以在你的项目中直接使用. 第一步:引入animate.css样式文件或者引入某些平台的CDN文件: <head> < ...
- Python selenium中注入并执行Javascript语句
众所周知,Python通常结合selenium模块来完成一些web的自动化测试以及RPA(Robotic Process Automation)工作.事实上,Selenium还可以支持插入js语句.执 ...
- JAVA获取汉字拼音首字母
package com.common.util; import java.io.UnsupportedEncodingException; /** * 取得给定汉字串的首字母串,即声母串 * Titl ...
- cocos2dx在win10系统上的VS2017运行时报错:丢失MSVCR110.dll
如题,运行环境为cocos2dx 3.14.1,win10系统,VS2017. 编译cocos2dx的cocos2d-x-3.14.1/build/cocos2d-Win32.sln已通过,不过运行时 ...