传送门


差分是真心人类智慧……完全不会

这么经典的式子肯定考虑莫比乌斯反演,不难得到\(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 莫比乌斯反演、差分、前缀和的更多相关文章

  1. 【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) ...

  2. 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演

    [题目]G. Coprime Arrays [题意]当含n个数字的数组的总gcd=1时认为这个数组互质.给定n和k,求所有sum(i),i=1~k,其中sum(i)为n个数字的数组,每个数字均< ...

  3. Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)

    Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...

  4. CF915G Coprime Arrays (莫比乌斯反演)

    CF915G Coprime Arrays 题解 (看了好半天终于看懂了) 我们先对于每一个i想,那么 我们设 我们用莫比乌斯反演 有了这个式子,可比可以求出△ans呢?我们注意到,由于那个(i/d) ...

  5. Gym - 101982B Coprime Integers (莫比乌斯反演)

    题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c ...

  6. CF585E-Present for Vitalik the Philatelist【莫比乌斯反演,狄利克雷前缀和】

    正题 题目链接:https://www.luogu.com.cn/problem/CF585E 题目大意 给出一个大小为\(n\)的可重集\(T\),求有多少个它的非空子集\(S\)和元素\(x\)满 ...

  7. F. Coprime Subsequences 莫比乌斯反演

    http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...

  8. nyoj CO-PRIME 莫比乌斯反演

    CO-PRIME 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 This problem is so easy! Can you solve it? You are ...

  9. [HAOI2011][bzoj2301] Problem b [莫比乌斯反演+容斥原理+分块前缀和优化]

    题面: 传送门 有洛谷就尽量放洛谷链接呗,界面友好一点 思路: 和HDU1695比较像,但是这一回有50000组数据,直接莫比乌斯反演慢慢加的话会T 先解决一个前置问题:怎么处理a,c不是1的情况? ...

随机推荐

  1. 遇到npm报错read ECONNRESET怎么办

    遇到npm 像弱智一样报错怎么办 read ECONNRESET This is most likely not a problem with npm itselft 'proxy' config i ...

  2. 对JavaScript中闭包的理解

    在前端开发中闭包是一个很重要的知识点,是面试中一定会被问到的内容.之前我对闭包的理解主要是"通过闭包可以在函数外部能访问到函数内部的变量",对闭包运用的也很少,甚至自己写过闭包自己 ...

  3. Python之随机森林实战

    代码实现: # -*- coding: utf-8 -*- """ Created on Tue Sep 4 09:38:57 2018 @author: zhen &q ...

  4. ALSA声卡驱动的DAPM(二)-建立过程

    在上一篇文章中,我们重点介绍了widget.path.route之间的关系及其widget的注册: http://www.cnblogs.com/linhaostudy/p/8509899.html ...

  5. Excel两列查找重复值

    判断A列的值在B列是否存在,如果存在则输出“yes” 在C1单元格使用如下公式: =IF(COUNTIF(A:A,B2)>0,"yes","")

  6. 异常检测(anomaly detection)

    版权声明:本文为博主原创文章,转载或者引用请务必注明作者和出处,尊重原创,谢谢合作 https://blog.csdn.net/u012328159/article/details/51462942 ...

  7. 【PAT】B1075 链表元素分类(25 分)

    这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...

  8. Linux之特殊的环境变量IFS以及如何删除带有空格的目录

    1.IFS是什么? Linux下有一个特殊的环境变量叫做IFS,叫做内部字段分隔符(internal field separator).IFS环境变量定义了bash shell用户字段分隔符的一系列字 ...

  9. Ubuntu 16.04 LTS 降级安装GCC 4.8

    转载自https://www.linuxidc.com/Linux/2017-03/142299.htm Ubuntu 16.04 LTS 降级安装GCC 4.8 [日期:2017-03-28] 来源 ...

  10. input 属性radio中设置checked 不生效

    同一个页面中有许多地方都用到了单选按钮并设置了默认选中 , 结果在运行的时候发现单选按钮没有被默认选中 由于是复制然后修改个别属性 ,然后直接使用的 , 所以name值忘记修改了 , 单选框是根据na ...