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. 【手记】解决Graphics.DrawImage带ImageAttributes在XP报内存不足的问题

    异常信息: System.OutOfMemoryException: 内存不足. 在 System.Drawing.Graphics.CheckErrorStatus(Int32 status) 在 ...

  2. [译]如何使用Python构建指数平滑模型:Simple Exponential Smoothing, Holt, and Holt-Winters

    原文连接:How to Build Exponential Smoothing Models Using Python: Simple Exponential Smoothing, Holt, and ...

  3. 【VS开发】千兆以太网的传输速度

    千兆以太网主流标准 千兆以太网络技术早在上世纪90年代末就已成熟,其中,1995年国际标准化组织TIA/EIA颁布了1000Base-TX标准,该标准的目的是把双绞线用于千兆以太网中,其目的是在6类非 ...

  4. Linux系统下GDB调试

    GDB 一.gdb常用命令: 命令 描述 backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 in ...

  5. HanLP-命名实体识别总结

    人名识别 在HanLP中,基于角色标注识别了中国人名.首先系统利用隐马尔可夫模型标注每个词语的角色,之后利用最大模式匹配法对角色序列进行匹配,匹配上模式的即为人名.理论指导文章为:<基于角色标注 ...

  6. LINUX添加新的用户账号并赋予root权限

    一:添加新的用户账号使用 useradd 命令 语法:     useradd  选项  用户名 示例:      # 添加用户,设定登录目录:useradd -d  /home/admin -m a ...

  7. mac更新后,xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    解决方案: xcode-select --install

  8. Centos安装elasticsearch,php连接使用

    一.下载安装JAVA 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  9. 【Python基础】15_Python中的变量-进阶

    1.引用的概念 2.可变类型和不可变类型 3.哈希

  10. 怎样理解AJAX

    AJAX: Asynchronous JavaScript and XML, 翻译过来就是: 异步的JavaScript与XML 这已经成为了一个通用名词, 字面意义已经消失了, 因为现在使用Java ...