博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
欧拉函数
阅读量:3954 次
发布时间:2019-05-24

本文共 2024 字,大约阅读时间需要 6 分钟。

The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)

Input

There are several test cases. Each line has two integers a, b (2<a<b<3000000).

Output

Output the result of (a)+ (a+1)+....+ (b)

Sample Input

3 100

Sample Output

3042

【题意】

给定区间a,b要求a到b的欧拉函数对应的值的和

直接上模板

#include
#include
#include
#include
typedef long long ll;using namespace std;const int size=3000000;ll euler[size];void init(){ memset(euler,0,sizeof(euler)); euler[1]=1; for(ll i=2;i<=size;i++) { if(euler[i]==0) { for(ll j=i;j<=size;j+=i) { if(euler[j]==0) { euler[j]=j; } euler[j]=euler[j]/i*(i-1); } } }}int main(){ init(); ios::sync_with_stdio(false); ll a,b; while(cin>>a>>b) { ll sum=0; for(ll i=a;i<=b;i++) sum+=euler[i]; cout<
<

后看到网上有人用前缀和做:

#include
#include
#include
#include
#define ll long longusing namespace std;const int size=3000000;ll euler[size+10];void init(){ memset(euler,0,sizeof(euler)); euler[1]=1; for(ll i=2;i<=size;i++) { if(euler[i]==0) { for(ll j=i;j<=size;j+=i) { if(euler[j]==0) { euler[j]=j; } euler[j]=euler[j]/i*(i-1); //先进行除法是为了防止中间数据的溢出 } } } for(ll i=3;i<=size;i++) //前缀和 { euler[i]+=euler[i-1]; }}int main(){ init(); //初始化,首先求出每个数对应的质因数个数 ll a,b; while(~scanf("%lld%lld",&a,&b)) { printf("%lld\n",euler[b]-euler[a-1]); } return 0;}

 

转载地址:http://epyzi.baihongyu.com/

你可能感兴趣的文章
"巧"仿蚂蚁森林水滴动效
查看>>
用算法撩妹都不会,别跟我说你是程序员
查看>>
“揭秘”大数据的10个神话!
查看>>
《中国区块链行业发展报告2018》全文发布!
查看>>
高盛发布区块链报告:从理论到实践(中文版)
查看>>
用Python从零开始创建区块链
查看>>
使用 Charles 抓取 app 数据包
查看>>
分析千万条数据后,终于找到了北上广深租金最低的地铁房
查看>>
dfrobot红外激光测距传感器的精度,测量距离和应用场景
查看>>
arduino扩展板引脚和连接图
查看>>
DFRduino Nano4.0介绍及原理图
查看>>
linux板级内存管理之-物理内存描述的两种实现方法
查看>>
App 调试的几个命令实践
查看>>
“独裁”的张小龙和他的微信帝国诞生记
查看>>
linux-arm中断系统之GIC
查看>>
Linux time subsystem 详解(1) ----概述
查看>>
大牛很通俗地介绍《信号与系统》
查看>>
执行程序(例如UltraEdit)在WIN7下添加到右键菜单
查看>>
flash and root your Nexus10
查看>>
深入学习Make命令和Makefile(上)(2)
查看>>