Discipntion

Let's call an array a of size n coprime iff gcd(a1, a2, ..., an) = 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 ≤ aj ≤ 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 bi 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).

Example

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.

一开始没有想到题目求b的整体性,,,直接用了一个简单的容斥,,,然后就T了。。

如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define ll long long
#define ha 1000000007
#define maxn 2000005
using namespace std;
int ans[maxn],n,k,tot; inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} inline void get(int x){
ans[x]=-ksm(x,n);
for(int i=2,j,now;i<=x;i=j+1){
now=x/i,j=x/now;
ans[x]=((ll)ans[x]+(ll)(j-i+1)*(ll)ans[now])%ha;
} ans[x]=-ans[x];
if(ans[x]<0) ans[x]+=ha;
} int main(){
scanf("%d%d",&n,&k);
ans[1]=1;
for(int i=2;i<=k;i++) get(i); tot=0;
for(int i=1;i<=k;i++){
tot+=ans[i]^i;
while(tot>=ha) tot-=ha;
} printf("%d\n",tot);
return 0;
}

  

后来发现还是要上反演才能过啊hhhh。

当我们求b[t]的时候:

设g(x)为gcd是x的倍数的数组个数,
显然g(x)=(t/x)^n;
设f(x)为gcd==x的数组个数,
显然g(x)=Σf(x*i)
所以=> f(x)=Σg(x*i)*μ(i)

我们求的显然是f(1),
所以b[t]=Σg(i)*μ(i)=Σ μ(i) * (t/i)^n

我们考虑通过b[t-1]推b[t],发现只有i是t的约数时(t/i)变化了,且都是+1;而对于其他的i,(t/i)不变。

所以我们可以先预处理出b[]的差分(可以做到调和级数的N ln N,通过枚举i,但前提是你得先预处理出(1-k)的n次方最好再把次方的差分也一起算出来)。

最后求差分的前缀和就是b[]了。

正解如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define ll long long
#define ha 1000000007
#define maxn 2000005
using namespace std;
int ans[maxn],n,k,tot;
int ci[maxn],u[maxn];
int zs[1000000],t=0;
bool v[maxn]; inline void init(){
u[1]=1;
for(int i=2;i<=2000000;i++){
if(!v[i]) zs[++t]=i,u[i]=-1;
for(int j=1,w;j<=t&&(w=zs[j]*i)<=2000000;j++){
v[w]=1;
if(!(i%zs[j])) break;
u[w]=-u[i];
}
}
} inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} int main(){
init(); scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++) ci[i]=ksm(i,n);
for(int i=k;i;i--){
ci[i]-=ci[i-1];
if(ci[i]<0) ci[i]+=ha;
} for(int i=1;i<=k;i++)
for(int j=i;j<=k;j+=i){
ans[j]+=u[i]*ci[j/i];
if(ans[j]<0) ans[j]+=ha;
else if(ans[j]>=ha) ans[j]-=ha;
} for(int i=1;i<=k;i++){
ans[i]+=ans[i-1];
if(ans[i]>=ha) ans[i]-=ha; tot+=ans[i]^i;
while(tot>=ha) tot-=ha;
} printf("%d\n",tot);
return 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 莫比乌斯反演,前缀和,差分

    Coprime Arrays CodeForces - 915G Let's call an array a of size n coprime iff gcd(a1, a2, ..., *a**n) ...

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

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

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

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

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

  6. [codeforces 549]G. Happy Line

    [codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...

  7. CodeForces 794 G.Replace All

    CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...

  8. Codeforces 1207 G. Indie Album

    Codeforces 1207 G. Indie Album 解题思路 离线下来用SAM或者AC自动机就是一个单点加子树求和,套个树状数组就好了,因为这个题广义SAM不能存在 \(len[u] = l ...

  9. Coprime Arrays CodeForces - 915G (数论水题)

    反演一下可以得到$b_i=\sum\limits_{d=1}^i{\mu(i)(\lfloor \frac{i}{d} \rfloor})^n$ 整除分块的话会T, 可以维护一个差分, 优化到$O(n ...

随机推荐

  1. ldconfig用法小记

    By francis_hao    Aug 4,2017   ldconfig:配置运行时动态链接库 概述 /sbin/ldconfig [ -nNvXV ] [ -f conf ] [ -C cac ...

  2. mapreduce出现大量task被KILLED_UNCLEAN的3个原因

    Request received to kill task 'attempt_201411191723_2827635_r_000009_0' by user ------- Task has bee ...

  3. jw player学习笔记三---发布到其它网站

    一.通过官网发布向导 登陆 http://www.longtailvideo.com,注册一个账号,进入你的用户管理中心,如下图 然后按提示,一步步操作,就会得到js代码了. 二.自己服务器发布 &l ...

  4. -webkit-overflow-scrolling:touch;

    -webkit-overflow-scrolling建了带有硬件加速的系统级控件,所以效率很高.但是这相对是耗更多内存的,最好在产生了非常大面积的overflow时才应用. 而且在 ios8  里有b ...

  5. Lesson 3

    1.关于面向对象的三个重要属性  Encapsulation(封装):无法直接访问类的成员变量,而是通过一些get set方法,间接访问数据域: Polymorphism(多态):静态绑定,动态绑定, ...

  6. python 写 excel 模块 : xlwt

    主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python ...

  7. Linux服务器中毒事件(libudev.so)

    今天机房管理人员反馈公司的某台服务器在防火墙上的连接数超限,登陆服务器时发现非常卡顿,远程登录后查看,CPU持续100%,且有一长度为10的随机字符串进程,kill掉,会重新生成另外长度为10的字符串 ...

  8. Spring容器整合WebSocket

    原链接:http://blog.csdn.net/canot/article/details/52575054 WebSocker是一个保持web客户端与服务器长链接的技术.这样在两者通信过程中如果服 ...

  9. codevs 1085

    数字游戏 2003年NOIP全国联赛普及组 时间限制: s 空间限制: KB 题目等级 : 黄金 Gold 题解 题目描述 Description 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但 ...

  10. Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状态改变 绘图设备(QPixmap,QImage,QBitmap,QPict ...