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. Oracle中有关数学表达式的语法

    Oracle中有关数学表达式的语法 三角函数 SIN               ASIN              SINHCOS             ACOS           COSHTA ...

  2. java常见问题集锦

    Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案 Eclipse ...

  3. HDU1021-Fibonacci Again,,找规律就好了~~~

    Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 洛谷P1759 通天之潜水

    题目背景 直达通天路·小A历险记第三篇 题目描述 在猴王的帮助下,小A终于走出了这篇荒山,却发现一条波涛汹涌的河拦在了自己的面前.河面上并没有船,但好在小A有n个潜水工具.由于他还要背重重的背包,所以 ...

  5. Vim command handbook

    /* 本篇文章已经默认你通过了vimtuor训练并能熟练使用大部分命令.此篇文章主要是对于tutor命令的总结和梳理.适合边学习边记忆 tutor那个完全是在学习中记忆 符合认知规律但是练习有限.所以 ...

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

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

  7. Memcached 管理与监控工具 MemAdmin

    MemAdmin是一款可视化的Memcached管理与监控工具,基于 PHP5 & JQuery 开发,体积小,操作简单. 主要功能: 服务器参数监控:STATS.SETTINGS.ITEMS ...

  8. Java实验——输出一个数组里面连续子数组最大的和(二)文件操作

    在本周的练习中,主要是对上周的实验进行健壮性的完善,即在所能考虑到的情况之中,尽量使自己的程序能够正常地运行. 在上周的实验中,我已经是在编程过程中考虑到用户输入的错误类型的问题,所以这一方面并没有多 ...

  9. html5 拖拽元素

    利用html5实现元素的拖拽,让拖动元素在指定的容器中拖动. 注意点:1.被拖元素要设置拖动属性.draggable="true" 2.容器元素要设置阻止默认事件处理 实现效果图如 ...

  10. C#编程语言及.NET 平台快速入门指南

    github: https://github.com/mfjiang e-mail: hamlet.jiang@live.com   ⼀.C#,CLR,IL,JIT概念 以及 .NET 家族 (⼀)基 ...