Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Description

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
 

Source

2009 Multi-University Training Contest 1 - Host by TJU

算出300w个范围内的欧拉函数,然后求个前缀和。

然而这题内存限制好小,直接套惯用的模板会MLE。

已经没有什么优化的空间了,只好重构代码。

解1:MLE

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mxn=;
LL phi[mxn],pr[mxn>>];
int cnt=;
int mark[mxn];
int n;
void euler(){
phi[]=;
for(int i=;i<=mxn;i++){
if(!mark[i])
pr[++cnt]=mark[i]=i,phi[i]=i-;
for(int j=;j<=cnt && pr[j]*i<mxn;j++){
mark[i*pr[j]]=pr[j];
if(mark[i]==pr[j]){
phi[pr[j]*i]=phi[i]*pr[j];
break;
}
else phi[i*pr[j]]=phi[i]*(pr[j]-);
}
}
return;
}
int main(){
int s,t;
euler();
int i;
for(i=;i<=mxn;i++)phi[i]+=phi[i-];
while(scanf("%d%d",&s,&t)!=EOF)printf("%I64d\n",phi[t]-phi[s-]);
return ;
}

解2:AC

 #include <cstdio>
long long n=,phi[],p[],top=;
bool ma[];
void init()
{
phi[]=;
for (int i=;i<=n;++i)
{
if (!ma[i]) ma[i]=true,p[++top]=i,phi[i]=i-;
for (int j=;j<=top&&i*p[j]<=n;++j)
{
ma[i*p[j]]=true;
if (i%p[j]==){phi[i*p[j]]=phi[i]*p[j];break;}
else phi[i*p[j]]=(p[j]-)*phi[i];
}
}
}
int main()
{
int l,r; init();
for (int i=;i<=n;++i) phi[i]+=phi[i-];
while (~scanf("%d%d",&l,&r)) printf("%lld\n",phi[r]-phi[l-]);
}

HDU2824 The Euler function的更多相关文章

  1. hdu2824 The Euler function(欧拉函数个数)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...

  2. HDU2824 The Euler function(欧拉函数)

    题目求φ(a)+φ(a+1)+...+φ(b-1)+φ(b). 用欧拉筛选法O(n)计算出n以内的φ值,存个前缀和即可. φ(p)=p-1(p是质数),小于这个质数且与其互质的个数就是p-1: φ(p ...

  3. The Euler function[HDU2824]

    The Euler functionTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...

  4. The Euler function(欧拉函数)

    The Euler function Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  5. hdu 2824 The Euler function

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. hdu 2824 The Euler function(欧拉函数)

    题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...

  7. The Euler function(线性筛欧拉函数)

    /* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...

  8. hdu 2824 The Euler function 欧拉函数打表

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU 2824 The Euler function --------欧拉模板

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. <Docker学习>5. docker数据管理

    当我们创建了一个tomcat容器,如何简单部署一个web应用?如何将war包放入到容器中?也就是说怎么样把文件从宿主机中 "放入" 到容器中? docker cp命令可以将宿主机本 ...

  2. python协程和IO多路复用

     协程介绍                                                                                                ...

  3. android stadio open recent 在同一窗口打开

    Android staido 有一个功能是open recent ,默认是下面这样的: 就出来一个框,给你选择,是在新的窗口打开,还是在当前窗口打开.如果你选了当前窗口,并且点了Remember,do ...

  4. Entity FrameWork和Dapper的使用

    EF是微软系列下的更正苗红的重量级的ORM框架,功能强大,操作数据库的时候几乎不用写sql,可以像写C#代码一样操作数据库,尤其支持多表关联操作的时候极为方便,但是生成的sql语句性能很差,实在不敢恭 ...

  5. CSS3 loading效果全

    梦想天空 关注前端开发技术 ◆ 推动 HTML5 & CSS3 技术发展 ◆ 本博客全新站点:yyyweb.com 欢迎围观:) 首页 管理 订阅 网页设计 JavaScript jQuery ...

  6. Result Maps collection does not contain value for XXXXX

    在做mybatis多表查询的时候,出现了下面的错误: java.lang.IllegalArgumentException: Result Maps collection does not conta ...

  7. laravel5.5用户认证源码分析

    目录 1. 生成相关文件和配置 2. 分析路由文件 3. 以登陆开始为例,分析auth到底是怎么工作的 3.1 分析登录文件 3.2 分析门面Auth. 1. 生成相关文件和配置 快速生成命令 php ...

  8. Github前端项目排名

      Github前端项目排名(2016-04-04) 一.前言 近几年前端技术日新月异,从 RequireJS 到 AngularJS 再到 React,似乎每天都有新的技术诞生.而大神们总能第一时间 ...

  9. js点击重置按钮重置表单

    <html><head><script type="text/javascript">function formReset(){document ...

  10. 抓包工具 - Fiddler - (三)

    <转载自 miantest> 我们知道Fiddler是位于客户端和服务器之间的代理,它能够记录客户端和服务器之间的所有 HTTP请求,可以针对特定的HTTP请求,分析请求数据.设置断点.调 ...