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. IBM MQ 创建以及常见问题集锦

    消息队列+发送队列+消息通道 接收通道名称与发送端的发送通道名称要一致,修改通道信息后要执行 start channle(chlname) 重启通道.常用的MQ命令 66.0.42.240 用户 mq ...

  2. 日志不得应用情况切换强制standby改变状态为primary

    日志不得应用情况切换备库为主库 备库运行如下: alter database recover managed standby database disconnect from session; alt ...

  3. [luoguP2622] 关灯问题II(状压最短路)

    传送门 本以为是状压DP,但是有后效性. 所以写一手状压spfa #include <queue> #include <cstdio> #include <cstring ...

  4. BZOJ 1303: [CQOI2009]中位数图 【水题】

    给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Out ...

  5. 按WSDL信息手动生成代理类

    命令行: wsdl /language:c# /n:Entity /out:C:\Users\mengxianming\Desktop\Centrex_IMS_Client.cs C:\Users\m ...

  6. 跳石头(codevs 4768)

    题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有N ...

  7. Topcoder 2015_1C

    A:大水题; B:求一颗树中,有多少条路径 不存在路径中一点是另外一点的祖先,(后面废话说了很多) 其实一个点 可以到它本身也可以是一条路径结论是:统计叶子的节点.(真简单粗暴 C:题目不说,说也说不 ...

  8. Spring Boot使用MyBatis 3打印SQL的配置

    普通Spring下的XML文件配置: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE co ...

  9. 搭建网络svn实战

    工作中的问题(7) 转自:http://blog.csdn.net/xiaoting451292510/article/details/8562570 经常性我们和朋友写一些程序,大家在不同的城市确有 ...

  10. 一起talk C栗子吧(第一百回:C语言实例--使用信号量进行进程间同步与相互排斥一)

    各位看官们.大家好,上一回中咱们说的是进程间同步与相互排斥的样例,这一回咱们说的样例是:使用信号量进行进程间同步与相互排斥. 闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,信号量是由著名 ...