题目链接: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. java中的BigDecimal和String的相互转换

    /*由数字字符串构造BigDecimal的方法 02.*设置BigDecimal的小数位数的方法 03.*/ 04.import java.math.BigDecimal; 05.//数字字符串 06 ...

  2. springmvc处理ajax请求

    1.controller将数据封装成json格式返回页面 @RequestMapping("/dataList") public void datalist(CsoftCunsto ...

  3. 对linux的根目录执行强制递归移除

    开始开始时使用: #rm -f -r / 提示对根目录使用递归操作很危险,然后就没执行成功,让使用 --no-preserve-root 这个参数. 好吧,反正是虚拟机 于是执行: #rm -f -r ...

  4. 关于科台斯k97gprs调试记录(1)

    模块调试 1.gprs模块了解 用流量上网的模块,可以发短信,打电话. 2.AT指令的学习 AT+UART=波特率,流控位,数据位长度,校验控制,停止位长度 AT+NET=TCP/UDP 选择,APN ...

  5. 【crunch bang】程序中文化

    在应用程序中配置使用中文显示. # apt-get install locales # dpkg-reconfigure locales 安装文泉驿-微米黑字体: sudo apt-get insta ...

  6. HorizontalScrollView水平滑动

    xml布局 <HorizontalScrollView            android:id="@+id/hsv"            android:layout_ ...

  7. android 学习随笔七(网络:图片及文本传输及线程关系 )

    主线程.子线程.UI的关系 简单的HTTP请求 -------------------------------------------------------- public class MainAc ...

  8. 【python cookbook】【数据结构与算法】10.从序列中移除重复项且保持元素间顺序不变

    问题:从序列中移除重复的元素,但仍然保持剩下的元素顺序不变 解决方案: 1.如果序列中的值时可哈希(hashable)的,可以通过使用集合和生成器解决.

  9. iOS 学习笔记 二 (2015.02.26)

    How To Use Git Source Control with Xcode in iOS 6 If you're new here, you may want to subscribe to m ...

  10. 将UTF8编码的3字节中英文转成2字节中英文.

    1. 首先要考虑将String转成一个bytes的数组, 每个汉字是3个bytes, 英文或者标点是1个byte. 2. 然后去判断一下每一个byte的前面几个bit, 看下面的表, 1个byte的字 ...