题目链接:http://poj.org/problem?id=2407

Relatives
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13599   Accepted: 6772

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

 
两道题差不多的结题思路,都是求一个数的互质的数的个数。用欧拉函数。
欧拉函数:
一个数x的素因数p1,p2,p3,......,那么他的欧拉函数就为x(1-1/p1)(1-1/p2)(1-1/p3)......
欧拉函数相当于一个筛选,找到一个素因数后,就将该素因子全部约掉。
然后Greater New York Regional 2015 (D)中,1是一个特例,他有0/1,1/1两个互质的数,所以ans[1] = 2;
然后再打表就可以了。
#include <stdio.h>

int Euler(int n)
{
int ans = n;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
n/=i;
ans = ans - ans/i; while(n%i==)
{
n/=i;
} }
}
if(n>) ans = ans - ans/n;
return ans;
} int main()
{
int n;
while(scanf("%d",&n),n)
{
printf("%d\n",Euler(n));
}
return ;
}
#include <stdio.h>

int Euler(int n)
{
int res = n;
for(int i=; i*i<=n; i++)
{
if(n%i==)
{
n=n/i;
res = res - res/i;
while(n%i==)
n/=i;
}
}
if(n>) res = res - res/n;
return res;
} int main()
{
int cases;
scanf("%d",&cases); int ans[];
ans[] = ;
for(int i=; i<=; i++)
{
ans[i]=ans[i-]+Euler(i);
}
while(cases--)
{
int t,k;
scanf("%d%d",&t,&k);
printf("%d %d\n",t,ans[k]);
}
return ;
}
 

Poj(2407),Greater New York Regional 2015 (D)的更多相关文章

  1. 【2011 Greater New York Regional 】Problem I :The Golden Ceiling

    一道比较简单但是繁琐的三维计算几何,找错误找的我好心酸,没想到就把一个变量给写错了 = =: 题目的意思是求平面切长方体的截面面积+正方体顶部所遮盖的面积: 找出所有的切点,然后二维凸包一下直接算面积 ...

  2. 【2011 Greater New York Regional 】Problem G: Rancher's Gift

    计算几何的题目,很简单: 自己随手敲了个,纪念下! #include<cstdio> #include<cmath> using namespace std; struct p ...

  3. 【2011 Greater New York Regional 】Problem H: Maximum in the Cycle of 1

    也是一个数学题: 主要用到的是排列组合的知识,推推公式就行了,挺简单的: 唯一要注意的是A(0,0)=1: 在这个上面WA了几次,= = 代码: #include<stdio.h> #de ...

  4. 【2011 Greater New York Regional 】Problem B The Rascal Triangle

    一个简单的规律题,每一列都是一个等差数列: 代码: #include<cstdio> #define ll long long using namespace std; int main( ...

  5. 75-ADMI,Average Directional Movement Index,平均方向性运动指标.(2015.7.1)

    ADMI,Average Directional Movement Index 平均方向性运动指标 Directional Movement Index,平均方向性运动指标.(2015.7.1)&qu ...

  6. POJ 2407 (欧拉函数)

    题目链接: http://poj.org/problem?id=2407 题目大意:求小于n且与n互质的正整数个数. 解题思路: 欧拉函数=小于n且与n互质的正整数个数. 公式=n*(1-1/P1)* ...

  7. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  8. POJ 2407 Relatives(欧拉函数)

    http://poj.org/problem?id=2407 题意: 给出一个n,求小于等于的n的数中与n互质的数有几个. 思路: 欧拉函数的作用就是用来求这个的. #include<iostr ...

  9. poj 2407 欧拉函数裸题

    http://poj.org/problem?id=2407 题意:多组数据,每次输入一个数 ,求这个数的欧拉函数 int euler_phi(int n){//单个欧拉函数 int m=(int)s ...

随机推荐

  1. 如何准确进行T+0操作

    如何准确进行T+0操作 http://tangulunjin.com.cn/t0.html 常用的几种T+0模式 1.受外盘或者个股利好高开,高开一般在10点之前会有个回抽昨日收盘价的下跌波.特别是弱 ...

  2. __int64和long long输入输出

    __int64 num; scanf("%I64d", &num); printf("%I64d\n", num); long long num; sc ...

  3. Excel操作增强包

    一.前期准备1)  pywin32安装包(根据系统要求选择32位装包还是64位安装包)2)  xlwings安装包二.安装过程1)  pywin32安装包为exe文件,直接进行安装即可2)  xlwi ...

  4. [原创]java WEB学习笔记56:Struts2学习之路---Struts 版本的 登录 demo

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Web Project配置Hirbernate

    1:首先找到hibernate-release-4.1.9.Final.zip\hibernate-release-4.1.9.Final\lib\required ,把required里的所有jar ...

  6. HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...

  7. dataset 使用

    下面有例子说明: 首先我们需要打开一个联结: string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/ ...

  8. ppaer 67 : matlab 函数errorbar

    MATLAB ERRORBAR 这个函数的意思是:ERRORBAR(X,Y,L,U),X是自变量,Y是因变量,L是Y的变动下限,U是Y的变动上限 errorbar(X,Y,E)  X是自变量,Y是因变 ...

  9. paper 13:计算机视觉研究群体及专家主页汇总

    做机器视觉和图像处理方面的研究工作,最重要的两个问题:其一是要把握住国际上最前沿的内容:其二是所作工作要具备很高的实用背景.解决第一个问题 的办法就是找出这个方向公认最高成就的几个超级专家(看看他们都 ...

  10. BETWEEN and

    select * from ( select *,ROW_NUMBER() over (ORDER BY AddTime desc) RowNumber from Product where ID n ...