题目链接: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. zjuoj 3609 Modular Inverse

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...

  2. 结构体. ->操作符的内涵

    实质上就是结构体成员相对于结构体大变量的偏移地址, 操作符所干的事情就是寻址.是偏移,是偏移,是偏移.偏移后的地址

  3. cocos2d对动画的各种操作

    瞬时动作:瞬时动作的基类是InstantAction 1.放置位置   CGPoint p = ccp(width,height); [sprite runAction:[CCPlace action ...

  4. C# 多线程 lock 实例

    class Program { static void Main(string[] args) { //在t1线程中调用LockMe,并将deadlock设为true(将出现死锁) int i = 1 ...

  5. sql 中延迟执行

    SQL有定时执行的语句WaitFor. 语法格式:waitfor {delay 'time'|time 'time'} delay后面的时间是需要延迟多长时间后执行. time后面的时间是指定何时执行 ...

  6. PAT乙级 1003. 我要通过!(20)

    答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1. ...

  7. mysql 管理工具

    摘自: http://www.chinaz.com/free/2009/0306/68691.shtml MySQL是一个非常流行的小型关系型数据库管理系统,2008年1月16号被Sun公司收购.目前 ...

  8. zw版【转发·台湾nvp系列Delphi例程】.NET调用HALCON COM控件内存释放模式

    zw版[转发·台湾nvp系列Delphi例程].NET调用HALCON COM控件内存释放模式 ------------------------------------方法一 :Imports Sys ...

  9. 绑定repeater时三目运算加特殊结果处理

    <%#((Convert.ToDouble().ToString() != ).ToString(%

  10. android 项目学习随笔十二(ListView加脚布局)

    1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...