题目问有多少个小于n的正整数与n互质。

这个可以用容斥原理来解HDU4135。事实上这道题就是求欧拉函数$φ(n)$。

$$φ(n)=n(1-1/p_1)(1-1/p_2)\dots(1-1/p_m)\tag{p为n的质因子}$$

这个通项公式可以通过容斥原理的解法来验证。那么利用这个通项就能在$O(\sqrt[]n)$下计算出φ(n)。

 #include<cstdio>
#include<cstring>
using namespace std;
int phi(int n){
int res=n;
for(int i=; i*i<=n; ++i){
if(n%i) continue;
while(n%i==) n/=i;
res-=res/i;
}
if(n!=) res-=res/n;
return res;
}
int main(){
int n;
while(~scanf("%d",&n) && n){
printf("%d\n",phi(n));
}
return ;
}

POJ2407 Relatives(欧拉函数)的更多相关文章

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

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

  2. POJ2407–Relatives(欧拉函数)

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

  3. poj2407(欧拉函数模板题)

    题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...

  4. POJ 2407 Relatives(欧拉函数)

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

  5. POJ 2407 Relatives 欧拉函数题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

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

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

  7. POJ2407(欧拉函数)

    Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13598   Accepted: 6771 Descri ...

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

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

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

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

随机推荐

  1. gen already exists but is not a source folder. Convert to a source folder or rename it.

    异常提示: gen already exists but is not a source folder. Convert to a source folder or rename it.   错误原因 ...

  2. 最长公共子序列 NYOJ37

    http://acm.nyist.net/JudgeOnline/problem.php?pid=37 先逆转原来的字符串,再用原来的字符串跟逆转后的字符串进行比较,求得的最长公共子序列就是回文串,也 ...

  3. 用Python操纵MySQL

    本例用Python操纵MySQL,从指定文件读取数据,并对数据进行处理,处理之后批量插入MySQL. 贴上代码: # -*- coding: gbk -*- import re import MySQ ...

  4. 【leetcode】3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  5. iOS CALayer动画中使用的3个tree

    在网上经常看到关于layer的tree的描述,不太理解,今天找到了官方文档,原文在Core Animation Programming Guide 中. Layer Trees Reflect Dif ...

  6. Java虚拟机支持的最大内存限制

    最近在开发Java的程序.本来我是一直很喜欢Java的内存管理的,不需要担心分配内存,只管分配,垃圾收集器自己会给你回收内存的.现在开发的程序数据量很大,为了速度快,我准备把所有的信息加载进内存,这样 ...

  7. ubuntu 下wireshark 软件安装与使用

    在ubuntu下,使用wireshark也是很有必要的.虽然可以使用tcpdump等工具. ubuntu:11.10     1. sudo apt-get install wireshark     ...

  8. struts2 标签问题----escape="false" 这个属性

    1.在编程过程中,会遇到这个动西,escape="false" eg: <s:fielderror escape="false"/>-------& ...

  9. .net学习笔记---HttpRuntime类

    HttpRuntime在ASP.NET处理请求中负责的是创建HttpContext对象以及调用HttpApplicationFactory创建HttpApplication. 其定义如下: publi ...

  10. Java中Json用法

    首先导入json.jar,非常简单看过代码就明白怎么用了 package cn.mylucene; import java.util.HashMap; import java.util.Map; im ...