【LG3768】简单的数学题

题面

求

\[(\sum_{i=1}^n\sum_{j=1}^nij\text{gcd}(i,j))\text{mod}p
\]

其中\(n\leq 10^{10},5\times 10^8\leq p \leq 1.1*10^9\)。

题解

推柿子:

\[\sum_{i=1}^n\sum_{j=1}^nij\text{gcd}(i,j)\\
=\sum_{d=1}d\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac nd\rfloor}ijd^2[\gcd(i,j)==1]\\
=\sum_{d=1}d^3\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac nd\rfloor}ij[\gcd(i,j)==1]\\
\]

令

\[f(d')=\sum_{i=1}^{n'}\sum_{j=1}^{n'}ij[\gcd(i,j)==d']\\
g(d')=\sum_{d'\mid x}f(x)
\]

则

\[g(d')=\sum_{d'\mid x}\sum_{i=1}^{n'}\sum_{j=1}^{n'}ij[\gcd(i,j)==x]\\
=\sum_{i=1}^{n'}\sum_{j=1}^{n'}ij[d'|\gcd(i,j)]\\
=\sum_{i=1}^{\lfloor\frac {n'}{d'}\rfloor}\sum_{j=1}^{\lfloor\frac {n'}{d'}\rfloor}ijd'^2[1|\gcd(i,j)]\\
=S(\lfloor\frac {n'}{d'}\rfloor)^2d'^2
\]

其中\(S(x)=\sum_{i=1}^x i\)

那么

\[f(d')=\sum_{d'\mid x}S(\lfloor\frac {n'}x\rfloor)^2x^2\mu (x)\\
f(1)=\sum_{x=1}^{n'}S(\lfloor\frac {n'}x\rfloor)^2x^2\mu (x)
\]

代回去

\[\sum_{d=1}^nd^3\sum_{x=1}^{\lfloor\frac nd\rfloor}S(\lfloor\frac {n}{xd}\rfloor)^2x^2\mu (x)
\]

令\(Q=xd\),

\[\sum_{d=1}^nd^3\sum_{x=1}^{\lfloor\frac nd\rfloor}S(\lfloor\frac {n}{Q}\rfloor)^2x^2\mu (x)\\
=\sum_{Q=1}^nS(\lfloor\frac {n}{Q}\rfloor)\sum_{d\mid Q}d^3(\frac Qd)^2\mu (\frac Qd)\\
=\sum_{Q=1}^nS(\lfloor\frac {n}{Q}\rfloor)^2\sum_{d\mid Q}dQ^2\mu (\frac Qd)\\
=\sum_{Q=1}^nS(\lfloor\frac {n}{Q}\rfloor)^2Q^2\varphi (Q)
\]

现在我们如何求\(Q^2\varphi (Q)\)的前缀和呢?

令\(f=\text{id}^{2}\cdot \varphi\),令\(g=\text{id}^2\cdot 1\),

那么\(f*g=\text{id}^3,g=\text{id}^2\)。

就做完了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int MAX = 1e7, MAX_N = 1e7 + 5;
bool nprime[MAX_N];
int prime[MAX_N], cnt, phi[MAX_N], f[MAX_N];
long long N;
int Mod;
int fpow(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res = 1ll * res * x % Mod;
x = 1ll * x * x % Mod;
y >>= 1;
}
return res;
}
void sieve() {
phi[1] = 1;
for (int i = 2; i <= MAX; i++) {
if (!nprime[i]) prime[++cnt] = i, phi[i] = i - 1;
for (int j = 1; j <= cnt && prime[j] * i <= MAX; j++) {
nprime[prime[j] * i] = 1;
if (i % prime[j] == 0) { phi[i * prime[j]] = 1ll * phi[i] * prime[j] % Mod; break; }
phi[i * prime[j]] = 1ll * phi[i] * phi[prime[j]] % Mod;
}
}
for (int i = 1; i <= MAX; i++) f[i] = (f[i - 1] + 1ll * phi[i] * i % Mod * i % Mod) % Mod;
}
int inv2, inv6;
int sqr(int n) { return 1ll * n * n % Mod; }
int S(long long n) { return n %= Mod, n % Mod * (n + 1) % Mod * inv2 % Mod; }
int g(long long n) { return n %= Mod, n % Mod * (n + 1) % Mod * (2 * n + 1) % Mod * inv6 % Mod; }
map<long long, int> mp;
int get_f(long long n) {
if (n <= MAX) return f[n];
if (mp.find(n) != mp.end()) return mp[n];
int res = sqr(S(n));
for (long long l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
res = (res - 1ll * (g(r) - g(l - 1) + Mod) % Mod * get_f(n / l) % Mod + Mod) % Mod;
}
return mp[n] = res;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
cin >> Mod >> N;
sieve();
inv2 = fpow(2, Mod - 2), inv6 = fpow(6, Mod - 2);
int ans = 0;
for (long long l = 1, r; l <= N; l = r + 1) {
r = N / (N / l);
ans = (ans + 1ll * sqr(S(N / l)) * ((get_f(r) - get_f(l - 1) + Mod) % Mod) % Mod) % Mod;
}
printf("%d\n", ans);
return 0;
}

【LG3768】简单的数学题的更多相关文章

  1. LG3768 简单的数学题

    P3768 简单的数学题 题目描述 输入一个整数n和一个整数p,你需要求出$(\sum_{i=1}^n\sum_{j=1}^n ijgcd(i,j))~mod~p$,其中gcd(a,b)表示a与b的最 ...

  2. 【数学】HPU--1037 一个简单的数学题

    1037: 一个简单的数学题 [数学] 时间限制: 1 Sec 内存限制: 128 MB提交: 259 解决: 41 统计 题目描述 小明想要知道$a^b$的值,但是这个值会非常的大. 所以退而求其次 ...

  3. 【Luogu3768】简单的数学题(莫比乌斯反演,杜教筛)

    [Luogu3768]简单的数学题(莫比乌斯反演,杜教筛) 题面 洛谷 \[求\sum_{i=1}^n\sum_{j=1}^nijgcd(i,j)\] $ n<=10^9$ 题解 很明显的把\( ...

  4. luoguP3768 简单的数学题

    题目链接 luoguP3768 简单的数学题 题解 上面那个式子的最后一步,需要定理 用数学归纳法证明 \(S1=1^3=1^2\) \(S2=1^3+2^3=9=3^2=(1+2)^2\) \(S3 ...

  5. 洛谷 P3768 简单的数学题 解题报告

    P3768 简单的数学题 题目描述 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数\(n\)和一个整数\(p,\)你需要求出\((\sum_{i=1}^n\sum_{j=1}^n ijgc ...

  6. loj#6229 这是一道简单的数学题

    \(\color{#0066ff}{ 题目描述 }\) 这是一道非常简单的数学题. 最近 LzyRapxLzyRapx 正在看 mathematics for computer science 这本书 ...

  7. 「洛谷P3768」简单的数学题 莫比乌斯反演+杜教筛

    题目链接 简单的数学题 题目描述 输入一个整数n和一个整数p,你需要求出 \[\sum_{i=1}^n\sum_{j=1}^n (i\cdot j\cdot gcd(i,j))\ mod\ p\]  ...

  8. P3768 【简单的数学题】

    P3768 [简单的数学题] \(Ans=\sum ^{n}_{i=1}\sum ^{n}_{j=1}ijgcd(i,j)\) \(=\sum ^{n}_{i=1}\sum ^{n}_{j=1}ij\ ...

  9. NYOJ 330 一个简单的数学题【数学题】

    /* 题目大意:求解1/n; 解题思路:写一个输出小数的算法 关键点:怎样处理小数点循环输出 解题人:lingnichong 解题时间:2014-10-18 09:04:22 解题体会:输出小数的算法 ...

随机推荐

  1. CSS 小结笔记之BFC

    BFC 即为Block formatting context 的缩写,BFC 主要用来将一个盒子设置为一个隔离的容器,不管盒子内部的元素具有什么属性,都不会影响到盒子的外面. 1.哪些元素能产生BFC ...

  2. python 实现int函数

    拖了这么久,最终还是战胜了懒惰,打开电脑写了这篇博客,内容也很简单,python实现字符串转整型的int方法 python已经实现了int方法,我们为什么还要再写一遍,直接用不就好了?事实确实如此,但 ...

  3. 腾讯云自建MySQL数据库访问

    1. 登陆腾讯云 https://cloud.tencent.com/ 2. 登陆控制台 https://console.cloud.tencent.com/ 3. 选择云主机 4. 选择重装系统 5 ...

  4. 在 Azure VM 中使用应用商店映像创建 HPC Pack 群集的头节点

    使用 Azure 应用商店和 Azure 门户中的 Microsoft HPC Pack 2012 R2 虚拟机映像创建 HPC 群集的头节点. 此 HPC Pack VM 映像基于预安装了 HPC ...

  5. jmeter之数据库相关

    一.JDBC Connection Configuration 1.Variable Name Bound to Pool-Variable Name:连接池名称, JDBC Request通过此名称 ...

  6. SQL Server自动备份 备份到本地或者远程服务器

    0.1 在SQLServer2008 --> 备份数据库 --> 安全 --> 新建用户 --> 用户名 选择该windows用户 (确保 --> 机器名/人名 --&g ...

  7. Python3部分Print输出格式

    print("Hello World!") #直接打印字符串 print('Hello World!') #对于python,单引号也可以表示字符串 name = 'Tom' #自 ...

  8. C#读取AD域用户信息

    private const string domainName = "本机IP地址或域名"; private const string adAdmin = "管理员帐号& ...

  9. Python实例--12306的抢票功能

    基础知识学习 目标: 通过python程序实现自动登录下单功能 知识点: Selenium + 云打码 + Python 学习链接: 1. Python学习--Selenium模块 2. Python ...

  10. qt designer启动后不显示界面问题的原因与解决办法

    Qt 5.6.1无论是在vs里双击ui文件还是直接启动designer.exe都一直无法显示界面,但任务管理器中可以看到该进程是存在的.前几天还正常的,但昨天加了一块NVIDIA的显卡(机器自带核显) ...