HDU6706 huntian oy(2019年CCPC网络赛+杜教筛)
题目链接
思路
看到这题还比较懵逼,然后机房大佬板子里面刚好有这个公式\(gcd(a^n-b^n,a^m-b^m)=a^{gcd(n,m)}-b^{gcd(n,m)}\),然后自己随手推了一下就过了。
在知道上面那个公式后化简如下:
&\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网络赛+杜教筛)的更多相关文章
- 2019年CCPC网络赛 HDU 6703 array【权值线段树】
题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...
- 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互质. ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
- (四面体)CCPC网络赛 HDU5839 Special Tetrahedron
CCPC网络赛 HDU5839 Special Tetrahedron 题意:n个点,选四个出来组成四面体,要符合四面体至少四条边相等,若四条边相等则剩下两条边不相邻,求个数 思路:枚举四面体上一条线 ...
- 2018 CCPC网络赛
2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...
- ccpc 网络赛 hdu 6155
# ccpc 网络赛 hdu 6155(矩阵乘法 + 线段树) 题意: 给出 01 串,要么询问某个区间内不同的 01 子序列数量,要么把区间翻转. 叉姐的题解: 先考虑怎么算 \(s_1, s_2, ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- 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( ...
- 2019杭电多校&CCPC网络赛&大一总结
多校结束了, 网络赛结束了.发现自己还是太菜了,多校基本就是爆零和签到徘徊,第一次打这种高强度的比赛, 全英文,知识点又很广,充分暴露了自己菜的事实,发现数学还是很重要的.还是要多刷题,少玩游戏. 网 ...
随机推荐
- Web协议详解与抓包实战:HTTP1协议-内容协商是怎样进行的(8)
一.内容协商的两种方式 每个 URI 指向的资源可以是任何事物,可以有多种不同的表述,例如一份文档可以有不同语言的翻译.不同的媒体格式.可以针对不同的浏览器提供不同的压缩编码等 二.Proactive ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- Mysql遇到的问题总结
1.解决导出csv中文乱码问题: 将csv用txt打开,另存为,选择utf8编码即可. 解决导入问题: mysql安装目录下的my.ini,增加如下参数: [client]default-charac ...
- xunit测试无法找到testhost或没有可用测试的问题解决方法
xunit进行测试,需要安装如下几个包: Microsoft.TestPlatform.TestHost Microsoft.NET.Test.Sdk xunit.runner.visualstudi ...
- 初探Java设计模式5:一文了解Spring涉及到的9种设计模式
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- 改写URL的查询字符串QUERY_STRING[URL重定向问号问题](转)
查询字符串是指URL请求中"问号"后面的部分.比如,http://mysite/?foo=bar 中粗体部分就是查询字符串,其中变量名是foo,值是bar. 'last|L' (最 ...
- linux centos7下源码 tar安装mysql5.7.22或mysql5.7.20 图文详解
之前用的rpm安装的每次安装都是最新的,,,导致每次版本不统一... 现在用tar包安装5.7.22和5.7.20一样的 5.7.20之后的和之前的版本还是有点不一样的 官网地址 https:// ...
- 关于宝塔面板windows版6.2的一些使用心得
关于宝塔面板windows版6.2的一些使用心得 第一次使用windows版本的 给客户搭建 asp+mssql的需求 心得1 安装 server2012 基于python开发的,所以安装的 ...
- 实现外网远程桌面内网的电脑和外网访问内网的FTP
基于之前两篇文章搭建了ngrok实现了内网穿透,用过了http和https的协议完成了外网访问内网的网站,这一篇教大家用tcp协议实现外网远程桌面内网的电脑和外网访问内网的FTP. 一.外网远程桌面 ...
- git 命令从入门到放弃
o(︶︿︶)o 由于项目使用 git 作为版本控制工具,自己可以进行一些常用操作但是有时候还是会忘掉,导致每次遇到 git 命令的使用问题时就要再查一遍,效率就立马降下来了,所以今天就来一个从头到尾 ...