POJ 2407 Relatives (欧拉函数)
2016-02-02 16:43
204 查看
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
Sample Output
6
4
题意:小于n且与n互素的数的个数
思路:欧拉函数模板
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 100010
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
ll eular(ll n)
{
ll i,res=n;
for(i=2;i*i<=n;i++)
if(n%i==0)
{
res=res/i*(i-1);
while(n%i==0)
n/=i;
}
if(n>1)
res=res/n*(n-1);
return res;
}
int main()
{
ll n;
while(scanf("%I64d",&n)!=EOF,n)
{
printf("%d\n",eular(n));
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12847 | Accepted: 6337 |
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<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 100010
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
ll eular(ll n)
{
ll i,res=n;
for(i=2;i*i<=n;i++)
if(n%i==0)
{
res=res/i*(i-1);
while(n%i==0)
n/=i;
}
if(n>1)
res=res/n*(n-1);
return res;
}
int main()
{
ll n;
while(scanf("%I64d",&n)!=EOF,n)
{
printf("%d\n",eular(n));
}
return 0;
}
相关文章推荐
- 5.6 建立逻辑表达式
- ecplise android 项目转AndroidStrudio 流程
- vs form设计器空白
- JS_call_APP native 与 html的交互
- 5.5 关于数据的问题
- java.lang.IllegalStateException: This Activity already has an action bar supplied by the window deco
- thinkphp3.2 验证码生成和刷新验证码
- 5.4 关于问题的问题
- 5.3 问题的种类
- Android开发环境、镜像服务地址、工具网址
- Light OJ 1033 Generating Palindromes (最长回文子串 区间DP)
- uvaoj-489:刽子手
- 自定义模块
- navicat 修改数据库字符集、字符排序规则、数据库引擎
- Android DiskLRUCache图片下载优化 <20>
- 第5章 关于问题
- canvas标签绘图基础
- xlslib在window下编译方法
- 第十章 会话管理——《跟我学Shiro》
- Hive学习笔记1:Hive创建外部表指向Hbase表时的汉字乱码问题