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的情况? ...
随机推荐
- svg简介与使用
什么是svg SVG是"Scalable Vector Graphics"的简称.中文可以理解成"可缩放矢量图形". 可缩放矢量图形是基于可扩展标记语言(标准通 ...
- Flutter 安装
都说程序猿学习是不分平台的,做了一辈子的Xaml,也想看看现在最牛逼的移动技术. 看了看Google 的Flutter,好像很牛逼,不怎么需要Android和IOS基础(应该还是要的), 不过现在是B ...
- JdbcTemplate学习笔记(更新插入删除等)
1.使用JdbcTemplate的execute()方法执行SQL语句 jdbcTemplate.execute("CREATE TABLE USER (user_id integer, n ...
- 机器学习实战(Machine Learning in Action)学习笔记————10.奇异值分解(SVD)原理、基于协同过滤的推荐引擎、数据降维
关键字:SVD.奇异值分解.降维.基于协同过滤的推荐引擎作者:米仓山下时间:2018-11-3机器学习实战(Machine Learning in Action,@author: Peter Harr ...
- Openjdk 安装 on centos7
本文演示如何在CentOS7上安装openjdk. 1 准备工作 1.1 查看可安装的版本 $ yum -y list java-1.8* # 列出当前可用的安装版本 Available Packag ...
- TensorFlow实现Softmax回归(模型存储与加载)
# -*- coding: utf-8 -*- """ Created on Thu Oct 18 18:02:26 2018 @author: zhen "& ...
- C++ 获取当前正在执行的函数的相关信息
(我的运行环境:win10x64+vs2015通过, 有的环境KUbuntu 8.04.1 x64 g++ 4.2.3也通过了)主要通过宏来实现:(注意,开头和结尾都是两个下划线) 1. __PRET ...
- 洗礼灵魂,修炼python(42)--巩固篇—type内置函数与类的千丝万缕关系
type函数的隐藏属性 相信大家都知道内置函数type是用来查看对象的数据类型的.例: 那比如我对int类查看类型呢? 有朋友会说,int是内置类啊,用自定义的应该不会这样,我们自定义一个类呢? 还是 ...
- AspNet Core2 浏览器缓存使用
Core2中使用Microsoft.AspNetCore.Mvc下的ResponseCacheAttribute特性来控制Http Get请求的缓存 原理是设置http请求 响应头的Cache-con ...
- 第五章 绘图基础(SINEWAVE)
//SINEWAVE.C -- Sine Wave Using Polyline (c) Charles Petzold, 1998 #include <Windows.h> #inclu ...