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. 七牛云成功通过 CMMI3 认证

    10 月 31 日,在上海七牛信息技术有限公司青岛会议室举行的 CMMI3 级认证结果发布会上,主任评估师王庆付老师和评估组向公司高层及参与评审的 EPG 成员及项目组成员郑重宣布:经过严格的现场审核 ...

  2. LeetCode:不同路径&不同路径II

    不同路径一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 问 ...

  3. HDU 4641

    动态更新后缀自动机,每次不断依据当前添加的节点不断往前寻找父节点上字符串最多可出现的次数 这里为了减少运算,当父节点已经达到k次就不在往前寻找,因为之前的必然达到k次,也已经统计在内 #include ...

  4. vue.js基础知识总结

    初始化一个项目 npm init -y 安装一些依赖 npm install 名称 --save 例如 npm install vue axios bootstrap --save --save 表示 ...

  5. Spring Boot配置方式

    Spring提供了xml.注解.Java配置.groovy配置实现Bean的创建和注入. 配置元数据 无论xml配置.注解配置还是Java配置,都被称为配置元数据,所谓元数据即描述数据的数据.元数据本 ...

  6. Hybris Virtualjdbc Extension

    作者:Eason 编写日期:2018/07/31 联系方式:13920409462 1. Extension 说明 virtualjdbc extension 提供了虚拟JDBC驱动程序的实现. 通过 ...

  7. Python基础教程笔记——第6章:抽象(函数)

    (1)计算裴波那契数列: fbis=[0,1] num=int(input("please input the number")) for i in range(num-2): f ...

  8. Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]

    传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...

  9. 并发编程——IO模型

    前言 同步(synchronous):一个进程在执行某个任务时,另外一个进程必须等待其执行完毕,才能继续执行 #所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不会返回.按照这个定义, ...

  10. oracle alert 日志位置

    Oracle 11g Alert log 文件位置的问题 https://blog.csdn.net/yujin2010good/article/details/7690191 https://blo ...