Relatives
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15708   Accepted: 7966

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

题解:通过这道题了解到了欧拉函数

欧拉函数可以求出小于n的质因数,则这题可以通过公式φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数就是1本身)。来求解。例如:

12=2*2*3

那么φ(12)=12*(1-1/2)*(1-1/3)=4)

AC代码:

 1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4
5 int ans;
6
7 void solve(int x)
8 {
9 ans = x;
10 int ant = 0;
11 for(int i = 2; i*i < x; i++)
12 {
13 if(x%i == 0)
14 {
15 ans = ans / i * (i - 1);
16 }
17 while(x%i == 0) x /= i;
18 }
19
20 if(x > 1)
21 ans = ans/x*(x-1);
22 }
23
24 int main()
25 {
26 int n;
27 while(1)
28 {
29 cin >> n;
30 if(n == 0)
31 break;
32 solve(n);
33 cout << ans << endl;
34 }
35
36
37 return 0;
38 }

Relatives(容斥)的更多相关文章

  1. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

  2. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  3. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  4. 【BZOJ-4455】小星星 容斥 + 树形DP

    4455: [Zjoi2016]小星星 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 204  Solved: 137[Submit][Status] ...

  5. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. UVa12633 Super Rooks on Chessboard(容斥 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...

  7. PE-1 & 暴模|容斥

    题意: 求1000以下3或5的倍数之和. SOL: 暴模也是兹瓷的啊... 那么就想到了初赛悲催的滚粗...容斥忘了加上多减的数了... 然后对着题...T = 3*333*(1+333)/2 + 5 ...

  8. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  9. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  10. codeforces B. Friends and Presents(二分+容斥)

    题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值 ...

随机推荐

  1. linux的0,1,2号进程

    一.答案 https://blog.csdn.net/gatieme/article/details/51532804 仔细读一下作者的博客,都是操作系统底层相关. 二.补充: 1.linux代码,a ...

  2. js一周时间表

    <div class="datetext"> <img class="dateLeft" src="./images/dateLef ...

  3. js导出execl 兼容ie Chrome Firefox各种主流浏览器(js export execl)

    第一种导出table布局的表格 1 <html> 2 3 <head> 4 <meta charset="utf-8"> 5 <scrip ...

  4. CSS基础 和 font字体、背景属性连写 与 清除浮动方法

    1.伪类 1. :link 2. :visited 3. :hover(重要) 4. :active 5. :focus(input标签获取光标焦点) 2.伪元素 1.:first-letter 2. ...

  5. 如何实现一个简易版的 Spring - 如何实现 @Autowired 注解

    前言 本文是 如何实现一个简易版的 Spring 系列第四篇,在 上篇 介绍了 @Component 注解的实现,这篇再来看看在使用 Spring 框架开发中常用的 @Autowired 注入要如何实 ...

  6. freebsd升级时出错,没有ntp用户解决

    freebsd升级出错,没有ntp用户 终端执行命令 pw groupadd ntpd -g 123 pw useradd ntpd -u 123 -g ntpd -h - -d /var/db/nt ...

  7. Solon 框架详解(九)- 渲染控制之定制统一的接口输出

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  8. c++ 反汇编 继承

    单继承,父类中没有虚函数 单继承,父类存在虚函数,子类重写虚函数 单继承,父类存在虚函数,子类不新定义虚函数 单继承,父类存在虚函数,子类新定义虚函数 单继承,父类不存在虚函数,子类定义虚函数 多继承 ...

  9. java例题_06 最大公约数&最小公倍数

    1 /*6 [程序 6 求最大公约数及最小公倍数] 2 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数. 3 程序分析:利用辗除法. 4 */ 5 6 /*分析 7 * ======== ...

  10. CDN域名解析问题

    CDN域名解析问题 之前配置CDN域名解析,碰到一个配置带www的域名和不带www的域名,这里就有个解析的坑,已经将cdn域名都配置好的,但是一直访问不了,白屏现象 后面排除源站问题和cdn配置问题, ...