欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。
特殊性质:当n为奇数时,φ(2n)=φ(n),
φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),
其中p1,p2,p3,p4,……pn为x的所有质因数,
原因:如果x = p^k,那么p的所有倍数与x都不互质,所以除了p的倍数外的数的个数就是x*(1-1/p)个
 
比如下面的题目:

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,输入以0,0结束
     找出比n小的数中与a互质的数的个数 用欧拉定理,任意一个数可以分解成小于等于它的质数的乘积,
如果n是质数: 那么比他小的数中和它互质的数的个数为n-1
如果n不是质数:n = p1^a1 * p2^a2 * p3^a3 * p4^a4 * …… *pn^an;
        那么n的欧拉函数值就等于pi^ai(1<=i<=n)的欧拉函数的积
        每一项的欧拉函数就等于pi^ai * (1-1/pi)化简后就是(pi-1)*pi^(n-1) 原来的解题思路一直超时,当时是按照常规方法来做,把,本来案例个数就很多很多,每个案例中的n还是10e9这么大的,一个一个找素数多麻烦,多浪费时间
欧拉函数就行了啊,欧拉好伟大,赞一个,如果我先学欧拉函数的话肯定不喜欢欧拉,整这么多东西,真难记,可是现在觉得方便多啦…… source
上代码:
#include <stdio.h>
using namespace std;
int eular(int n)
{
int ret = , i;
for(i = ; i * i <= n; i++)
{
if(n % i == )
{
n /= i;
ret *= i-;
}
while(n % i == )
{
n /= i;
ret *= i;
}
}
if(n > ) //如果n是质数的情况下
ret *= n-;
return ret;
}
int main()
{
int n;
while(scanf("%d", &n), n)
{
printf("%d\n", eular(n));
}
return ;
}

欧拉函数K - Relatives的更多相关文章

  1. 数论 - 欧拉函数模板题 --- poj 2407 : Relatives

    Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Descri ...

  2. poj2407 Relatives 欧拉函数基本应用

    题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...

  3. POJ 2407 Relatives(欧拉函数入门题)

    Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...

  4. POJ 2407:Relatives(欧拉函数模板)

    Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accept ...

  5. Relatives POJ - 2407 欧拉函数

    题意: 给你一个正整数n,问你在区间[1,n)中有多少数与n互质 题解: 1既不是合数也不是质数(1不是素数) 互质是公约数只有1的两个整数,叫做互质整数.公约数只有1的两个自然数,叫做互质自然数 所 ...

  6. POJ 2407 Relatives 【欧拉函数】

    裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i ...

  7. POJ2407 Relatives(欧拉函数)

    题目问有多少个小于n的正整数与n互质. 这个可以用容斥原理来解HDU4135.事实上这道题就是求欧拉函数$φ(n)$. $$φ(n)=n(1-1/p_1)(1-1/p_2)\dots(1-1/p_m) ...

  8. POJ 2407 Relatives(欧拉函数)

    题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...

  9. POJ2407–Relatives(欧拉函数)

    题目大意 给定一个正整数n,要求你求出所有小于n的正整数当中与n互质的数的个数 题解 欧拉函数模板题~~~因为n过大~~~所以直接用公式求 代码: #include<iostream> # ...

随机推荐

  1. UVa1585 Score

    #include <stdio.h> int main(){    int T, O, score;    char str[81], *p;    scanf("%d" ...

  2. c# zip file and folder programmatically

    In .net 4.5 Framework, we can zip a file by this way: private static string CompressFile(string sour ...

  3. HDU2795 billboard【转化为线段树。】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 hhanger大神的题目,水题都得有点思维. 题意:h*w的木板,放进一些1*L的物品,求每次放 ...

  4. 获取WebView里的网页文本内容

    获取WebView里的网页文本内容,能够採用例如以下方法: public class ComJSInterface { public void loadHtmlContent(String conte ...

  5. MessageDigest简单介绍

    本文博客原文 參考文章:http://blog.sina.com.cn/s/blog_4f36423201000c1e.html 一.概述 java.security.MessageDigest类用于 ...

  6. Java输入输出流(1)

    1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java全部的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...

  7. sql 时间和字符串 取到毫秒级

    (select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')+(Select ri ...

  8. 实现AJAX局部刷新以及PageMethod方法的使用

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. 获取图片中的文本--MODI

    http://www.aspsnippets.com/Articles/Read-Extract-Text-from-Image-OCR-in-ASPNet-using-C-and-VBNet.asp ...

  10. C# Coding & Naming Conventions

    Reference document https://msdn.microsoft.com/en-us/library/ff926074.aspx https://msdn.microsoft.com ...