题目链接

传送门

思路

看到这题还比较懵逼,然后机房大佬板子里面刚好有这个公式\(gcd(a^n-b^n,a^m-b^m)=a^{gcd(n,m)}-b^{gcd(n,m)}\),然后自己随手推了一下就过了。

在知道上面那个公式后化简如下:

\[\begin{aligned}
&\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}(i-j)[gcd(i,j)=1]&\\
=&\sum\limits_{i=1}^{n}(i\phi(i)-\sum\limits_{j=1}^{i}j[gcd(i,j)=1]&\\
=&\sum\limits_{i=1}^{n}i\phi(i)-\frac{i\phi(i)}{2}&\\
=&\frac{1}{2}(\sum\limits_{i=1}^{n}i\phi(i)-1)&
\end{aligned}
\]

第一步到第二步是算\(i\)的贡献,第二步到第三步是小于\(i\)且与\(i\)互质的数的和。

然后我们可以用杜教筛来求解这个东西,杜教筛推导过程可以看这篇博客

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 3000000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; bool v[maxn];
int phi[maxn], p[maxn];
int t, n, a, b, cnt, inv, inv2;
LL sum[maxn];
unordered_map<int, LL> dp; LL qpow(LL x, int n) {
LL res = 1;
while(n) {
if(n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
} void init() {
phi[1] = 1;
for(int i = 2; i < maxn; ++i) {
if(!v[i]) {
p[cnt++] = i;
phi[i] = i - 1;
}
for(int j = 0; j < cnt && i * p[j] < maxn; ++j) {
v[i*p[j]] = 1;
if(i % p[j] == 0) {
phi[i*p[j]] = phi[i] * p[j];
break;
}
phi[i*p[j]] = phi[i] * (p[j] - 1);
}
}
for(int i = 1; i < maxn; ++i) sum[i] = (sum[i-1] + 1LL * i * phi[i] % mod) % mod;
} LL dfs(int x) {
if(x < maxn) return sum[x];
if(dp.count(x)) return dp[x];
LL ans = 1LL * x * (x + 1) % mod * (2LL * x % mod + 1) % mod * inv % mod;
for(int l = 2, r; l <= x; l = r + 1) {
r = x / (x / l);
LL tmp = 1LL * (r - l + 1) * (l + r) / 2;
tmp %= mod;
ans = ((ans - 1LL * tmp % mod * dfs(x / l) % mod) % mod + mod) % mod;
}
return dp[x] = ans;
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
init();
inv = qpow(6, mod - 2);
inv2 = qpow(2, mod - 2);
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &a, &b);
LL tmp = dfs(n);
printf("%lld\n", (dfs(n) - 1 + mod) % mod * inv2 % mod);
}
return 0;
}

HDU6706 huntian oy(2019年CCPC网络赛+杜教筛)的更多相关文章

  1. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  2. CCPC 2019 网络赛 HDU huntian oy (杜教筛)

    1005 huntian oy (HDU 6706) 题意: 令,有T次询问,求 f(n, a, b). 其中 T = 10^4,1 <= n,a,b <= 1e9,保证每次 a,b互质. ...

  3. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

  4. (四面体)CCPC网络赛 HDU5839 Special Tetrahedron

    CCPC网络赛 HDU5839 Special Tetrahedron 题意:n个点,选四个出来组成四面体,要符合四面体至少四条边相等,若四条边相等则剩下两条边不相邻,求个数 思路:枚举四面体上一条线 ...

  5. 2018 CCPC网络赛

    2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...

  6. ccpc 网络赛 hdu 6155

    # ccpc 网络赛 hdu 6155(矩阵乘法 + 线段树) 题意: 给出 01 串,要么询问某个区间内不同的 01 子序列数量,要么把区间翻转. 叉姐的题解: 先考虑怎么算 \(s_1, s_2, ...

  7. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  8. EOJ Monthly 2019.11 E. 数学题(莫比乌斯反演+杜教筛+拉格朗日插值)

    传送门 题意: 统计\(k\)元组个数\((a_1,a_2,\cdots,a_n),1\leq a_i\leq n\)使得\(gcd(a_1,a_2,\cdots,a_k,n)=1\). 定义\(f( ...

  9. 2019杭电多校&CCPC网络赛&大一总结

    多校结束了, 网络赛结束了.发现自己还是太菜了,多校基本就是爆零和签到徘徊,第一次打这种高强度的比赛, 全英文,知识点又很广,充分暴露了自己菜的事实,发现数学还是很重要的.还是要多刷题,少玩游戏. 网 ...

随机推荐

  1. Kubernetes Dashboard 安装与认证

    1.安装dashboard $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/ ...

  2. [FY20 创新人才班 ASE] 第 1 次作业成绩

    作业概况 条目 备注 作业链接 [ASE高级软件工程]热身作业! 提交人数 19 未完成人数 2 满分 10分 作业情况总结 本次作业作为大家软工课程的第一次作业,完成度相当不错(尤其是在国外暑研/赶 ...

  3. 一篇了解大数据架构及Hadoop生态圈

    一篇了解大数据架构及Hadoop生态圈 阅读建议,有一定基础的阅读顺序为1,2,3,4节,没有基础的阅读顺序为2,3,4,1节. 第一节 集群规划 大数据集群规划(以CDH集群为例),参考链接: ht ...

  4. PostMan测试REST接口时候,如何绕过登录的验证

    原文地址:https://blog.csdn.net/qq_34178998/article/details/80361315 之前测试的时候,需要页面进行登录之后,才能让访问后台程序,但是在进行接口 ...

  5. linux 开启oracle监听

    secureCRT连接到数据库所在的linux机器,切换到oracle用户模式下 [root@nstlbeta ~]# su - oracle 步骤阅读 2 然后用sqlplus登录到数据库,关闭数据 ...

  6. python学习--大数据与科学计算第三方库简介

    大数据与科学计算  库名称 简介 pycuda/opencl GPU高性能并发计算 Pandas python实现的类似R语言的数据统计.分析平台.基于NumPy和Matplotlib开发的,主要用于 ...

  7. kvm虚拟机vnc配置

    通过vnc方式访问虚拟主机上的KVM虚拟机 通过虚拟主机的IP地址与端口进行访问 1.  修改qemu.conf # vi /etc/libvirt/qemu.conf vnc_listen = &q ...

  8. springboot xss防护

    概述 XSS(Cross Site Script)全称跨站脚本攻击,为了跟CSS区分开来,所以变成了XSS.它允许恶意代码植入到正常的页面中,盗取正常用户的账号密码,诱使用户访问恶意的网站. 攻击 实 ...

  9. 深入V8引擎-初始化之InitPlatform

    上一篇其实想讲初始化的第二步,但是内容比较无聊,所以换了一个话题,谈了谈v8的命名空间和宏,稍微轻松一下. 在这里还是接着说说初始化过程,毕竟写博客的初衷是对自己努力的记录,不是为了吸粉,这篇没图,对 ...

  10. java的三种随机数生成方式

    随机数的产生在一些代码中很常用,也是我们必须要掌握的.而java中产生随机数的方法主要有三种: 第一种:new Random() 第二种:Math.random() 第三种:currentTimeMi ...