Problem - 1286

用容斥原理做的代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6
 7 using namespace std;
 8
 9 const int N = 33333;
10
11 int last[N];
12 void pre() {
13     last[1] = 1;
14     for (int i = 2; i < N; i++) {
15         if (!last[i]) {
16             for (int j = i; j < N; j += i) {
17                 last[j] = i;
18             }
19         }
20     }
21 //    for (int i = 0; i < 20; i++) cout << last[i] << endl;
22 }
23
24 vector<int> fac;
25 void getFac(int n) {
26     fac.clear();
27     while (n > 1) {
28         fac.push_back(last[n]);
29         n /= last[n];
30     }
31     int t = (int) (unique(fac.begin(), fac.end()) - fac.begin());
32     while (fac.size() > t) fac.pop_back();
33 }
34
35 int cntBit(int n) { return n > 0 ? cntBit(n >> 1) + (n & 1) : 0;}
36
37 int main() {
38     pre();
39     int T, n;
40     cin >> T;
41     while (T-- && cin >> n) {
42         getFac(n);
43         int ans = 0;
44         for (int i = 1, szi = 1 << fac.size(); i < szi; i++) {
45             int tmp = 1;
46             for (int j = 0, szj = fac.size(); j < szj; j++) {
47                 if (i & 1 << j) tmp *= fac[j];
48             }
49             if (cntBit(i) & 1) {
50                 ans += n / tmp;
51             } else {
52                 ans -= n / tmp;
53             }
54         }
55         cout << n - ans << endl;
56     }
57     return 0;
58 }

View Code

用欧拉函数做的代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6
 7 using namespace std;
 8
 9 const int N = 33333;
10
11 int last[N];
12 void pre() {
13     last[1] = 1;
14     for (int i = 2; i < N; i++) {
15         if (!last[i]) {
16             for (int j = i; j < N; j += i) {
17                 last[j] = i;
18             }
19         }
20     }
21 //    for (int i = 0; i < 20; i++) cout << last[i] << endl;
22 }
23
24 int phi(int n) {
25     int ret = 1;
26     while (n > 1) {
27         int tmp = last[n];
28 //        cout << tmp << endl;
29         ret *= tmp - 1;
30         n /= last[n];
31         while (tmp == last[n]) {
32             ret *= tmp;
33             n /= last[n];
34         }
35     }
36     return ret;
37 }
38
39 int main() {
40     pre();
41     int T, n;
42     cin >> T;
43     while (T-- && cin >> n) cout << phi(n) << endl;
44     return 0;
45 }

View Code

——written by Lyon

转载于:https://www.cnblogs.com/LyonLys/p/hdu_1286_Lyon.html

hdu 1286 找新朋友 (容斥原理 || 欧拉函数)相关推荐

  1. HDU 1286 找新朋友 (欧拉函数)

    找新朋友 http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=8 T ...

  2. HDU1286 找新朋友【欧拉函数】

    找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

  3. 找新朋友(欧拉函数)

    新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的公约数,否则都是新 ...

  4. hdu 1286 找新朋友 欧拉函数模版题

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  5. HDOJ 1286 HDU 1286 找新朋友 ACM 1286 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目描述:          http://acm.hdu.edu.cn/showproblem.php?pid=1286 ...

  6. hdu 1286找新朋友 (简单数学题)

    点击打开链接 第一次暴力求解,tle #include"stdio.h" #include"string.h" int fun(int x,int y) {wh ...

  7. 杭电1286找新朋友

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. HDU 5597 GTW likes function(规律+欧拉函数模板题)——BestCoder Round #66(div.1 div.2)

    GTW likes function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  9. hdu 4983 Goffi and GCD(欧拉函数)

    Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...

最新文章

  1. 如何设计一个优秀的向导式界面(Wizard)
  2. 一个想法不一定对 系列
  3. python中的glob 模块学习文件路径查找
  4. java oauth2.0_教程:如何实现Java OAuth 2.0以使用GitHub和Google登录
  5. Openfire3.9.3源代码导入eclipse中开发配置指南(转载)
  6. 分享一个 GIFControl 控件
  7. 打开QQ快捷键截屏 CTRL+ALT+A
  8. 181008有道扇贝每日一句
  9. 论文阅读《Vision-Aided RAIM: A New Method for GPS Integrity Monitoring in Approach and Landing Phase》3
  10. 文件夹如何改名,教你批量重命名文件夹名方法
  11. python的口算自动生成系统(GUI界面编程)
  12. 台式计算机有哪些硬件,台式机包括什么
  13. 传统建材采购的痛点有哪些?
  14. 错误的robots文件设置对SEO的影响
  15. 快速集成百度定位功能
  16. 无限极分类在html怎么用,wxj.html
  17. java编写车类_用Java程序创建一个汽车接口,接口中要定义汽车应有的属性和行为,随后编写多个汽车接口的实现类,...
  18. iOS开发--AVFoundation视频合成(添加背景音乐)
  19. 大数据、人工智能,未来已来
  20. 控制面板中的管理工具提示位置不可用

热门文章

  1. yum报错-Network is unreachableError:
  2. IDEA中Maven项目使用Junit4单元测试的写法
  3. 通过composer安装阿里大于接口扩展
  4. Linux0.11内核--加载可执行二进制文件之1.copy_strings
  5. ListView 与 它里面的Item 点击滑动冲突
  6. eclipse 环境下整合 struts2+spring+hibernate 开发web应用常见问题及解答
  7. Java中的数组数据结构需要了解的要点
  8. Java集合框架体系(超详细)
  9. SQL自动流水号函数
  10. 网络对抗作业——程瑀