POJ 2407:Relatives(欧拉函数模板)
Relatives
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16186 | Accepted: 8208 |
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
Source
欧拉函数模板题
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int b[maxn];
//欧拉函数公式:
//φ(N)=N*(1-1/P1)*(1-1/P2)*...*(1-1/Pn).
int main(int argc, char const *argv[])
{
int n;
while(cin>>n&&n)
{
ms(b);
ll ans=n;
int vis=n;
for(int i=2;i*i<=vis;i++)
{
if(vis%i==0)
{
ans=ans/i*(i-1);
while(vis%i==0)
{
vis/=i;
}
}
}
if(vis>1)
ans=ans/vis*(vis-1);
cout<<ans<<endl;
}
return 0;
}
// 两种求欧拉函数的方法:
//
// ******************第一种直接求解****************
// long long Euler(long long x)
// {
// int res = x,a = x;
// for(int i=2;i*i<=a;i++)
// {
// if(a%i==0)
// {
// res = res/i*(i-1);//res -= res/i;
// while(a%i==0)a/=i;
// }
// }
// if(a>1)res =res/a*(a-1);//res -= res/a;
// return res;
// }
// ******************第二种打表求解****************
// #define Max 1000001
// int euler[Max];
// void Euler()
// {
// euler[1]=1;
// for(int i=2;i<Max;i++)
// euler[i]=i;
// for(int i=2;i<Max;i++)
// if(euler[i]==i)
// for(int j=i;j<Max;j+=i)
// euler[j]=euler[j]/i*(i-1);//先进行除法是为了防止中间数据的溢出
// }
POJ 2407:Relatives(欧拉函数模板)的更多相关文章
- POJ 2407 Relatives(欧拉函数)
题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...
- POJ 2407 Relatives 欧拉函数题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- POJ2407–Relatives(欧拉函数)
题目大意 给定一个正整数n,要求你求出所有小于n的正整数当中与n互质的数的个数 题解 欧拉函数模板题~~~因为n过大~~~所以直接用公式求 代码: #include<iostream> # ...
- UVA 10820 欧拉函数模板题
这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...
- hdu3501Calculation 2——欧拉函数模板
题目: Problem Description Given a positive integer N, your task is to calculate the sum of the positiv ...
- poj2407(欧拉函数模板)
sqrt(n)复杂度 欧拉函数模板 #include <iostream> #include <cstdio> #include <queue> #include ...
- 数论 - 欧拉函数模板题 --- poj 2407 : Relatives
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11372 Accepted: 5544 Descri ...
- 找新朋友 HDU - 1286 欧拉函数模板题
题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...
- POJ 2480 (约数+欧拉函数)
题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...
随机推荐
- 【Golang 接口自动化07】struct转map的三种方式
背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...
- Codeforces 38B - Chess
38B - Chess 思路:懂点象棋的规则就可以,看看哪些点可以放马. 代码: #include<bits/stdc++.h> using namespace std; #define ...
- Python爬虫Urllib库的高级用法
Python爬虫Urllib库的高级用法 设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Head ...
- 雷林鹏分享:C# 接口(Interface)
C# 接口(Interface) 接口定义了所有类继承接口时应遵循的语法合同.接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分. 接 ...
- Java 历史
James Gosling 最初开始 Java 语言项目是在 1991 年的 7 月.Java 被用在他的许多 set-top box 工程中.这个语言最开始的时候被称为 Oka,这个是因为 Jame ...
- thinkphp3.2 jquery ajax巧妙使用
1.做帐号管理的时候,我们去除一些重复的帐号是有必要的. 我使用的是jquery ajax 来和控制器进行传值.当我们跳转到我们要验证方法返回结果的时候,我们就可以使用php里的一个 0 为false ...
- hibernate中删除表遇到主键被外键引用违反完整约束条件不能删除的问题
MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHECKS ...
- iOS UI-团购案例(通过xib文件自定义UITableViewCell)
一.Model #import <Foundation/Foundation.h> @interface Goods : NSObject @property (nonatomic, co ...
- Linux,du、df统计磁盘情况不一致
转载:http://blog.linezing.com/?p=2136 在运维Linux服务器时,会碰到需要查看硬盘空间的情况,这时候,通常会使用df -lh命令来检查每个挂载了文件系统的硬盘的总量和 ...
- 模拟QQ分组
package com.lixu.fenzu; import java.util.ArrayList; import java.util.HashMap; import android.app.Lis ...