【CodeForces】915 G. Coprime Arrays 莫比乌斯反演,前缀和,差分
Coprime Arrays
Let's call an array a of size n coprime iff gcd(a1, a2, ..., a**n) = 1, where gcd is the greatest common divisor of the arguments.
You are given two numbers n and k. For each i (1 ≤ i ≤ k) you have to determine the number of coprime arrays a of size n such that for every j (1 ≤ j ≤ n) 1 ≤ a**j ≤ i. Since the answers can be very large, you have to calculate them modulo 109 + 7.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 2·106) — the size of the desired arrays and the maximum upper bound on elements, respectively.
Output
Since printing 2·106 numbers may take a lot of time, you have to output the answer in such a way:
Let b**i be the number of coprime arrays with elements in range [1, i], taken modulo 109 + 7. You have to print , taken modulo 109 + 7. Here denotes bitwise xor operation (^ in C++ or Java, xor in Pascal).
Examples
Input
3 4
Output
82
Input
2000000 8
Output
339310063
Note
Explanation of the example:
Since the number of coprime arrays is large, we will list the arrays that are non-coprime, but contain only elements in range [1, i]:
For i = 1, the only array is coprime. b1 = 1.
For i = 2, array [2, 2, 2] is not coprime. b2 = 7.
For i = 3, arrays [2, 2, 2] and [3, 3, 3] are not coprime. b3 = 25.
For i = 4, arrays [2, 2, 2], [3, 3, 3], [2, 2, 4], [2, 4, 2], [2, 4, 4], [4, 2, 2], [4, 2, 4], [4, 4, 2] and [4, 4, 4] are not coprime. b4 = 55.
题意:
给你一个整数n和k,
问你长度为n的整数数组中,每一个元素a[i] 要求满足\(a[i]<=k\),即最大值为k
b[i] = 最大值为i(即数组中每一个元素\(a[j]<=i\)),满足条件的数组种类个数。
让你输出
思路:
根据莫比乌斯反演,可以得到
但是我们需要求的是b1到bk而不是单独的一个bi,这是最重要的一个性质。
所以我们考虑\(b[i+1]\)和\(b[i]\)的关系,
\(b[i+1]=Σ[d|k] μ(d)*((k/d)^n-(k/d-1)^n)+b[i]\)
然后差分,求\(f[i]=b[i+1]-b[i]\)
计算答案的时候求一个前缀和即可,
记得\(i^n\) 要预处理出来降低时间复杂度。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 2000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define N maxn
bool vis[N];
long long prim[N], mu[N], sum[N], cnt;
void get_mu(long long n)
{
mu[1] = 1;
for (long long i = 2; i <= n; i++) {
if (!vis[i]) {mu[i] = -1; prim[++cnt] = i;}
for (long long j = 1; j <= cnt && i * prim[j] <= n; j++) {
vis[i * prim[j]] = 1;
if (i % prim[j] == 0) { break; }
else { mu[i * prim[j]] = -mu[i]; }
}
}
for (long long i = 1; i <= n; i++) { sum[i] = sum[i - 1] + mu[i]; }
}
ll n, k;
ll b[maxn];
const ll mod = 1e9 + 7ll;
ll f[maxn];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
get_mu(maxn - 1);
cin >> n >> k;
cout<<mu[2]<<endl;
repd(i, 0, k) {
ll cnt = powmod(1ll * i, n, mod);
b[i] = cnt;
}
ll ans = 0ll;
ll ANS = 0ll;
repd(i, 1, k) {
for (int j = i; j <= k; j += i) {
f[j] += mu[i] * (b[j / i] - b[j / i - 1]) % mod;
f[j] = (f[j] + mod) % mod;
}
// chu(f[i]);
ans = (ans + (f[i] )) % mod;
ANS = (ANS + (ans ^ i)) % mod;
}
cout << ANS << endl;
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
【CodeForces】915 G. Coprime Arrays 莫比乌斯反演,前缀和,差分的更多相关文章
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演
[题目]G. Coprime Arrays [题意]当含n个数字的数组的总gcd=1时认为这个数组互质.给定n和k,求所有sum(i),i=1~k,其中sum(i)为n个数字的数组,每个数字均< ...
- Codeforces 915 G Coprime Arrays
Discipntion Let's call an array a of size n coprime iff gcd(a1, a2, ..., an) = 1, where gcd is the g ...
- Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)
Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...
- CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和
传送门 差分是真心人类智慧--完全不会 这么经典的式子肯定考虑莫比乌斯反演,不难得到\(b_k = \sum\limits_{i=1}^k \mu(i) \lfloor\frac{k}{i} \rfl ...
- Codeforces 479E Riding in a Lift:前缀和/差分优化dp
题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...
- Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays
求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_ ...
- Gym - 101982B Coprime Integers (莫比乌斯反演)
题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c ...
- hdu 4746 Mophues 莫比乌斯反演+前缀和优化
Mophues 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 有Q组数据:(n, m, ...
- F. Coprime Subsequences 莫比乌斯反演
http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...
随机推荐
- SQL中有关DQL、DML、DDL、DCL的概念与区别?
SQL(Structure Query Language)结构化查询语言是数据库的核心语言,是高级的非过程化编程语言.它功能强大,效率高,简单易学易维护.SQL语言基本上独立于数据库本身.使用的机器. ...
- 前端JS之HTML利用XMLHttpRequest()和FormData()进行大文件分段上传
用于网页向后端上传大文件 ### 前端代码<body> <input type="file" name="video" id="fi ...
- jquery设置input不可编辑,背景变灰,鼠标变禁止
先看效果 $("#id").attr("onfocus", "this.blur()"); $("#id").css(& ...
- Docker 安装 Tomcat
查找Docker Hub上的tomcat镜像 docker search tomcat 取官方的镜像 docker pull tomcat 使用tomcat镜像 创建目录tomcat,用于存放后面的相 ...
- Design In-Memory File System
Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...
- HanLP-基于HMM-Viterbi的人名识别原理介绍
Hanlp自然语言处理包中的基于HMM-Viterbi处理人名识别的内容大概在年初的有分享过这类的文章,时间稍微久了一点,有点忘记了.看了 baiziyu 分享的这篇比我之前分享的要简单明了的多.下面 ...
- Spark Scala当中reduceByKey的用法
[学习笔记] /*reduceByKey(function)reduceByKey就是对元素为KV对的RDD中Key相同的元素的Value进行function的reduce操作(如前所述),因此,Ke ...
- MySQL中的左连接遇到的坑
参考地址:https://blog.csdn.net/feichangwurao/article/details/89526741 待研究整理中.....
- DISCO Presents Discovery Channel Code Contest 2020 Qual Task E. Majority of Balls
Not able to solve this problem during the contest (virtual participation). The first observation is ...
- 深入理解C++11 C3
继承构造函数 class A { public: A(int i):m_i(i) {} A(double d, int i):m_d(d),m_i(i){} private: int m_i{0}; ...