题目链接: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. FOJ 2013 11 月赛

    这套题目还是比较吊的,由于我的沙茶,还是很多没有做出来- -! C:逆序数 D:呵呵 A:妈蛋,自己精度没弄好,想到之前GCC的要加eps,就WA了几次后交Visual C++过了!C(n,m)p^m ...

  2. ui方案设计

    最近在做一个ui,前端,为了引出创意,万事开头难,记录一下: 算是单页吧,一般都是左侧是导航,右边是内容,单页的话,要记录标签关闭,窗口区域. 操作的连惯性等.UI设计是个细活,需要深加研究,以后补充 ...

  3. [转]Web程序员必须知道的 Console 对象里的九个方法

    一.显示信息的命令 01 1: <!DOCTYPE html> 02  2: <html> 03  3: <head> 04  4:     <title&g ...

  4. springday02-go2

    1.复制xml文件到container.auto下2.Waiter类实现构造函数3.Bar类中Waiter作为其成员变量,并实现其get/set方法,有参和无参构造器,toString方法4.分别修改 ...

  5. mesos资源动态分配测试

    测试集群配置 60CPU,320G RAM 测试内容:先运行一个需要60CPU的Application1,再运行一个需要50CPU的Application2. 结果分析 如果要等Application ...

  6. 去掉list重复值

    /** * 去掉list重复值 */ public List<String> removeDuplicate(List<String> list) { HashSet<S ...

  7. jquery遍历对象,数组,集合

    1.jquery 遍历对象 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTM ...

  8. windows下pip升级到8.1.2

    升级pip只要切换到easy_install-3.5目录下:  easy_install-3.5 pip==8.1.2

  9. scrum站立会议------10.20

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app(约吧--暂定) 1.任务进度 2.燃尽图

  10. mtk的安卓手机刷机时出现的错误信息

    手机已成砖,用过好多工具都没刷回来,以下是用smart phone flash tool刷机时出现的错误信息 ---------------------------Smart Phone Flash ...