【PE512】Sums of totients of powers(欧拉函数)
大致题意: 求\(\sum_{n=1}^{5\times10^8}((\sum_{i=1}^n\phi(n^i))(mod\ n+1))\)。
大力推式子
单独考虑\((\sum_{i=1}^n\phi(n^i))(mod\ n+1)\)。
由于\(\phi\)有一个显然的性质:
\]
所以上面的式子就可以推成:
\]
又由于\(n\equiv-1(mod\ n+1)\),所以上式即为:
\]
观察\(\sum_{i=1}^n(-1)^{i-1}\)可知,这个式子在\(n\)为奇数时为\(1\),\(n\)为偶数时为\(0\)。
而显然\(\phi(n)<n<n+1\),所以最后我们要求的就是\(1\sim5*10^8\)内所有奇数的\(\phi\)值之和。
注意开数组
注意到一点,\(5\times10^8\)的数组即使在本地也是开不下的。
怎么办?杜教筛。
好吧,实际上可以不用杜教筛。
考虑到我们只需要奇数的\(\phi\)值,而\(\phi\)是一个积性函数,显然我们不可能从偶数的\(\phi\)值转移得出奇数的\(\phi\)值,因此筛偶数是不必要的。
这样一来,对于一个奇数\(x\),我们用数组第\(\frac{x+1}2\)位去存储它,就实现了数组大小减半,开得下了。
代码
#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 500000000
#define LL long long
using namespace std;
class LinearSiever//线性筛
{
private:
#define LS 250000000
#define PS 15000000
int Pt,P[PS+5];bool vis[LS+5];
public:
int phi[LS+5];//存储phi值
I void Sieve(CI S)
{
RI i,j;for(phi[1]=1,i=3;i<=S;i+=2)//与普通线性筛几乎无异,但注意下标变化
{
!vis[i+1>>1]&&(P[++Pt]=i,phi[i+1>>1]=i-1);
for(j=1;j<=Pt&&1LL*i*P[j]<=S;++j)
if(vis[i*P[j]+1>>1]=1,i%P[j]) phi[i*P[j]+1>>1]=phi[i+1>>1]*(P[j]-1);
else {phi[i*P[j]+1>>1]=phi[i+1>>1]*P[j];break;}
}
}
}L;
int main()
{
RI i;LL ans=0;for(L.Sieve(N),i=1;i<=(N+1>>1);++i) ans+=L.phi[i];//统计答案
return printf("%lld",ans),0;//输出答案
}
运行结果
50660591862310323
【PE512】Sums of totients of powers(欧拉函数)的更多相关文章
- POJ1284 Primitive Roots [欧拉函数,原根]
题目传送门 Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5434 Accepted: ...
- 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 ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- 51Nod-1136 欧拉函数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...
- 欧拉函数 - HDU1286
欧拉函数的作用: 有[1,2.....n]这样一个集合,f(n)=这个集合中与n互质的元素的个数.欧拉函数描述了一些列与这个f(n)有关的一些性质,如下: 1.令p为一个素数,n = p ^ k,则 ...
- FZU 1759 欧拉函数 降幂公式
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- OpenCV 相机标定 findChessboardCorners() 与 cornerSubPix() 函数
OpenCV 官方文档 findChessboardCorners():Finds the positions of internal corners of the chessboard. bool ...
- Image-transpose
import Image im=Image.open('test.jpg') #out = im.resize((128, 128),Image.BILINEAR) #改变大小 #out = im.r ...
- python生成多维数组方法总结(多维创建有问题的情况)
1.一维 list1=[]* #[,,,,] list2=np.arange() #[,,,,] 2.二维(注意) list2=[[]*]* 这种创建是有问题的!! print(list2)list2 ...
- MySQL使用crontab定时备份不执行问题
在使用crontab定时备份数据库时,发现并没有执行备份命令. 下面是定时备份的代码: 30 1 * * * /usr/local/mysql/bin/mysqldump --defaults-ext ...
- 架构设计系列-前端模式的后端(BFF)翻译PhilCalçado
本文翻译自PhilCalçado的官网:https://philcalcado.com/2015/09/18/the_back_end_for_front_end_pattern_bff.html 对 ...
- java的递归异常—一个异常可能由另一个异常触发
关键字: Caused by nested exception java.lang.reflect.InvocationTargetException: null at sun.reflect.Nat ...
- python随机选取目录下的若干个文件
个人记录用. python模块random argparse shutil import argparse parser = argparse.ArgumentParser() parser.add_ ...
- Python常用代码,置顶备用!
1.jupyter notebook 设置全部行输出: # 设置全部行输出 from IPython.core.interactiveshell import InteractiveShellInte ...
- Asp.Net MVC强类型页面获取值几种方式
方式一 (V:视图) @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="v ...
- 如何在Ubuntu编译ice
摘要:所有操作都在root用户下执行 第一步:下载ice最新版本,以3.7版本为例 第二步:安装ICE依赖包里面的三方库 #apt-get install libmcpp-dev;apt-get in ...