SPOJ:NT Games(欧拉函数)
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(欧拉函数)的更多相关文章
- 【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 ...
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- 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 ...
- 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举
1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...
- 【SPOJ-GCDEX】GCD Extreme(欧拉函数)
题目: SPOJ-GCDEX (洛谷 Remote Judge) 分析: 求: \[\sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j)\] 这道题给同届新生讲过,由于种种原因 ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- COGS2531. [HZOI 2016]函数的美 打表+欧拉函数
题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...
随机推荐
- 【CSS】常见问题集锦
position=absolute 时,定位的父元素变成了body而不是父div?原因:如果父div的position为非static,则相对父div.参考:http://www.jianshu.co ...
- 生产环境下lnmp的权限说明
https://www.cnblogs.com/zrp2013/p/4183546.html 有关权限说明:-rwxrw-r‐-1 root root 1213 Feb 2 09:39 50.html ...
- [luoguP2015] 二叉苹果树(DP)
传送门 貌似是个树形背包... 好像吧.. f[i][j]表示节点i选条边的最优解 #include <cstdio> #include <cstring> #include ...
- bzoj 3786 星系探索 dfs+splay
[BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...
- 约分差束 例题 ZOJ 2770 火烧连营
题目来源:ZOJ Monthly, October 2006, ZOJ2770题目描述:大家都知道,三国时期,蜀国刘备被吴国大都督陆逊打败了.刘备失败的原因是刘备的错误决策.他把军队分成几十个大营,每 ...
- oracle 启动监听报错TNS-12547: TNS:lost contact
https://blog.csdn.net/liqfyiyi/article/details/7534018
- Django学习之 - 基础视图函数
视图:Views 获取用户请求的方法: 1: request.GET 2: request.POST 3: request.FILES # checkbox 等多选文件 4:request.POST. ...
- 通过socket过去本地ip,port和远端ip,port
struct sockaddr addr;struct sockaddr_in* addr_v4;int addr_len = sizeof(addr); //获取local ip and portZ ...
- atomic原子操作
C++中对共享数据的存取在并发条件下可能会引起data race的未定义行为,需要限制并发程序以某种特定的顺序执行,有两种方式:1.使用mutex保护共享数据: 2.原子操作 原子操作:针对原子类型操 ...
- Android Activity与远程Service的通信学习总结
当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...