Katniss Everdeen after participating in Hunger Games now wants to participate in NT Games (Number Theory Games).

As she begins President Snow provides her a number k. Then, she has to defend t back to back attacks from Haymitch Abernathy for practice. In each attack Haymitch Abernathy gives two numbers l and r, for defense she has to compute :

As she is new to number theory, help her by computing given expression.

Input Format

First line contain an integer, i.e. k.

Second line contain an integer, i.e. t.

Each of next t lines contain two integers, i.e. l & r.

Constraints

1<=k<=10^5

1<=t<=10^5

1<=l<=10^5

l<=r<=10^5

Output Format

For each attack output the value of expression.

Sample Input

1

1

1 5

Sample Output

26

Explanation : Just evaluate the expression.

题意: 求题意的区间的GCD^K之和模Mod

思路:利用前缀和思想+欧拉函数:

Σx  (GCD(i,j)==x,j>i),枚举X,然后枚举j,根据欧拉函数得到i的数量。

由于询问次数多,我们预处理出答案,预处理的时候,利用前缀和思想降低复杂度。

总的复杂度=N*(N/1+N/2+N/3+N/4+...N/N)=NlogN。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
const int Mod=1e9+;
ll phi[maxn+],p[maxn+],vis[maxn+];
ll ans[maxn+],K,T,L,R,cnt;
ll qpow(ll a,ll x){ ll res=; while(x){ if(x&) res=res*a%Mod; a=a*a%Mod; x>>=;} return res;}
void getphi()
{
for(ll i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i,phi[i]=i-;
for(ll j=;j<=cnt&&p[j]*i<=maxn;j++){
vis[i*p[j]]=;
phi[i*p[j]]=phi[i]*(p[j]-);
if(i%p[j]==){
phi[i*p[j]]=phi[i]*p[j];
break;
}
}
}
}
void solve()
{
for(ll i=;i<=maxn;i++) ans[i]=(ans[i]+qpow(i,K))%Mod;//自己
for(ll i=;i<=maxn;i++) ans[i]=(ans[i]+phi[i])%Mod;//
for(ll i=;i<=maxn;i++){
for(ll j=;j*i<=maxn;j++)
ans[i*j]=(ans[i*j]+qpow(i,K)*phi[j]%Mod)%Mod;
}
for(ll i=;i<=maxn;i++) ans[i]=(ans[i-]+ans[i])%Mod;
}
int main()
{
getphi();
scanf("%lld%lld",&K,&T);
solve();
while(T--){
scanf("%lld%lld",&L,&R);
printf("%lld\n",((ans[R]-ans[L-])%Mod+Mod)%Mod);
}
return ;
}

SPOJ:NT Games(欧拉函数)的更多相关文章

  1. 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数

    题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...

  2. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  3. SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1

    5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...

  4. 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举

    1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...

  5. 【SPOJ-GCDEX】GCD Extreme(欧拉函数)

    题目: SPOJ-GCDEX (洛谷 Remote Judge) 分析: 求: \[\sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j)\] 这道题给同届新生讲过,由于种种原因 ...

  6. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  7. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  8. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  9. COGS2531. [HZOI 2016]函数的美 打表+欧拉函数

    题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...

随机推荐

  1. python013 Python3 循环语句

    Python3 循环语句本章节将为大家介绍Python循环语句的使用.Python中的循环语句有 for 和 while.Python循环语句的控制结构图如下所示: while 循环Python中wh ...

  2. iOS第三方地图-百度地图中心点定位

    使用百度地图定位后,滑动地图,使用反编码确定地图中心店的位置信息 // // MapControl.m // quyizu // // Created by apple on 15/9/2. // C ...

  3. android GET 请求在5.0版本的取不到数据,报IO异常兼容问题解决

    使用lib类库xUtils-2.6.10.jar作为数据请求的框架,在android的5.0版会有兼容问题,取不到GET请求的数据. 但是POST没有问题,难取到数据. public static R ...

  4. 使用Sencha Architect开发Sencha Touch应用的整理

    官网:http://www.sencha.com/ 其实官网上的文档都很清楚了,不过整理一下总比较好 第一步,软件准备 注: 以下软件的安装本着这样两条原则 一是不要安装在中文目录下 二是不要安装在带 ...

  5. 选课(codevs 1378)

    题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...

  6. python监控tomcat日记文件

    最近写了一个用python监控tomcat日记文件的功能 实现的功能: 监控日记文件中实时过来的记录,统计每分钟各个接口调用次数,统计结果插入oracle #!/usr/bin/python # -* ...

  7. ajax请求数据之后在已经有的数据前面打对勾的方法

    今天遇到这么一个需求: 选择一部分人,在点击确定的时候添加到对应的div中,也就是添加到对应的表单下面,当再次查询的时候需要在已经选过的人的复选框前面打伤对勾.

  8. hdu 4923 Room and Moor [ 找规律 + 单调栈 ]

    传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. hdu 3943

    数位dp #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> ...

  10. Python常用的几种常用的内置函数

    abs(x)              用于返回绝对值 divmod(x,y)       函数中传入两个数字,返回的是x/y的一个结果的元组(商,余数) pow(x,y)            用于 ...