CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和
差分是真心人类智慧……完全不会
这么经典的式子肯定考虑莫比乌斯反演,不难得到\(b_k = \sum\limits_{i=1}^k \mu(i) \lfloor\frac{k}{i} \rfloor^n\)
直接做是\(O(n\sqrt{n})\)的不够优秀,但是我们需要求的是\(b_1\)到\(b_K\)而不是单独的一个\(b\),这是最重要的一个性质。
考虑每一个数\(p\)对\(b_1\)到\(b_k\)的贡献。因为\(\mu(p)\)不变,所以对于\(\forall k \in Z_+ ,\)数\(p\)对\(b_{kp}\)到\(b_{(k+1)p-1}\)的贡献是一致的,都是\(\mu(p) k^n\)。既然对于一段区间的贡献相同,那就差分一下,最后前缀和统计答案即可。
总复杂度为预处理\(n\)次方的\(O(klogn)\)加上差分时的\(O(klogk)\)。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<vector>
#include<cmath>
#include<random>
#include<cassert>
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return f ? -a : a;
}
#define ll long long
const int MAXN = 2e6 + 3 , MOD = 1e9 + 7;
int powx[MAXN] , prime[MAXN] , mu[MAXN] , cf[MAXN];
bool nprime[MAXN];
int cnt , N , K;
inline int poww(ll a , int b){
int times = 1;
while(b){
if(b & 1)
times = times * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return times;
}
void init(){
for(int i = 1 ; i <= K ; ++i)
powx[i] = poww(i , N);
mu[1] = 1;
for(int i = 2 ; i <= K ; ++i){
if(!nprime[i]){
prime[++cnt] = i;
mu[i] = -1;
}
for(int j = 1 ; prime[j] * i <= K ; ++j){
nprime[prime[j] * i] = 1;
if(i % prime[j] == 0)
break;
mu[prime[j] * i] = mu[i] * -1;
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
N = read();
K = read();
init();
for(int i = 1 ; i <= K ; ++i)
for(int j = 1 ; j * i <= K ; ++j)
(cf[j * i] += mu[i] * (powx[j] - powx[j - 1] + MOD) % MOD) %= MOD;
int ans = 0;
for(int i = 1 ; i <= K ; ++i){
cf[i] = (0ll + MOD + cf[i] + cf[i - 1]) % MOD;
ans = (ans + (cf[i] ^ i)) % MOD;
}
cout << ans;
return 0;
}
CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和的更多相关文章
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演,前缀和,差分
Coprime Arrays CodeForces - 915G Let's call an array a of size n coprime iff gcd(a1, a2, ..., *a**n) ...
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演
[题目]G. Coprime Arrays [题意]当含n个数字的数组的总gcd=1时认为这个数组互质.给定n和k,求所有sum(i),i=1~k,其中sum(i)为n个数字的数组,每个数字均< ...
- Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)
Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...
- CF915G Coprime Arrays (莫比乌斯反演)
CF915G Coprime Arrays 题解 (看了好半天终于看懂了) 我们先对于每一个i想,那么 我们设 我们用莫比乌斯反演 有了这个式子,可比可以求出△ans呢?我们注意到,由于那个(i/d) ...
- Gym - 101982B Coprime Integers (莫比乌斯反演)
题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c ...
- CF585E-Present for Vitalik the Philatelist【莫比乌斯反演,狄利克雷前缀和】
正题 题目链接:https://www.luogu.com.cn/problem/CF585E 题目大意 给出一个大小为\(n\)的可重集\(T\),求有多少个它的非空子集\(S\)和元素\(x\)满 ...
- F. Coprime Subsequences 莫比乌斯反演
http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...
- nyoj CO-PRIME 莫比乌斯反演
CO-PRIME 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 This problem is so easy! Can you solve it? You are ...
- [HAOI2011][bzoj2301] Problem b [莫比乌斯反演+容斥原理+分块前缀和优化]
题面: 传送门 有洛谷就尽量放洛谷链接呗,界面友好一点 思路: 和HDU1695比较像,但是这一回有50000组数据,直接莫比乌斯反演慢慢加的话会T 先解决一个前置问题:怎么处理a,c不是1的情况? ...
随机推荐
- 基础篇|一文搞懂RNN(循环神经网络)
基础篇|一文搞懂RNN(循环神经网络) https://mp.weixin.qq.com/s/va1gmavl2ZESgnM7biORQg 神经网络基础 神经网络可以当做是能够拟合任意函数的黑盒子,只 ...
- Ansible--inventory
简介 Inventory 是 Ansible 管理主机信息的配置文件,相当于系统 HOSTS 文件的功能,默认存放在 /etc/ansible/hosts.为方便批量管理主机,便捷使用其中的主机分组, ...
- 【转】Maya Mel – Search String in String
转自:http://schiho.com/?p=179 Hi, this is a little script which returns a 0 or 1 if the searched text ...
- loadrunner 运行场景-场景运行原理
运行场景-场景运行原理 by:授客 QQ:1033553122 运行原理 1 Remote Agent Dispatcher(Process) 运行Controller在负载机上开启应用程序. 2 ...
- LeanCloud数据存储相关问题
1.短信验证码 当注册用户的时候,会发现收不到短信验证码,打印e : That operation isn't allowed for clients. 含义 - 该操作无法从客户端发起.请检查该错误 ...
- JMeter—逻辑控制器(六)
参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第一节JMeter逻辑控制器 JMeter逻辑控制器可以对元件的执行逻辑进行控制,除仅一次控制器外,其他可以嵌套 ...
- ffmpeg文件切片
先用ffmpeg把abc.mp4文件转换为abc.ts文件: ffmpeg -y -i abc.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb ...
- cx_Oracle读取Oracle数据库中文乱码问题解决
在使用cx_Oracle模块读取Oracle数据库中的中文记录时,返回值皆为?,后google得此佳文,遂问题得以解决,特此记之. Oracle数据库版本是10g,字符集是AL32UTF8. 编写的p ...
- Go语言学习笔记(一)Let's 干吧
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 简介 Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索 ...
- nginx服务器开启缓存、反向代理
一.反向代理配置 1.反向代理服务器配置如下 反向代理就是需要这一行proxy_pass来完成.当我们要访问后端web服务器的时候,我们只需要访问代理服务器就可以了,此时代理服务器就充当后端web服务 ...