题目链接

传送门

思路

看到这题还比较懵逼,然后机房大佬板子里面刚好有这个公式\(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. [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  2. 浅析容斥和DP综合运用

    浅析容斥和DP综合运用 前言 众所周知在数数题中有一种很重要的计数方法--容斥.但是容斥有一个很大的缺陷:枚举子集的复杂度过高.所以对于数据规模较大的情况会很乏力,那么我们就只能引入容斥DP. 复习一 ...

  3. AD域配置以及开发机加入AD域

    1. windows server 2012 添加AD域 : https://www.cnblogs.com/chenjiangfeng/p/9706483.html 2. 配置成功后重启AD域服务器 ...

  4. fiddler抓包-2-5分钟学会手机端抓包

    前言 小伙伴们在前篇是否学会了简单的电脑web或其它软件抓包了呢?今天小编给大家带来的是fiddler设置手机代理抓手机端的数据包. 大致流程如下: 1.准备一台wifi功能正常的真机或虚拟机:2.设 ...

  5. <T>泛型,广泛的类型

    其实早在1999年的JSR 14规范中就提到了泛型概念,知道jdk5泛型的使用才正式发布,在jdk7后,又对泛型做了优化,泛型的推断. 泛型类 public class Pair<T> { ...

  6. Go排序练习

    1.插入排序 类似扑克起牌,每起一张牌都按大小将牌放到合适的位置 package main import "fmt" func insert(a []]int { for i := ...

  7. 转载:string、const char*、 char* 、char[]相互转换

    本文转自:https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 一:转化总结形式如下: 使用时,要对源格式和目标格式进行初始化 ...

  8. hbase源码之 compact源码(一)

    hbase compact流程较多,这里分章节介绍,首先介绍compact在regionserver中的调用流程,并不会涉及真正的compact读取合并文件的流程,后续介绍. 在regionserve ...

  9. Docker修改已创建容器端口映射

    修改已创建容器端口映射. 通过编辑 hostconfig.json 文件来修改 Docker 容器的端口映射 该文件地址:/var/lib/docker/containers/[hash_of_the ...

  10. log4j打印抛出异常时堆栈内容

    JAVA 中为快速定位,抛出异常的位置.往往需要在日志中描述. log.err("异常出错点", e); 会将出错时的堆栈信息显示出来. package com.jqyc.jase ...