HDU2824 The Euler function
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
Sample Output
Source
算出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的更多相关文章
- hdu2824 The Euler function(欧拉函数个数)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...
- HDU2824 The Euler function(欧拉函数)
题目求φ(a)+φ(a+1)+...+φ(b-1)+φ(b). 用欧拉筛选法O(n)计算出n以内的φ值,存个前缀和即可. φ(p)=p-1(p是质数),小于这个质数且与其互质的个数就是p-1: φ(p ...
- The Euler function[HDU2824]
The Euler functionTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- The Euler function(欧拉函数)
The Euler function Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- hdu 2824 The Euler function
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 2824 The Euler function(欧拉函数)
题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...
- The Euler function(线性筛欧拉函数)
/* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...
- hdu 2824 The Euler function 欧拉函数打表
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2824 The Euler function --------欧拉模板
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- Python中的文件和目录操作实现
Python中的文件和目录操作实现 对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数.重要的是,这 ...
- Java8 新API读取文件内容
import java.io.IOException;import java.nio.charset.Charset;import java.nio.file.Files;import java.ni ...
- Linux 安装github并配置ssh
首先,你得有个github帐号. 1.用apt-get install git的方式安装git test@er:/$ sudo add-apt-repository ppa:git-core/ppa ...
- P1021 邮票面值设计
P1021 邮票面值设计 题目描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大值MAX,使在1-MAX ...
- webstorm git提交不成功的
git pull git pull origin master git pull origin master --allow-unrelated-histories
- mysql用root账户建立用户和赋予权限
1.创建用户 create user guest_test@localhost identified by "root";-- 创建名为guest_test的用户 2.赋予权限 - ...
- sharePreference的几个重点
一. SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中,文件存放在/data/data/&l ...
- react生命周期方法
Mounting阶段,当一个组件的实例被创建并插入到DOM中时,下面这些函数会被调用: constructor() componentWillMount:组件即将被渲染到页面上,render之前最后一 ...
- Linux学习前的准备
Linux学习前的准备 Linux的学习也是这次项目的一个点. 我的学习教材是 The Linux Command Line,这是本已经被翻译好了的 中英版本的教材,知识点还是比较入门的 我使用的是 ...
- 想了一天的题目QAQ 毛线数列的最值
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #i ...