U - Relatives(欧拉函数)
Description
Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.
Input
There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.
Output
For each test case there should be single line of output answering the question posed above.
Sample Input
7
12
0
Sample Output
6
4
解题思路:欧拉函数:求不大于n且与n互质的数的个数。

AC代码:
#include<bits/stdc++.h>
using namespace std;
int Euler(int x){
int r=x;
for(int i=;i*i<=x;i++){//由于任何一个合数都至少有一个不大于根号x的质因子,所以只需遍历到根号x即可
if(x%i==){
r=r/i*(i-);//欧拉函数的通式,先除后乘,避免数据溢出
while(x%i==)x/=i;//消除x中所有质因子i,直到不能被i整除
}
}
if(x>)r=r/x*(x-);//如果x大于1,说明还有一个质因子没有除掉
return r;//返回个数
}
int main(){
int n;
while(cin>>n&&n)
cout<<Euler(n)<<endl;
return ;
}
U - Relatives(欧拉函数)的更多相关文章
- poj2407 Relatives 欧拉函数基本应用
题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...
- POJ 2407 Relatives(欧拉函数)
题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...
- POJ2407–Relatives(欧拉函数)
题目大意 给定一个正整数n,要求你求出所有小于n的正整数当中与n互质的数的个数 题解 欧拉函数模板题~~~因为n过大~~~所以直接用公式求 代码: #include<iostream> # ...
- POJ 2407 Relatives 欧拉函数题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 数论 - 欧拉函数模板题 --- poj 2407 : Relatives
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11372 Accepted: 5544 Descri ...
- POJ 2407 Relatives(欧拉函数入门题)
Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...
- POJ 2407:Relatives(欧拉函数模板)
Relatives AC代码 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16186 Accept ...
- POJ 2407 Relatives 【欧拉函数】
裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i ...
- POJ2407 Relatives(欧拉函数)
题目问有多少个小于n的正整数与n互质. 这个可以用容斥原理来解HDU4135.事实上这道题就是求欧拉函数$φ(n)$. $$φ(n)=n(1-1/p_1)(1-1/p_2)\dots(1-1/p_m) ...
- 欧拉函数K - Relatives
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n). 特殊性质:当n为奇数时,φ(2n)=φ(n), φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..( ...
随机推荐
- Window Pains(poj 2585)
题意: 一个屏幕要同时打开9个窗口,每个窗口是2*2的矩阵,整个屏幕大小是9*9,每个窗口位置固定. 但是是否被激活(即完整显示出来)不确定. 给定屏幕状态,问是否可以实现显示. 分析:拓扑排序,把完 ...
- scp远程文件传输
第一次.提示下载公钥 [root@rhel5 ~]# scp install.log root@192.168.124.129:/tmp The authenticity of host '192.1 ...
- 洛谷——P2866 [USACO06NOV]糟糕的一天Bad Hair Day
https://www.luogu.org/problem/show?pid=2866 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are h ...
- c++ stl 使用汇总(string,vector,map,set)
1.string 1>substr(),截取字串的方法.返回一个从指定位置开始,并具有指定长度的子字符串.参数 start(必选),所需的子字符串的起始位置.字符串中第一个字符的索引为 0.le ...
- hadoop(1)入门
hadoop入门(一) 一.概述 1.什么是hadoop hadoop不仅是一个用于存储分布式文件系统,还是设计用来在有通用计算设备组成的大型集群上执行的分布式应用的基础框架. hadoop框架最 ...
- javascript 时间格式(很方便的原生函数)
原文:http://www.cnblogs.com/yjf512/p/3796229.html --------------------- <html xmlns="http://ww ...
- Dell PowerEdgeServerT110II USB Boot更新
可引导USB设备更新Dell PowerEdge服务器 当显示Boot Options(“启动选项”)时,选择option 1(选项 1)以开始固件更新. 现在正在加载的Linux发行版本 然后固件更 ...
- Linux下C编程的学习_1
0x0:为什么写这个系列的文章 博客原本的定位是安卓游戏的破解,可是为什么写这系列的文章呢? 由于在破解过程中,我们是无法避免来敲代码的,恢复算法,模拟算法,游戏中对数据的解密.游戏中对保存在clie ...
- Jmeter技巧
Jmeter参数化的4种方法 用Jmeter测试时包含两种情况的参数,一种是在url中,一种是请求中需要发送的参数. URL中的参数,如:http://blog.da-fang.com/index.p ...
- mac 查看python路径
1,terminal : input: which python 2, terminal: input : python --->import sys ----> print sys ...