Coprime Arrays

CodeForces - 915G

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 莫比乌斯反演,前缀和,差分的更多相关文章

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

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

  2. 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 ...

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

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

  4. CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和

    传送门 差分是真心人类智慧--完全不会 这么经典的式子肯定考虑莫比乌斯反演,不难得到\(b_k = \sum\limits_{i=1}^k \mu(i) \lfloor\frac{k}{i} \rfl ...

  5. Codeforces 479E Riding in a Lift:前缀和/差分优化dp

    题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...

  6. 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_ ...

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

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

  8. hdu 4746 Mophues 莫比乌斯反演+前缀和优化

    Mophues 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 有Q组数据:(n, m, ...

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

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

随机推荐

  1. 【VS开发】malloc申请内存错误分析

    每个进程会有4G的虚拟地址空间, malloc得到的的地址都是虚拟地址, 并且当malloc的时候, 操作系统并不会将实际的内存分配给进程的, 所以malloc只会占用进程自身的虚拟地址空间.我以前也 ...

  2. JS实现对数组进行自定义排序

    /** * 数组排序 * @param source 待排序数组 * @param orders 排序字段数组 * @param type 升序-asc 倒序-desc * 调用:var res =  ...

  3. 洛谷 题解 P2119【魔法阵】

    很好的一道数学推导题 45分做法 \(O(N^4)\) 暴力枚举四个材料 55分做法 从第一个约束条件可得到所有可行答案都是单调递增的,所以可以排序一遍,减少枚举量,可以拿到55分 100分做法 首先 ...

  4. Mybatis插件之Mybatis-Plus(传统模式)

    1.什么是Mybatis-Plus? Mybatis-Plus(简称MP),是一个Mybatis的增强工具,可以在原来Mybatis基础上不错任何改变就能够对原来Mybatis进行增强,能够简化我们的 ...

  5. 【jmeter测试范例】001——TCP测试

    1.打开Jmeter(或者运行NewDriver.java启动Jmeter) 2.新建一个测试计划 ······ 3.新建线程组 4.设置线程组的参数 1.线程的数量 2.要在多久内完成,即每个请求发 ...

  6. web前端页面解决中文传参乱码问题

    问题背景:在项目中往往会涉及到前端跳转页面时要传一些参数给下一个页面,如果参数是英文或者数字的时候就很好解决,然而有时候传参会涉及到中文汉字,这个时候再单纯的拼接往往就会导致中文乱码,下面我们就该讨论 ...

  7. Vufuria入门 1 图片识别和选择

    Vufutia中的图片识别功能,底层主要是识别特征点来实现的.特征点,即那些棱角分明的点.尖锐的而不是圆滑的.对比度大的而不是小的. *** 步骤: 进入vofuria官网,登录,点击develop. ...

  8. JavaScript(js)笔记

    js注释 JavaScript注释与Java注释相同 // 单行注释 /* 多行注释 */ js五大基本类型:   number(数值型).string(字符串性).boolean(布尔型).unde ...

  9. [python]近日 用3种库 实现简单的窗口 的回顾~

    最近任务:利用python 实现以下4个窗口弹窗. 信息提示框 文本输入框(需在窗口消失后,返回 用户输入的值) 文件选择(需在窗口消失后, 返回 用户选择的文件名的全路径) 文件夹选择(需在窗口消失 ...

  10. not or and 的优先级是不同的

    not or and 的优先级是不同的: not > and > or 请用最快速度说出答案: not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 an ...