直接套模板,这道题貌似单独求还快一些

解法一

#include<cstdio>
#include<cctype>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std; typedef long long ll;
const int MAXN = 21234567;
int euler[MAXN]; void get_euler()
{
_for(i, 1, MAXN) euler[i] = i;
_for(i, 2, MAXN)
if(euler[i] == i)
for(int j = i; j <= MAXN; j += i)
euler[j] = euler[j] / i * (i - 1);
} void read(ll& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
} int main()
{
get_euler();
ll n, x; read(n);
while(n--)
{
read(x);
printf("%lld\n", euler[x]);
}
return 0;
}

解法二

#include<cstdio>
#include<cctype>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std; typedef long long ll;
const int MAXN = 21234567; ll euler(ll x)
{
ll ret = x;
for(int i = 2; i * i <= x; i++)
if(x % i == 0)
{
ret = ret / i * (i - 1);
while(x % i == 0) x /= i;
if(x == 1) break;
}
if(x > 1) ret = ret / x * (x - 1);
return ret;
} void read(ll& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
} int main()
{
ll n, x; read(n);
while(n--)
{
read(x);
printf("%lld\n", euler(x));
}
return 0;
}

caioj 1158 欧拉函数的更多相关文章

  1. caioj 1161 欧拉函数3:可见点数

    (x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...

  2. poj1284 && caioj 1159 欧拉函数1:原根

    这道题不知道这个定理很难做出来. 除非暴力找规律. 我原本找的时候出了问题 暴力找出的从13及以上的答案就有问题了 因为13的12次方会溢出 那么该怎么做? 快速幂派上用场. 把前几个素数的答案找出来 ...

  3. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  4. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  5. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  6. COGS2531. [HZOI 2016]函数的美 打表+欧拉函数

    题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...

  7. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  8. 51Nod-1136 欧拉函数

    51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...

  9. 欧拉函数 - HDU1286

    欧拉函数的作用: 有[1,2.....n]这样一个集合,f(n)=这个集合中与n互质的元素的个数.欧拉函数描述了一些列与这个f(n)有关的一些性质,如下: 1.令p为一个素数,n = p ^ k,则 ...

随机推荐

  1. Pyhton学习——Day54

    #Django内容回顾# -请求响应HTTP协议(有.无状态)默认传递的是字符串# 传递字符串分为两个部分:1.http1.1 GET /url /index + 请求头# Provisional h ...

  2. scrollBot滚动条美化,niceScroll有滚动条错位得问题

    http://www.htmleaf.com/Demo/201706204585.html

  3. node——Commonjs

    ECMA只规范了js最近的一些能力,并没有规范当要用js写一个庞大的项目如果使用模块化开发. CommonJS规范了当我们想开发大型程序的时候如何模块化来开发,以及模块化的时候,不同文件的通讯. Co ...

  4. mysql局域网服务搭建

    首先配置电脑Mysql环境变量 新建 Mysql服务配置 CMD >命令 : mysql -u root -p 4. 创建用户 Mysql 用户权限分配 5 重启服务 如果还不能访问就一定是你的 ...

  5. Vue系列(二):发送Ajax、JSONP请求、Vue生命周期及实例属性和方法、自定义指令与过渡

    上一篇:Vue系列(一):简介.起步.常用指令.事件和属性.模板.过滤器 一. 发送AJAX请求 1. 简介 vue本身不支持发送AJAX请求,需要使用vue-resource.axios等插件实现 ...

  6. 基于Quick_Thought Vectors的Sentence2Vec神经网络实现

    一.前言 1.Skip-Thought-Vector论文 https://github.com/ryankiros/skip-thoughts 2.本文假设读者已了解Skip-Gram-Vector和 ...

  7. WPF利用radiobutton制作菜单按钮

    原文:WPF利用radiobutton制作菜单按钮 版权声明:欢迎转载.转载请注明出处,谢谢 https://blog.csdn.net/wzcool273509239/article/details ...

  8. hdu4927 Series 1(组合+公式 Java大数高精度运算)

    题目链接: Series 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  9. Leetcode--easy系列4

    #58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...

  10. 【原创】Zend Framework 2框架之MVC

    ZendFramework 2框架之MVC 作者:sys(360电商技术组) 1.前言 Zend Framework 2是zend官方推出的php开源框架,基于php5.3.他全然採用面向对象的代码实 ...