【51nod】1227 平均最小公倍数
题解
这个故事告诉们数论函数不要往分式上跑,你推不出来
好久没推式子了这么明显的转化我都忘了= =
首先\(A(n) = \frac{1}{n} \sum_{i = 1}^{n} \frac{i * n}{gcd(i,n)}\)
然后显然可以把n消掉
\(A(n) = \sum_{i = 1}^{n} \frac{i}{gcd(i,n)}\)
改为枚举约数
\(A(n) = \sum_{d = 1}^{n} \frac{1}{d}\sum_{i = 1}^{n} i [gcd(i,n) == d]\)
\(A(n) = \sum_{d | n} \sum_{i = 1}^{\frac{n}{d}} i [gcd(i,\frac{n}{d}) == 1]\)
有个欧拉函数的性质是,小于这个数的且与这个数互质的数的和是
\(\frac{n \phi(n) + [n = 1]}{2}\) 挺好理解的,因为一个与n互质的数p,n - p也和n互质
\(\frac{n \phi(n) + [n = 1]}{2} = \sum_{i = 1}^{n} i [gcd(i,n) == 1]\)
\(A(n) = \frac{1}{2} (\sum_{d | n} \frac{n}{d} \phi(\frac{n}{d}) + 1)\)
\(F(n) = \sum_{i = 1}^{n} A(i)\)
\(F(n) = \frac{1}{2} (\sum_{i = 1}^{n} \sum_{d | i} \frac{i}{d} \phi(\frac{i}{d}) + n)\)
\(F(n) = \frac{1}{2} (\sum_{i = 1}^{n} \sum_{d | i} d \phi(d) + n)\)
\(F(n) = \frac{1}{2} (\sum_{i = 1}^{n} \sum_{d = 1}^{\frac{n}{i}} d \phi(d) + n)\)
我们发现这个东西可以构造卷积啊
\(\sum_{d = 1}^{n} d \phi(d)\)
卷上一个\(Id(x) = x^{2}\)
那么我们就有
\(\sum_{i = 1}^{n} i^2 = \sum_{i = 1}^{n} \sum_{d | i}d \phi(d) \frac{i}{d}\)
\(\sum_{i = 1}^{n} i^2 = \sum_{k = 1}^{n} k \sum_{d = 1}^{\frac{n}{k}}d \phi(d)\)
那么就有
\(S(n) = \frac{n(n +1)(2n + 1)}{6} - \sum_{i = 2}^{n} i S(\lfloor \frac{n}{i} \rfloor)\)
然后用数论分块求\(F(n)\)即可
题解
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
//#define ivorysi
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mo 974711
#define MAXN 1000000
#define RG register
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
const int MOD = 1000000007;
struct node {
int x,v,next;
}E[100006];
int head[mo + 5],sumE,Inv2,Inv6;
int prime[MAXN + 5],tot,S[MAXN + 5],phi[MAXN + 5];
bool nonprime[MAXN + 5];
int inc(int a,int b) {
a = a + b;
if(a >= MOD) a -= MOD;
return a;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void add(int u,int x,int v) {
E[++sumE].x = x;E[sumE].v = v;E[sumE].next = head[u];
head[u] = sumE;
}
void Insert(int x,int v) {
add(x % mo,x,v);
}
int Query(int x) {
int u = x % mo;
for(int i = head[u] ; i ; i = E[i].next) {
if(E[i].x == x) return E[i].v;
}
return -1;
}
int f(int x) {
if(x <= MAXN) return S[x];
int c = Query(x);
if(c != -1) return c;
int res = 0;
for(int i = 2 ; i <= x ; ++i) {
int r = x / (x / i);
res = inc(res,1LL * (r - i + 1) * (r + i) / 2 % MOD * f(x / i) % MOD);
i = r;
}
res = inc(1LL * x * (x + 1) % MOD * (2 * x + 1) % MOD * Inv6 % MOD,MOD - res);
Insert(x,res);
return res;
}
int calc(int x) {
int res = 0;
for(int i = 1 ; i <= x ; ++i) {
int r = x / (x / i);
res = inc(res,mul(r - i + 1,f(x / i)));
i = r;
}
res = inc(res,x);
res = mul(res,Inv2);
return res;
}
void Solve() {
phi[1] = 1;S[1] = 1;
for(int i = 2 ; i <= MAXN ; ++i) {
if(!nonprime[i]) {
prime[++tot] = i;
phi[i] = i - 1;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > MAXN / i) break;
nonprime[prime[j] * i] = 1;
if(i % prime[j] == 0) {phi[i * prime[j]] = phi[i] * prime[j];break;}
else phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
S[i] = inc(S[i - 1],mul(phi[i],i));
}
Inv2 = (MOD + 1) / 2;
Inv6 = 1LL * Inv2 * (MOD + 1) / 3 % MOD;
int a,b;
read(a);read(b);
out(inc(calc(b),MOD - calc(a - 1)));enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
【51nod】1227 平均最小公倍数的更多相关文章
- 51NOD 1227 平均最小公倍数 [杜教筛]
1227 平均最小公倍数 题意:求\(\frac{1}{n} \sum_{i=1}^n lcm(n,i)\) 和的弱化版? \[ ans = \frac{1}{2}((\sum_{i=1}^n \su ...
- 51nod 1227 平均最小公倍数【欧拉函数+杜教筛】
以后这种题能用phi的就不要用mu-mu往往会带着个ln然后被卡常致死 把题目要求转换为前缀和相减的形式,写出来大概是要求这样一个式子: \[ \sum_{i=1}^{n}\sum_{j=1}^{i} ...
- 51 nod 1227 平均最小公倍数
原题链接 Lcm(a,b)表示a和b的最小公倍数,A(n)表示Lcm(n,i)的平均数(1 <= i <= n), 例如:A(4) = (Lcm(1,4) + Lcm(2,4) + Lcm ...
- 51NOD 1227:平均最小公倍数——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1227 懒得打公式了,看这位的吧:https://blog.csdn.ne ...
- 【51Nod 1222】最小公倍数计数
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1222 求\([a,b]\)中的个数转化为求\([1,b]\)中的个数减去 ...
- 【51nod】1222 最小公倍数计数 莫比乌斯反演+组合计数
[题意]给定a和b,求满足a<=lcm(x,y)<=b && x<y的数对(x,y)个数.a,b<=10^11. [算法]莫比乌斯反演+组合计数 [题解]★具体 ...
- 【51Nod 1238】最小公倍数之和 V3
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1238 设\(A(n)=\sum\limits_{i=1}^n\frac{ ...
- 【51Nod 1190】最小公倍数之和 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1190 \[ \begin{aligned} &\sum_{i=a ...
- 【51Nod 1363】最小公倍数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1363 \[ \begin{aligned} &\sum_{i=1 ...
随机推荐
- 04-树6. Huffman Codes--优先队列(堆)在哈夫曼树与哈夫曼编码上的应用
题目来源:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%916 In 1953, David A. Huffman published his pap ...
- php中foreach使用引用的陷阱
有时候我们为了在循环过程中改变数组项的值,在foreach的时候变量入口可以加个&符合, 表示,循环过程中使用数组中原来的值,而不是一个复制的值,如 foreach ($array as &a ...
- Land of Farms HDU - 5556 二分图匹配
Farmer John and his brothers have found a new land. They are so excited and decide to build new farm ...
- bzoj 2530 [Poi2011]Party 构造
2530: [Poi2011]Party Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 364 Solved: ...
- Linux常用网络工具:路由扫描之traceroute
之前两篇<Linux常用网络工具:fping主机扫描>和<Linux常用网络工具:hping高级主机扫描>都是关于主机扫描的,本篇介绍Linux下常用的路由扫描工具tracer ...
- Turkey HSD检验法/W法
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- MingW和MSVC默认的编码方式不一样
同一份源代码,源文件编码格式为UTF-8: string tmp = "我"; ;i<tmp.size();++i) { printf("%0x ",tm ...
- 2017北京国庆刷题Day6 morning
期望得分:100+100+20=220 实际得分:100+100+20=220 模拟栈 #include<cstdio> #include<cstring> using nam ...
- 分块+deque维护 Codeforces Round #260 (Div. 1) D. Serega and Fun
D. Serega and Fun time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
- java中集合去重1
//TreeMap 处理 处理后 得到的值 按拼音排序 TreeMap map = new TreeMap(new MaterialComparator()); map.put("东莞&qu ...